blob: 054e5e0945f3ce34392bb037ab432ebd6dd6ed79 [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;
Charles Chen3d9fc0f2020-03-11 10:19:04 +080022import android.annotation.Nullable;
Makoto Onuki0ceb3e02019-11-18 10:37:10 -080023import android.annotation.SystemApi;
Makoto Onukid67070e2018-03-30 12:39:14 -070024import android.app.ContextImpl.ServiceInitializationState;
Jeff Brown6e539312015-02-24 18:53:21 -080025import android.app.admin.DevicePolicyManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060026import android.app.admin.IDevicePolicyManager;
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 Fullerad6eb2b2020-01-28 10:55:15 +000036import android.app.timedetector.TimeDetectorImpl;
Neil Fuller328532a2017-03-16 18:32:21 +000037import android.app.timezone.RulesManager;
Neil Fuller3e3b5402019-11-07 15:35:05 +000038import android.app.timezonedetector.TimeZoneDetector;
Neil Fullerad6eb2b2020-01-28 10:55:15 +000039import android.app.timezonedetector.TimeZoneDetectorImpl;
Jeff Brown6e539312015-02-24 18:53:21 -080040import android.app.trust.TrustManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070041import android.app.usage.IStorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080042import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000043import android.app.usage.NetworkStatsManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070044import android.app.usage.StorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080045import android.app.usage.UsageStatsManager;
46import android.appwidget.AppWidgetManager;
47import android.bluetooth.BluetoothManager;
Eugene Susla6ed45d82017-01-22 13:52:51 -080048import android.companion.CompanionDeviceManager;
49import android.companion.ICompanionDeviceManager;
Jeff Brown6e539312015-02-24 18:53:21 -080050import android.content.ClipboardManager;
Felipe Leme326f15a2019-02-19 09:42:24 -080051import android.content.ContentCaptureOptions;
Jeff Brown6e539312015-02-24 18:53:21 -080052import android.content.Context;
53import android.content.IRestrictionsManager;
54import android.content.RestrictionsManager;
Song Pan6e3677c2019-10-29 14:19:26 +000055import android.content.integrity.AppIntegrityManager;
56import android.content.integrity.IAppIntegrityManager;
Hyunyoung Song880a9512018-12-20 11:24:48 -080057import android.content.om.IOverlayManager;
58import android.content.om.OverlayManager;
Felipe Leme8ac0d0d2020-01-14 13:37:41 -080059import android.content.pm.ApplicationInfo;
Tony Makb0d22622018-01-18 12:49:49 +000060import android.content.pm.CrossProfileApps;
Songchun Fan6cde9d92019-12-10 10:23:54 -080061import android.content.pm.DataLoaderManager;
Tony Makb0d22622018-01-18 12:49:49 +000062import android.content.pm.ICrossProfileApps;
Songchun Fan6cde9d92019-12-10 10:23:54 -080063import android.content.pm.IDataLoaderManager;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070064import android.content.pm.IPackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080065import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080066import android.content.pm.LauncherApps;
Svet Ganovec7daa52017-06-30 12:02:59 -070067import android.content.pm.PackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080068import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080069import android.content.res.Resources;
Richard Uhlerb29f1452018-09-12 16:38:15 +010070import android.content.rollback.IRollbackManager;
71import android.content.rollback.RollbackManager;
Kenny Rootf74bfde2018-01-18 15:42:48 -080072import android.debug.AdbManager;
73import android.debug.IAdbManager;
Jeff Brown6e539312015-02-24 18:53:21 -080074import android.hardware.ConsumerIrManager;
75import android.hardware.ISerialManager;
76import android.hardware.SensorManager;
Chad Brubaker90f391f2018-10-19 10:26:19 -070077import android.hardware.SensorPrivacyManager;
Jeff Brown6e539312015-02-24 18:53:21 -080078import android.hardware.SerialManager;
79import android.hardware.SystemSensorManager;
Kevin Chyn05c21502018-09-18 13:07:19 -070080import android.hardware.biometrics.BiometricManager;
Ilya Matyukhine4675b32019-11-07 16:07:19 -080081import android.hardware.biometrics.IAuthService;
Jeff Brown6e539312015-02-24 18:53:21 -080082import android.hardware.camera2.CameraManager;
Christine Franks39b03112018-07-03 14:46:07 -070083import android.hardware.display.ColorDisplayManager;
Jeff Brown6e539312015-02-24 18:53:21 -080084import android.hardware.display.DisplayManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020085import android.hardware.face.FaceManager;
86import android.hardware.face.IFaceService;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060087import android.hardware.fingerprint.FingerprintManager;
88import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -080089import android.hardware.hdmi.HdmiControlManager;
90import android.hardware.hdmi.IHdmiControlService;
91import android.hardware.input.InputManager;
Kevin Chyn51676d22018-11-05 18:00:43 -080092import android.hardware.iris.IIrisService;
Felipe Lemee348dc32018-11-05 12:35:29 -080093import android.hardware.iris.IrisManager;
Ivailo Karamanolev090d02c2020-01-09 17:02:49 +010094import android.hardware.lights.LightsManager;
Peng Xu9ff7d222016-02-11 13:02:05 -080095import android.hardware.location.ContextHubManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060096import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080097import android.hardware.usb.IUsbManager;
98import android.hardware.usb.UsbManager;
99import android.location.CountryDetector;
100import android.location.ICountryDetector;
101import android.location.ILocationManager;
102import android.location.LocationManager;
103import android.media.AudioManager;
104import android.media.MediaRouter;
105import android.media.midi.IMidiManager;
106import android.media.midi.MidiManager;
107import android.media.projection.MediaProjectionManager;
108import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800109import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800110import android.media.tv.ITvInputManager;
111import android.media.tv.TvInputManager;
Amy92726fb2020-02-12 17:47:19 -0800112import android.media.tv.tunerresourcemanager.ITunerResourceManager;
113import android.media.tv.tunerresourcemanager.TunerResourceManager;
Cody Kestingb125776002019-12-17 17:21:40 -0800114import android.net.ConnectivityDiagnosticsManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800115import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700116import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -0800117import android.net.EthernetManager;
118import android.net.IConnectivityManager;
119import android.net.IEthernetManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800120import android.net.IIpSecService;
Jeff Brown6e539312015-02-24 18:53:21 -0800121import android.net.INetworkPolicyManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800122import android.net.ITestNetworkManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800123import android.net.IpSecManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800124import android.net.NetworkPolicyManager;
125import android.net.NetworkScoreManager;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100126import android.net.NetworkWatchlistManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800127import android.net.TestNetworkManager;
markchienae8aa642019-12-16 20:15:20 +0800128import android.net.TetheringManager;
Benedict Wong50b44432019-11-01 16:46:28 -0700129import android.net.VpnManager;
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700130import android.net.lowpan.ILowpanManager;
131import android.net.lowpan.LowpanManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700132import android.net.nsd.INsdManager;
133import android.net.nsd.NsdManager;
David Su409f3712019-10-24 11:18:41 -0700134import android.net.wifi.WifiFrameworkInitializer;
Etan Cohen804d94a2020-02-15 17:33:27 -0800135import android.net.wifi.nl80211.WifiNl80211Manager;
Jeff Brown6e539312015-02-24 18:53:21 -0800136import android.nfc.NfcManager;
137import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600138import android.os.BatteryStats;
Roshan Pius848513e2019-10-11 13:44:00 -0700139import android.os.BatteryStatsManager;
Nandana Duttd11850c2018-12-12 17:26:57 +0000140import android.os.BugreportManager;
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600141import android.os.Build;
Jeff Brown6e539312015-02-24 18:53:21 -0800142import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100143import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600144import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -0800145import android.os.IBinder;
Nandana Duttd11850c2018-12-12 17:26:57 +0000146import android.os.IDumpstate;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100147import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800148import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800149import android.os.IRecoverySystem;
Tao Bao07342dc2017-01-24 15:08:21 -0800150import android.os.ISystemUpdateManager;
Dan Stozab0470582019-11-12 16:09:41 -0800151import android.os.IThermalService;
Jeff Brown6e539312015-02-24 18:53:21 -0800152import android.os.IUserManager;
Joe Onorato1754d742016-11-21 17:51:35 -0800153import android.os.IncidentManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800154import android.os.PowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800155import android.os.RecoverySystem;
Benedict Wong99a48412018-11-09 14:45:34 -0800156import android.os.RemoteException;
Jeff Brown6e539312015-02-24 18:53:21 -0800157import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600158import android.os.ServiceManager.ServiceNotFoundException;
Jeffrey Huangf58800b2020-01-23 13:22:10 -0800159import android.os.StatsFrameworkInitializer;
Hall Liub38ad5f2019-12-23 18:11:10 -0800160import android.os.SystemConfigManager;
Tao Bao07342dc2017-01-24 15:08:21 -0800161import android.os.SystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800162import android.os.SystemVibrator;
163import android.os.UserHandle;
164import android.os.UserManager;
165import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800166import android.os.health.SystemHealthManager;
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +0800167import android.os.image.DynamicSystemManager;
168import android.os.image.IDynamicSystemService;
Alex Buynytskyy716588d2020-02-04 13:42:59 -0800169import android.os.incremental.IIncrementalService;
Songchun Fan43f18ea2019-12-11 17:54:38 -0800170import android.os.incremental.IncrementalManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800171import android.os.storage.StorageManager;
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800172import android.permission.PermissionControllerManager;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700173import android.permission.PermissionManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800174import android.print.IPrintManager;
175import android.print.PrintManager;
Victor Hsieh20fe1f62019-09-30 13:36:21 -0700176import android.security.FileIntegrityManager;
177import android.security.IFileIntegrityService;
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000178import android.service.oemlock.IOemLockService;
179import android.service.oemlock.OemLockManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800180import android.service.persistentdata.IPersistentDataBlockService;
181import android.service.persistentdata.PersistentDataBlockManager;
Zak Cohen56345f42017-01-26 13:54:28 -0800182import android.service.vr.IVrManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800183import android.telecom.TelecomManager;
Sarah Chin4affb512020-01-10 16:09:11 -0800184import android.telephony.MmsManager;
Jayachandran C58fd3eb2019-11-20 19:01:56 -0800185import android.telephony.TelephonyFrameworkInitializer;
Makoto Onuki54c4e032019-10-30 12:05:20 -0700186import android.telephony.TelephonyRegistryManager;
Robert Benea1901a5b2018-05-15 16:36:07 -0700187import android.util.ArrayMap;
Jeff Brown6e539312015-02-24 18:53:21 -0800188import android.util.Log;
189import android.view.ContextThemeWrapper;
190import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800191import android.view.WindowManager;
192import android.view.WindowManagerImpl;
193import android.view.accessibility.AccessibilityManager;
194import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100195import android.view.autofill.AutofillManager;
196import android.view.autofill.IAutoFillManager;
Felipe Leme749b8892018-12-03 16:30:30 -0800197import android.view.contentcapture.ContentCaptureManager;
198import android.view.contentcapture.IContentCaptureManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800199import android.view.inputmethod.InputMethodManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000200import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800201import android.view.textservice.TextServicesManager;
202
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600203import com.android.internal.app.IAppOpsService;
204import com.android.internal.app.IBatteryStats;
205import com.android.internal.app.ISoundTriggerService;
206import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100207import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600208import com.android.internal.os.IDropBoxManagerService;
209import com.android.internal.policy.PhoneLayoutInflater;
Makoto Onuki05b14f52019-11-05 12:36:07 -0800210import com.android.internal.util.Preconditions;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600211
Robert Benea1901a5b2018-05-15 16:36:07 -0700212import java.util.Map;
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +0000213import java.util.Objects;
Jeff Brown6e539312015-02-24 18:53:21 -0800214
215/**
216 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
217 * Used by {@link ContextImpl}.
Makoto Onuki792de8d2019-07-16 16:19:01 -0700218 *
219 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -0800220 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -0800221@SystemApi
Makoto Onuki792de8d2019-07-16 16:19:01 -0700222public final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600223 private static final String TAG = "SystemServiceRegistry";
224
Jeff Brown6e539312015-02-24 18:53:21 -0800225 // Service registry information.
226 // This information is never changed once static initialization has completed.
Robert Benea1901a5b2018-05-15 16:36:07 -0700227 private static final Map<Class<?>, String> SYSTEM_SERVICE_NAMES =
228 new ArrayMap<Class<?>, String>();
229 private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
230 new ArrayMap<String, ServiceFetcher<?>>();
Charles Chen3d9fc0f2020-03-11 10:19:04 +0800231 private static final Map<String, String> SYSTEM_SERVICE_CLASS_NAMES = new ArrayMap<>();
232
Jeff Brown6e539312015-02-24 18:53:21 -0800233 private static int sServiceCacheSize;
234
Makoto Onuki05b14f52019-11-05 12:36:07 -0800235 private static volatile boolean sInitializing;
236
Jeff Brown6e539312015-02-24 18:53:21 -0800237 // Not instantiable.
238 private SystemServiceRegistry() { }
239
240 static {
Felipe Lemeecb08be2018-11-27 15:48:47 -0800241 //CHECKSTYLE:OFF IndentationCheck
Jeff Brown6e539312015-02-24 18:53:21 -0800242 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
243 new CachedServiceFetcher<AccessibilityManager>() {
244 @Override
245 public AccessibilityManager createService(ContextImpl ctx) {
246 return AccessibilityManager.getInstance(ctx);
247 }});
248
249 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
250 new CachedServiceFetcher<CaptioningManager>() {
251 @Override
252 public CaptioningManager createService(ContextImpl ctx) {
253 return new CaptioningManager(ctx);
254 }});
255
256 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
257 new CachedServiceFetcher<AccountManager>() {
258 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600259 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
260 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800261 IAccountManager service = IAccountManager.Stub.asInterface(b);
262 return new AccountManager(ctx, service);
263 }});
264
265 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
266 new CachedServiceFetcher<ActivityManager>() {
267 @Override
268 public ActivityManager createService(ContextImpl ctx) {
269 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
270 }});
271
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700272 registerService(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class,
273 new CachedServiceFetcher<ActivityTaskManager>() {
274 @Override
275 public ActivityTaskManager createService(ContextImpl ctx) {
276 return new ActivityTaskManager(
277 ctx.getOuterContext(), ctx.mMainThread.getHandler());
278 }});
279
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700280 registerService(Context.URI_GRANTS_SERVICE, UriGrantsManager.class,
281 new CachedServiceFetcher<UriGrantsManager>() {
282 @Override
283 public UriGrantsManager createService(ContextImpl ctx) {
284 return new UriGrantsManager(
285 ctx.getOuterContext(), ctx.mMainThread.getHandler());
286 }});
287
Jeff Brown6e539312015-02-24 18:53:21 -0800288 registerService(Context.ALARM_SERVICE, AlarmManager.class,
289 new CachedServiceFetcher<AlarmManager>() {
290 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600291 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
292 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800293 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
294 return new AlarmManager(service, ctx);
295 }});
296
297 registerService(Context.AUDIO_SERVICE, AudioManager.class,
298 new CachedServiceFetcher<AudioManager>() {
299 @Override
300 public AudioManager createService(ContextImpl ctx) {
301 return new AudioManager(ctx);
302 }});
303
304 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
305 new CachedServiceFetcher<MediaRouter>() {
306 @Override
307 public MediaRouter createService(ContextImpl ctx) {
308 return new MediaRouter(ctx);
309 }});
310
311 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
312 new CachedServiceFetcher<BluetoothManager>() {
313 @Override
314 public BluetoothManager createService(ContextImpl ctx) {
315 return new BluetoothManager(ctx);
316 }});
317
318 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
319 new StaticServiceFetcher<HdmiControlManager>() {
320 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600321 public HdmiControlManager createService() throws ServiceNotFoundException {
322 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800323 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
324 }});
325
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000326 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000327 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000328 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000329 public TextClassificationManager createService(ContextImpl ctx) {
330 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000331 }});
332
Jeff Brown6e539312015-02-24 18:53:21 -0800333 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
334 new CachedServiceFetcher<ClipboardManager>() {
335 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600336 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800337 return new ClipboardManager(ctx.getOuterContext(),
338 ctx.mMainThread.getHandler());
339 }});
340
341 // The clipboard service moved to a new package. If someone asks for the old
342 // interface by class then we want to redirect over to the new interface instead
343 // (which extends it).
344 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
345
346 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900347 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800348 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600349 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
350 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900351 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
352 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800353 }});
354
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900355 registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() {
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900356 @Override
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900357 public IBinder createService() throws ServiceNotFoundException {
358 return ServiceManager.getServiceOrThrow(Context.NETD_SERVICE);
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900359 }
360 });
361
markchienae8aa642019-12-16 20:15:20 +0800362 registerService(Context.TETHERING_SERVICE, TetheringManager.class,
363 new CachedServiceFetcher<TetheringManager>() {
364 @Override
Remi NGUYEN VAN24b7b6a2020-01-27 17:26:14 +0900365 public TetheringManager createService(ContextImpl ctx) {
366 return new TetheringManager(
367 ctx, () -> ServiceManager.getService(Context.TETHERING_SERVICE));
markchienae8aa642019-12-16 20:15:20 +0800368 }});
369
370
Nathan Harold28084d82017-03-01 18:55:06 -0800371 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Nathan Harold592dadb2018-03-15 18:06:06 -0700372 new CachedServiceFetcher<IpSecManager>() {
Nathan Harold28084d82017-03-01 18:55:06 -0800373 @Override
Nathan Harold592dadb2018-03-15 18:06:06 -0700374 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nathan Harold28084d82017-03-01 18:55:06 -0800375 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
376 IIpSecService service = IIpSecService.Stub.asInterface(b);
Nathan Harold592dadb2018-03-15 18:06:06 -0700377 return new IpSecManager(ctx, service);
Nathan Harold28084d82017-03-01 18:55:06 -0800378 }});
379
Benedict Wong50b44432019-11-01 16:46:28 -0700380 registerService(Context.VPN_MANAGEMENT_SERVICE, VpnManager.class,
381 new CachedServiceFetcher<VpnManager>() {
382 @Override
383 public VpnManager createService(ContextImpl ctx) throws ServiceNotFoundException {
384 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
385 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
386 return new VpnManager(ctx, service);
387 }});
388
Cody Kestingb125776002019-12-17 17:21:40 -0800389 registerService(Context.CONNECTIVITY_DIAGNOSTICS_SERVICE,
390 ConnectivityDiagnosticsManager.class,
391 new CachedServiceFetcher<ConnectivityDiagnosticsManager>() {
392 @Override
393 public ConnectivityDiagnosticsManager createService(ContextImpl ctx)
394 throws ServiceNotFoundException {
395 // ConnectivityDiagnosticsManager is backed by ConnectivityService
396 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
397 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
398 return new ConnectivityDiagnosticsManager(ctx, service);
399 }});
400
Benedict Wong99a48412018-11-09 14:45:34 -0800401 registerService(
402 Context.TEST_NETWORK_SERVICE,
403 TestNetworkManager.class,
404 new StaticApplicationContextServiceFetcher<TestNetworkManager>() {
405 @Override
406 public TestNetworkManager createService(Context context)
407 throws ServiceNotFoundException {
408 IBinder csBinder =
409 ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
410 IConnectivityManager csMgr =
411 IConnectivityManager.Stub.asInterface(csBinder);
412
413 final IBinder tnBinder;
414 try {
415 tnBinder = csMgr.startOrGetTestNetworkService();
416 } catch (RemoteException e) {
417 throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE);
418 }
419 ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder);
Benedict Wongb4e98f32019-03-12 21:54:16 -0700420 return new TestNetworkManager(tnMgr);
Benedict Wong99a48412018-11-09 14:45:34 -0800421 }
422 });
423
Jeff Brown6e539312015-02-24 18:53:21 -0800424 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
425 new StaticServiceFetcher<CountryDetector>() {
426 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600427 public CountryDetector createService() throws ServiceNotFoundException {
428 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800429 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
430 }});
431
432 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
433 new CachedServiceFetcher<DevicePolicyManager>() {
434 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600435 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
436 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
437 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800438 }});
439
440 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
441 new CachedServiceFetcher<DownloadManager>() {
442 @Override
443 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700444 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800445 }});
446
447 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700448 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800449 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700450 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600451 IBatteryStats stats = IBatteryStats.Stub.asInterface(
452 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
453 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
454 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700455 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800456 }});
457
458 registerService(Context.NFC_SERVICE, NfcManager.class,
459 new CachedServiceFetcher<NfcManager>() {
460 @Override
461 public NfcManager createService(ContextImpl ctx) {
462 return new NfcManager(ctx);
463 }});
464
465 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700466 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800467 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600468 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
469 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800470 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700471 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800472 }});
473
474 registerService(Context.INPUT_SERVICE, InputManager.class,
475 new StaticServiceFetcher<InputManager>() {
476 @Override
477 public InputManager createService() {
478 return InputManager.getInstance();
479 }});
480
481 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
482 new CachedServiceFetcher<DisplayManager>() {
483 @Override
484 public DisplayManager createService(ContextImpl ctx) {
485 return new DisplayManager(ctx.getOuterContext());
486 }});
487
Christine Franks39b03112018-07-03 14:46:07 -0700488 registerService(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class,
489 new CachedServiceFetcher<ColorDisplayManager>() {
490 @Override
491 public ColorDisplayManager createService(ContextImpl ctx) {
492 return new ColorDisplayManager();
493 }
494 });
495
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800496 // InputMethodManager has its own cache strategy based on display id to support apps that
497 // still assume InputMethodManager is a per-process singleton and it's safe to directly
498 // access internal fields via reflection. Hence directly use ServiceFetcher instead of
499 // StaticServiceFetcher/CachedServiceFetcher.
Jeff Brown6e539312015-02-24 18:53:21 -0800500 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800501 new ServiceFetcher<InputMethodManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800502 @Override
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800503 public InputMethodManager getService(ContextImpl ctx) {
504 return InputMethodManager.forContext(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800505 }});
506
507 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800508 new CachedServiceFetcher<TextServicesManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800509 @Override
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800510 public TextServicesManager createService(ContextImpl ctx)
511 throws ServiceNotFoundException {
512 return TextServicesManager.createInstance(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800513 }});
514
515 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800516 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800517 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800518 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800519 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800520 }});
521
522 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
523 new CachedServiceFetcher<LayoutInflater>() {
524 @Override
525 public LayoutInflater createService(ContextImpl ctx) {
526 return new PhoneLayoutInflater(ctx.getOuterContext());
527 }});
528
529 registerService(Context.LOCATION_SERVICE, LocationManager.class,
530 new CachedServiceFetcher<LocationManager>() {
531 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600532 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
533 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800534 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
535 }});
536
537 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700538 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800539 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600540 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700541 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600542 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800543 }});
544
545 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
546 new CachedServiceFetcher<NotificationManager>() {
547 @Override
548 public NotificationManager createService(ContextImpl ctx) {
549 final Context outerContext = ctx.getOuterContext();
550 return new NotificationManager(
551 new ContextThemeWrapper(outerContext,
552 Resources.selectSystemTheme(0,
553 outerContext.getApplicationInfo().targetSdkVersion,
554 com.android.internal.R.style.Theme_Dialog,
555 com.android.internal.R.style.Theme_Holo_Dialog,
556 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
557 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
558 ctx.mMainThread.getHandler());
559 }});
560
561 registerService(Context.NSD_SERVICE, NsdManager.class,
562 new CachedServiceFetcher<NsdManager>() {
563 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600564 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
565 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800566 INsdManager service = INsdManager.Stub.asInterface(b);
567 return new NsdManager(ctx.getOuterContext(), service);
568 }});
569
570 registerService(Context.POWER_SERVICE, PowerManager.class,
571 new CachedServiceFetcher<PowerManager>() {
572 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600573 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Dan Stozab0470582019-11-12 16:09:41 -0800574 IBinder powerBinder = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
575 IPowerManager powerService = IPowerManager.Stub.asInterface(powerBinder);
576 IBinder thermalBinder = ServiceManager.getServiceOrThrow(Context.THERMAL_SERVICE);
577 IThermalService thermalService = IThermalService.Stub.asInterface(thermalBinder);
578 return new PowerManager(ctx.getOuterContext(), powerService, thermalService,
579 ctx.mMainThread.getHandler());
Jeff Brown6e539312015-02-24 18:53:21 -0800580 }});
581
Tao Baoe8a403d2015-12-31 07:44:55 -0800582 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
583 new CachedServiceFetcher<RecoverySystem>() {
584 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600585 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
586 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800587 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800588 return new RecoverySystem(service);
589 }});
590
Jeff Brown6e539312015-02-24 18:53:21 -0800591 registerService(Context.SEARCH_SERVICE, SearchManager.class,
592 new CachedServiceFetcher<SearchManager>() {
593 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600594 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800595 return new SearchManager(ctx.getOuterContext(),
596 ctx.mMainThread.getHandler());
597 }});
598
599 registerService(Context.SENSOR_SERVICE, SensorManager.class,
600 new CachedServiceFetcher<SensorManager>() {
601 @Override
602 public SensorManager createService(ContextImpl ctx) {
603 return new SystemSensorManager(ctx.getOuterContext(),
604 ctx.mMainThread.getHandler().getLooper());
605 }});
606
Chad Brubaker90f391f2018-10-19 10:26:19 -0700607 registerService(Context.SENSOR_PRIVACY_SERVICE, SensorPrivacyManager.class,
608 new CachedServiceFetcher<SensorPrivacyManager>() {
609 @Override
610 public SensorPrivacyManager createService(ContextImpl ctx) {
611 return SensorPrivacyManager.getInstance(ctx);
612 }});
613
Jeff Brown6e539312015-02-24 18:53:21 -0800614 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
615 new CachedServiceFetcher<StatusBarManager>() {
616 @Override
617 public StatusBarManager createService(ContextImpl ctx) {
618 return new StatusBarManager(ctx.getOuterContext());
619 }});
620
621 registerService(Context.STORAGE_SERVICE, StorageManager.class,
622 new CachedServiceFetcher<StorageManager>() {
623 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600624 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700625 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800626 }});
627
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700628 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
629 new CachedServiceFetcher<StorageStatsManager>() {
630 @Override
631 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
632 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
633 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
634 return new StorageStatsManager(ctx, service);
635 }});
636
Tao Bao07342dc2017-01-24 15:08:21 -0800637 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
638 new CachedServiceFetcher<SystemUpdateManager>() {
639 @Override
640 public SystemUpdateManager createService(ContextImpl ctx)
641 throws ServiceNotFoundException {
642 IBinder b = ServiceManager.getServiceOrThrow(
643 Context.SYSTEM_UPDATE_SERVICE);
644 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
645 return new SystemUpdateManager(service);
646 }});
647
Hall Liub38ad5f2019-12-23 18:11:10 -0800648 registerService(Context.SYSTEM_CONFIG_SERVICE, SystemConfigManager.class,
649 new CachedServiceFetcher<SystemConfigManager>() {
650 @Override
651 public SystemConfigManager createService(ContextImpl ctx) {
652 return new SystemConfigManager();
653 }});
654
Chen Xu288b71c2019-09-15 18:28:21 -0700655 registerService(Context.TELEPHONY_REGISTRY_SERVICE, TelephonyRegistryManager.class,
656 new CachedServiceFetcher<TelephonyRegistryManager>() {
657 @Override
658 public TelephonyRegistryManager createService(ContextImpl ctx) {
Chen Xu1f6cfa52019-10-13 17:30:32 -0700659 return new TelephonyRegistryManager(ctx);
Chen Xu288b71c2019-09-15 18:28:21 -0700660 }});
661
Jeff Brown6e539312015-02-24 18:53:21 -0800662 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
663 new CachedServiceFetcher<TelecomManager>() {
664 @Override
665 public TelecomManager createService(ContextImpl ctx) {
666 return new TelecomManager(ctx.getOuterContext());
667 }});
668
Sarah Chin4affb512020-01-10 16:09:11 -0800669 registerService(Context.MMS_SERVICE, MmsManager.class,
670 new CachedServiceFetcher<MmsManager>() {
671 @Override
672 public MmsManager createService(ContextImpl ctx) {
673 return new MmsManager(ctx.getOuterContext());
674 }});
675
Jeff Brown6e539312015-02-24 18:53:21 -0800676 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
677 new CachedServiceFetcher<UiModeManager>() {
678 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600679 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Tyler Gunndaef6682019-09-30 15:12:24 -0700680 return new UiModeManager(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800681 }});
682
683 registerService(Context.USB_SERVICE, UsbManager.class,
684 new CachedServiceFetcher<UsbManager>() {
685 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600686 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
687 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800688 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
689 }});
690
Kenny Rootf74bfde2018-01-18 15:42:48 -0800691 registerService(Context.ADB_SERVICE, AdbManager.class,
692 new CachedServiceFetcher<AdbManager>() {
693 @Override
694 public AdbManager createService(ContextImpl ctx)
695 throws ServiceNotFoundException {
696 IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE);
697 return new AdbManager(ctx, IAdbManager.Stub.asInterface(b));
698 }});
699
Jeff Brown6e539312015-02-24 18:53:21 -0800700 registerService(Context.SERIAL_SERVICE, SerialManager.class,
701 new CachedServiceFetcher<SerialManager>() {
702 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600703 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
704 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800705 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
706 }});
707
708 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
709 new CachedServiceFetcher<Vibrator>() {
710 @Override
711 public Vibrator createService(ContextImpl ctx) {
712 return new SystemVibrator(ctx);
713 }});
714
715 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
716 new CachedServiceFetcher<WallpaperManager>() {
717 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800718 public WallpaperManager createService(ContextImpl ctx)
719 throws ServiceNotFoundException {
Felipe Leme34a861a2019-08-05 16:00:12 -0700720 final IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
721 if (b == null) {
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800722 ApplicationInfo appInfo = ctx.getApplicationInfo();
723 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.P
724 && appInfo.isInstantApp()) {
Felipe Leme34a861a2019-08-05 16:00:12 -0700725 // Instant app
726 throw new ServiceNotFoundException(Context.WALLPAPER_SERVICE);
727 }
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800728 final boolean enabled = Resources.getSystem()
729 .getBoolean(com.android.internal.R.bool.config_enableWallpaperService);
730 if (!enabled) {
731 // Device doesn't support wallpaper, return a limited manager
732 return DisabledWallpaperManager.getInstance();
733 }
Felipe Leme34a861a2019-08-05 16:00:12 -0700734 // Bad state - WallpaperManager methods will throw exception
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800735 Log.e(TAG, "No wallpaper service");
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800736 }
737 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
738 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800739 ctx.mMainThread.getHandler());
740 }});
741
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700742 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
743 new CachedServiceFetcher<LowpanManager>() {
744 @Override
745 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
746 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
747 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
748 return new LowpanManager(ctx.getOuterContext(), service,
749 ConnectivityThread.getInstanceLooper());
750 }});
751
Jeff Brown6e539312015-02-24 18:53:21 -0800752 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
753 new CachedServiceFetcher<EthernetManager>() {
754 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600755 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
756 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800757 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
758 return new EthernetManager(ctx.getOuterContext(), service);
759 }});
760
Etan Cohenb9f28bc2020-02-09 12:06:56 -0800761 registerService(Context.WIFI_NL80211_SERVICE, WifiNl80211Manager.class,
762 new CachedServiceFetcher<WifiNl80211Manager>() {
Etan Cohen73450f22019-12-12 23:29:25 -0800763 @Override
Etan Cohenb9f28bc2020-02-09 12:06:56 -0800764 public WifiNl80211Manager createService(ContextImpl ctx) {
765 return new WifiNl80211Manager(ctx.getOuterContext());
Etan Cohen73450f22019-12-12 23:29:25 -0800766 }
767 });
768
Jeff Brown6e539312015-02-24 18:53:21 -0800769 registerService(Context.WINDOW_SERVICE, WindowManager.class,
770 new CachedServiceFetcher<WindowManager>() {
771 @Override
772 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700773 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800774 }});
775
776 registerService(Context.USER_SERVICE, UserManager.class,
777 new CachedServiceFetcher<UserManager>() {
778 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600779 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
780 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800781 IUserManager service = IUserManager.Stub.asInterface(b);
782 return new UserManager(ctx, service);
783 }});
784
785 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
786 new CachedServiceFetcher<AppOpsManager>() {
787 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600788 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
789 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800790 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
791 return new AppOpsManager(ctx, service);
792 }});
793
794 registerService(Context.CAMERA_SERVICE, CameraManager.class,
795 new CachedServiceFetcher<CameraManager>() {
796 @Override
797 public CameraManager createService(ContextImpl ctx) {
798 return new CameraManager(ctx);
799 }});
800
801 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
802 new CachedServiceFetcher<LauncherApps>() {
803 @Override
804 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700805 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800806 }});
807
808 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
809 new CachedServiceFetcher<RestrictionsManager>() {
810 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600811 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
812 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800813 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
814 return new RestrictionsManager(ctx, service);
815 }});
816
817 registerService(Context.PRINT_SERVICE, PrintManager.class,
818 new CachedServiceFetcher<PrintManager>() {
819 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600820 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700821 IPrintManager service = null;
822 // If the feature not present, don't try to look up every time
823 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
824 service = IPrintManager.Stub.asInterface(ServiceManager
825 .getServiceOrThrow(Context.PRINT_SERVICE));
826 }
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700827 final int userId = ctx.getUserId();
828 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
829 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Jeff Brown6e539312015-02-24 18:53:21 -0800830 }});
831
Eugene Susla6ed45d82017-01-22 13:52:51 -0800832 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
833 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700834 @Override
835 public CompanionDeviceManager createService(ContextImpl ctx)
836 throws ServiceNotFoundException {
837 ICompanionDeviceManager service = null;
838 // If the feature not present, don't try to look up every time
839 if (ctx.getPackageManager().hasSystemFeature(
840 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
841 service = ICompanionDeviceManager.Stub.asInterface(
842 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
843 }
844 return new CompanionDeviceManager(service, ctx.getOuterContext());
845 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800846
Jeff Brown6e539312015-02-24 18:53:21 -0800847 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
848 new CachedServiceFetcher<ConsumerIrManager>() {
849 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600850 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800851 return new ConsumerIrManager(ctx);
852 }});
853
854 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
855 new CachedServiceFetcher<MediaSessionManager>() {
856 @Override
857 public MediaSessionManager createService(ContextImpl ctx) {
858 return new MediaSessionManager(ctx);
859 }});
860
861 registerService(Context.TRUST_SERVICE, TrustManager.class,
862 new StaticServiceFetcher<TrustManager>() {
863 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600864 public TrustManager createService() throws ServiceNotFoundException {
865 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800866 return new TrustManager(b);
867 }});
868
869 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
870 new CachedServiceFetcher<FingerprintManager>() {
871 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600872 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600873 final IBinder binder;
874 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
875 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
876 } else {
877 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
878 }
Jeff Brown6e539312015-02-24 18:53:21 -0800879 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
880 return new FingerprintManager(ctx.getOuterContext(), service);
881 }});
882
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200883 registerService(Context.FACE_SERVICE, FaceManager.class,
884 new CachedServiceFetcher<FaceManager>() {
885 @Override
886 public FaceManager createService(ContextImpl ctx)
887 throws ServiceNotFoundException {
888 final IBinder binder;
889 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
890 binder = ServiceManager.getServiceOrThrow(Context.FACE_SERVICE);
891 } else {
892 binder = ServiceManager.getService(Context.FACE_SERVICE);
893 }
894 IFaceService service = IFaceService.Stub.asInterface(binder);
895 return new FaceManager(ctx.getOuterContext(), service);
896 }
897 });
898
Kevin Chyn51676d22018-11-05 18:00:43 -0800899 registerService(Context.IRIS_SERVICE, IrisManager.class,
900 new CachedServiceFetcher<IrisManager>() {
901 @Override
902 public IrisManager createService(ContextImpl ctx)
903 throws ServiceNotFoundException {
904 final IBinder binder =
905 ServiceManager.getServiceOrThrow(Context.IRIS_SERVICE);
906 IIrisService service = IIrisService.Stub.asInterface(binder);
907 return new IrisManager(ctx.getOuterContext(), service);
908 }
909 });
910
Kevin Chyn05c21502018-09-18 13:07:19 -0700911 registerService(Context.BIOMETRIC_SERVICE, BiometricManager.class,
912 new CachedServiceFetcher<BiometricManager>() {
913 @Override
914 public BiometricManager createService(ContextImpl ctx)
915 throws ServiceNotFoundException {
Kevin Chyn7d07c892020-02-18 18:18:17 -0800916 final IBinder binder =
917 ServiceManager.getServiceOrThrow(Context.AUTH_SERVICE);
918 final IAuthService service =
919 IAuthService.Stub.asInterface(binder);
920 return new BiometricManager(ctx.getOuterContext(), service);
Kevin Chyn05c21502018-09-18 13:07:19 -0700921 }
922 });
923
Jeff Brown6e539312015-02-24 18:53:21 -0800924 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700925 new CachedServiceFetcher<TvInputManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800926 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700927 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600928 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800929 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700930 return new TvInputManager(service, ctx.getUserId());
Jeff Brown6e539312015-02-24 18:53:21 -0800931 }});
932
Amy06a551e2019-11-07 13:44:33 -0800933 registerService(Context.TV_TUNER_RESOURCE_MGR_SERVICE, TunerResourceManager.class,
934 new CachedServiceFetcher<TunerResourceManager>() {
935 @Override
936 public TunerResourceManager createService(ContextImpl ctx)
937 throws ServiceNotFoundException {
938 IBinder iBinder =
939 ServiceManager.getServiceOrThrow(Context.TV_TUNER_RESOURCE_MGR_SERVICE);
940 ITunerResourceManager service = ITunerResourceManager.Stub.asInterface(iBinder);
941 return new TunerResourceManager(service, ctx.getUserId());
942 }});
943
Jeff Brown6e539312015-02-24 18:53:21 -0800944 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
945 new CachedServiceFetcher<NetworkScoreManager>() {
946 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600947 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800948 return new NetworkScoreManager(ctx);
949 }});
950
951 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
952 new CachedServiceFetcher<UsageStatsManager>() {
953 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600954 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
955 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800956 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
957 return new UsageStatsManager(ctx.getOuterContext(), service);
958 }});
959
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000960 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
961 new CachedServiceFetcher<NetworkStatsManager>() {
962 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600963 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000964 return new NetworkStatsManager(ctx.getOuterContext());
965 }});
966
Jeff Brown6e539312015-02-24 18:53:21 -0800967 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
968 new StaticServiceFetcher<PersistentDataBlockManager>() {
969 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600970 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
971 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800972 IPersistentDataBlockService persistentDataBlockService =
973 IPersistentDataBlockService.Stub.asInterface(b);
974 if (persistentDataBlockService != null) {
975 return new PersistentDataBlockManager(persistentDataBlockService);
976 } else {
977 // not supported
978 return null;
979 }
980 }});
981
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000982 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
983 new StaticServiceFetcher<OemLockManager>() {
984 @Override
985 public OemLockManager createService() throws ServiceNotFoundException {
986 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
987 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
988 if (oemLockService != null) {
989 return new OemLockManager(oemLockService);
990 } else {
991 // not supported
992 return null;
993 }
994 }});
995
Jeff Brown6e539312015-02-24 18:53:21 -0800996 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
997 new CachedServiceFetcher<MediaProjectionManager>() {
998 @Override
999 public MediaProjectionManager createService(ContextImpl ctx) {
1000 return new MediaProjectionManager(ctx);
1001 }});
1002
1003 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
1004 new CachedServiceFetcher<AppWidgetManager>() {
1005 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001006 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1007 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -08001008 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
1009 }});
1010
1011 registerService(Context.MIDI_SERVICE, MidiManager.class,
1012 new CachedServiceFetcher<MidiManager>() {
1013 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001014 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1015 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -07001016 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -08001017 }});
Eric Laurent2035ac82015-03-05 15:18:44 -08001018
1019 registerService(Context.RADIO_SERVICE, RadioManager.class,
1020 new CachedServiceFetcher<RadioManager>() {
1021 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -07001022 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -08001023 return new RadioManager(ctx);
1024 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001025
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001026 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
1027 new CachedServiceFetcher<HardwarePropertiesManager>() {
1028 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001029 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1030 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
1031 IHardwarePropertiesManager service =
1032 IHardwarePropertiesManager.Stub.asInterface(b);
1033 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001034 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001035
1036 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
1037 new CachedServiceFetcher<SoundTriggerManager>() {
1038 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001039 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1040 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001041 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
1042 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001043
1044 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
1045 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -08001046 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001047 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1048 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
1049 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -08001050 }});
Joe Onorato713fec82016-03-04 10:34:02 -08001051
Hyunyoung Song880a9512018-12-20 11:24:48 -08001052 registerService(Context.OVERLAY_SERVICE, OverlayManager.class,
1053 new CachedServiceFetcher<OverlayManager>() {
1054 @Override
1055 public OverlayManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1056 IBinder b = ServiceManager.getServiceOrThrow(Context.OVERLAY_SERVICE);
1057 return new OverlayManager(ctx, IOverlayManager.Stub.asInterface(b));
1058 }});
1059
Ricky Wai1a6e6672017-10-27 14:46:01 +01001060 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
1061 new CachedServiceFetcher<NetworkWatchlistManager>() {
1062 @Override
1063 public NetworkWatchlistManager createService(ContextImpl ctx)
1064 throws ServiceNotFoundException {
1065 IBinder b =
1066 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
1067 return new NetworkWatchlistManager(ctx,
1068 INetworkWatchlistManager.Stub.asInterface(b));
1069 }});
1070
Joe Onorato713fec82016-03-04 10:34:02 -08001071 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
1072 new CachedServiceFetcher<SystemHealthManager>() {
1073 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001074 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1075 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
1076 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -08001077 }});
Peng Xu9ff7d222016-02-11 13:02:05 -08001078
1079 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
1080 new CachedServiceFetcher<ContextHubManager>() {
1081 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001082 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -08001083 return new ContextHubManager(ctx.getOuterContext(),
1084 ctx.mMainThread.getHandler().getLooper());
1085 }});
Joe Onorato1754d742016-11-21 17:51:35 -08001086
1087 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
1088 new CachedServiceFetcher<IncidentManager>() {
1089 @Override
1090 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1091 return new IncidentManager(ctx);
1092 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -08001093
Nandana Duttd11850c2018-12-12 17:26:57 +00001094 registerService(Context.BUGREPORT_SERVICE, BugreportManager.class,
1095 new CachedServiceFetcher<BugreportManager>() {
1096 @Override
1097 public BugreportManager createService(ContextImpl ctx)
1098 throws ServiceNotFoundException {
1099 IBinder b = ServiceManager.getServiceOrThrow(Context.BUGREPORT_SERVICE);
1100 return new BugreportManager(ctx.getOuterContext(),
1101 IDumpstate.Stub.asInterface(b));
1102 }});
1103
Felipe Leme640f30a2017-03-06 15:44:06 -08001104 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
1105 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -08001106 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -08001107 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -07001108 // Get the services without throwing as this is an optional feature
1109 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +00001110 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -08001111 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -08001112 }});
Zak Cohen56345f42017-01-26 13:54:28 -08001113
Felipe Lemeecb08be2018-11-27 15:48:47 -08001114 registerService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class,
1115 new CachedServiceFetcher<ContentCaptureManager>() {
Felipe Lemee348dc32018-11-05 12:35:29 -08001116 @Override
Felipe Lemeecb08be2018-11-27 15:48:47 -08001117 public ContentCaptureManager createService(ContextImpl ctx)
Felipe Lemee348dc32018-11-05 12:35:29 -08001118 throws ServiceNotFoundException {
1119 // Get the services without throwing as this is an optional feature
Felipe Lemeecb08be2018-11-27 15:48:47 -08001120 Context outerContext = ctx.getOuterContext();
Felipe Leme326f15a2019-02-19 09:42:24 -08001121 ContentCaptureOptions options = outerContext.getContentCaptureOptions();
1122 // Options is null when the service didn't whitelist the activity or package
Felipe Lemecbf7f262019-04-17 13:57:59 -07001123 if (options != null && (options.lite || options.isWhitelisted(outerContext))) {
Felipe Lemeecb08be2018-11-27 15:48:47 -08001124 IBinder b = ServiceManager
1125 .getService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
Felipe Leme749b8892018-12-03 16:30:30 -08001126 IContentCaptureManager service = IContentCaptureManager.Stub.asInterface(b);
Felipe Leme326f15a2019-02-19 09:42:24 -08001127 // Service is null when not provided by OEM or disabled by kill-switch.
Felipe Lemed49d52c2019-02-15 09:48:20 -08001128 if (service != null) {
Felipe Leme326f15a2019-02-19 09:42:24 -08001129 return new ContentCaptureManager(outerContext, service, options);
Felipe Lemed49d52c2019-02-15 09:48:20 -08001130 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001131 }
Felipe Leme326f15a2019-02-19 09:42:24 -08001132 // When feature is disabled or app / package not whitelisted, we return a null
1133 // manager to apps so the performance impact is practically zero
Felipe Lemeecb08be2018-11-27 15:48:47 -08001134 return null;
Felipe Lemee348dc32018-11-05 12:35:29 -08001135 }});
1136
Sunny Goyal54e91342018-11-14 11:59:02 -08001137 registerService(Context.APP_PREDICTION_SERVICE, AppPredictionManager.class,
1138 new CachedServiceFetcher<AppPredictionManager>() {
1139 @Override
1140 public AppPredictionManager createService(ContextImpl ctx)
1141 throws ServiceNotFoundException {
Felipe Leme6378bd42019-08-14 18:14:55 -07001142 IBinder b = ServiceManager.getService(Context.APP_PREDICTION_SERVICE);
1143 return b == null ? null : new AppPredictionManager(ctx);
Sunny Goyal54e91342018-11-14 11:59:02 -08001144 }
1145 });
1146
Winson Chung3fb0f252019-01-08 17:41:55 -08001147 registerService(Context.CONTENT_SUGGESTIONS_SERVICE,
1148 ContentSuggestionsManager.class,
1149 new CachedServiceFetcher<ContentSuggestionsManager>() {
1150 @Override
1151 public ContentSuggestionsManager createService(ContextImpl ctx) {
1152 // No throw as this is an optional service
1153 IBinder b = ServiceManager.getService(
1154 Context.CONTENT_SUGGESTIONS_SERVICE);
1155 IContentSuggestionsManager service =
1156 IContentSuggestionsManager.Stub.asInterface(b);
Zak Cohen4834e9f2019-03-08 12:59:01 -08001157 return new ContentSuggestionsManager(ctx.getUserId(), service);
Winson Chung3fb0f252019-01-08 17:41:55 -08001158 }
1159 });
1160
Zak Cohen56345f42017-01-26 13:54:28 -08001161 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
1162 @Override
1163 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1164 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
1165 return new VrManager(IVrManager.Stub.asInterface(b));
1166 }
1167 });
Neil Fuller328532a2017-03-16 18:32:21 +00001168
1169 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
1170 new CachedServiceFetcher<RulesManager>() {
1171 @Override
1172 public RulesManager createService(ContextImpl ctx) {
1173 return new RulesManager(ctx.getOuterContext());
1174 }});
Tony Mak1b708e62017-10-12 10:59:11 +01001175
1176 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
1177 new CachedServiceFetcher<CrossProfileApps>() {
1178 @Override
1179 public CrossProfileApps createService(ContextImpl ctx)
1180 throws ServiceNotFoundException {
1181 IBinder b = ServiceManager.getServiceOrThrow(
1182 Context.CROSS_PROFILE_APPS_SERVICE);
1183 return new CrossProfileApps(ctx.getOuterContext(),
1184 ICrossProfileApps.Stub.asInterface(b));
1185 }
1186 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -04001187
1188 registerService(Context.SLICE_SERVICE, SliceManager.class,
1189 new CachedServiceFetcher<SliceManager>() {
1190 @Override
1191 public SliceManager createService(ContextImpl ctx)
1192 throws ServiceNotFoundException {
1193 return new SliceManager(ctx.getOuterContext(),
1194 ctx.mMainThread.getHandler());
1195 }
1196 });
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001197
Neil Fullerfeeee682018-05-30 14:35:24 +01001198 registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1199 new CachedServiceFetcher<TimeDetector>() {
1200 @Override
1201 public TimeDetector createService(ContextImpl ctx)
1202 throws ServiceNotFoundException {
Neil Fullerad6eb2b2020-01-28 10:55:15 +00001203 return new TimeDetectorImpl();
Neil Fullerfeeee682018-05-30 14:35:24 +01001204 }});
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001205
Neil Fuller3e3b5402019-11-07 15:35:05 +00001206 registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class,
1207 new CachedServiceFetcher<TimeZoneDetector>() {
1208 @Override
1209 public TimeZoneDetector createService(ContextImpl ctx)
1210 throws ServiceNotFoundException {
Neil Fullerad6eb2b2020-01-28 10:55:15 +00001211 return new TimeZoneDetectorImpl();
Neil Fuller3e3b5402019-11-07 15:35:05 +00001212 }});
1213
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001214 registerService(Context.PERMISSION_SERVICE, PermissionManager.class,
1215 new CachedServiceFetcher<PermissionManager>() {
1216 @Override
Peter Wang56dec3f2019-10-25 11:24:33 -07001217 public PermissionManager createService(ContextImpl ctx)
1218 throws ServiceNotFoundException {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001219 IPackageManager packageManager = AppGlobals.getPackageManager();
1220 return new PermissionManager(ctx.getOuterContext(), packageManager);
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001221 }});
Hai Zhang4ef21d02018-11-09 14:43:51 -08001222
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001223 registerService(Context.PERMISSION_CONTROLLER_SERVICE, PermissionControllerManager.class,
1224 new CachedServiceFetcher<PermissionControllerManager>() {
1225 @Override
1226 public PermissionControllerManager createService(ContextImpl ctx) {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001227 return new PermissionControllerManager(ctx.getOuterContext(),
1228 ctx.getMainThreadHandler());
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001229 }});
1230
Hai Zhang4ef21d02018-11-09 14:43:51 -08001231 registerService(Context.ROLE_SERVICE, RoleManager.class,
1232 new CachedServiceFetcher<RoleManager>() {
1233 @Override
1234 public RoleManager createService(ContextImpl ctx)
1235 throws ServiceNotFoundException {
1236 return new RoleManager(ctx.getOuterContext());
1237 }});
Richard Uhlerb29f1452018-09-12 16:38:15 +01001238
Hai Zhanga4959e52019-03-06 12:21:07 -08001239 registerService(Context.ROLE_CONTROLLER_SERVICE, RoleControllerManager.class,
1240 new CachedServiceFetcher<RoleControllerManager>() {
1241 @Override
1242 public RoleControllerManager createService(ContextImpl ctx)
1243 throws ServiceNotFoundException {
1244 return new RoleControllerManager(ctx.getOuterContext());
1245 }});
1246
Richard Uhlerb29f1452018-09-12 16:38:15 +01001247 registerService(Context.ROLLBACK_SERVICE, RollbackManager.class,
1248 new CachedServiceFetcher<RollbackManager>() {
1249 @Override
1250 public RollbackManager createService(ContextImpl ctx)
1251 throws ServiceNotFoundException {
1252 IBinder b = ServiceManager.getServiceOrThrow(Context.ROLLBACK_SERVICE);
1253 return new RollbackManager(ctx.getOuterContext(),
1254 IRollbackManager.Stub.asInterface(b));
1255 }});
Howard Chen0a947642019-01-07 14:10:44 +08001256
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001257 registerService(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class,
1258 new CachedServiceFetcher<DynamicSystemManager>() {
Howard Chen0a947642019-01-07 14:10:44 +08001259 @Override
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001260 public DynamicSystemManager createService(ContextImpl ctx)
Howard Chen0a947642019-01-07 14:10:44 +08001261 throws ServiceNotFoundException {
1262 IBinder b = ServiceManager.getServiceOrThrow(
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001263 Context.DYNAMIC_SYSTEM_SERVICE);
1264 return new DynamicSystemManager(
1265 IDynamicSystemService.Stub.asInterface(b));
Howard Chen0a947642019-01-07 14:10:44 +08001266 }});
Victor Hsieh20fe1f62019-09-30 13:36:21 -07001267
Roshan Pius848513e2019-10-11 13:44:00 -07001268 registerService(Context.BATTERY_STATS_SERVICE, BatteryStatsManager.class,
1269 new CachedServiceFetcher<BatteryStatsManager>() {
1270 @Override
1271 public BatteryStatsManager createService(ContextImpl ctx)
1272 throws ServiceNotFoundException {
1273 IBinder b = ServiceManager.getServiceOrThrow(
1274 Context.BATTERY_STATS_SERVICE);
1275 return new BatteryStatsManager(
1276 IBatteryStats.Stub.asInterface(b));
1277 }});
Songchun Fan6cde9d92019-12-10 10:23:54 -08001278 registerService(Context.DATA_LOADER_MANAGER_SERVICE, DataLoaderManager.class,
1279 new CachedServiceFetcher<DataLoaderManager>() {
1280 @Override
1281 public DataLoaderManager createService(ContextImpl ctx)
1282 throws ServiceNotFoundException {
1283 IBinder b = ServiceManager.getServiceOrThrow(
1284 Context.DATA_LOADER_MANAGER_SERVICE);
1285 return new DataLoaderManager(IDataLoaderManager.Stub.asInterface(b));
1286 }});
Ivailo Karamanolev090d02c2020-01-09 17:02:49 +01001287 registerService(Context.LIGHTS_SERVICE, LightsManager.class,
1288 new CachedServiceFetcher<LightsManager>() {
1289 @Override
1290 public LightsManager createService(ContextImpl ctx)
1291 throws ServiceNotFoundException {
1292 return new LightsManager(ctx);
1293 }});
Songchun Fan43f18ea2019-12-11 17:54:38 -08001294 registerService(Context.INCREMENTAL_SERVICE, IncrementalManager.class,
1295 new CachedServiceFetcher<IncrementalManager>() {
1296 @Override
1297 public IncrementalManager createService(ContextImpl ctx) {
1298 IBinder b = ServiceManager.getService(Context.INCREMENTAL_SERVICE);
1299 if (b == null) {
1300 return null;
1301 }
1302 return new IncrementalManager(
Alex Buynytskyy716588d2020-02-04 13:42:59 -08001303 IIncrementalService.Stub.asInterface(b));
Songchun Fan43f18ea2019-12-11 17:54:38 -08001304 }});
Victor Hsieh20fe1f62019-09-30 13:36:21 -07001305
1306 registerService(Context.FILE_INTEGRITY_SERVICE, FileIntegrityManager.class,
1307 new CachedServiceFetcher<FileIntegrityManager>() {
1308 @Override
1309 public FileIntegrityManager createService(ContextImpl ctx)
1310 throws ServiceNotFoundException {
1311 IBinder b = ServiceManager.getServiceOrThrow(
1312 Context.FILE_INTEGRITY_SERVICE);
Victor Hsiehb07f4852020-03-26 12:33:58 -07001313 return new FileIntegrityManager(ctx.getOuterContext(),
Victor Hsieh20fe1f62019-09-30 13:36:21 -07001314 IFileIntegrityService.Stub.asInterface(b));
1315 }});
Felipe Lemeecb08be2018-11-27 15:48:47 -08001316 //CHECKSTYLE:ON IndentationCheck
Song Pan6e3677c2019-10-29 14:19:26 +00001317 registerService(Context.APP_INTEGRITY_SERVICE, AppIntegrityManager.class,
1318 new CachedServiceFetcher<AppIntegrityManager>() {
1319 @Override
1320 public AppIntegrityManager createService(ContextImpl ctx)
1321 throws ServiceNotFoundException {
1322 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_INTEGRITY_SERVICE);
1323 return new AppIntegrityManager(IAppIntegrityManager.Stub.asInterface(b));
1324 }});
Galia Peycheva641e2e62020-02-18 00:47:05 +01001325 registerService(Context.DREAM_SERVICE, DreamManager.class,
1326 new CachedServiceFetcher<DreamManager>() {
1327 @Override
1328 public DreamManager createService(ContextImpl ctx)
1329 throws ServiceNotFoundException {
1330 return new DreamManager(ctx);
1331 }});
Makoto Onuki54c4e032019-10-30 12:05:20 -07001332
Makoto Onuki05b14f52019-11-05 12:36:07 -08001333 sInitializing = true;
1334 try {
1335 // Note: the following functions need to be @SystemApis, once they become mainline
1336 // modules.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001337 JobSchedulerFrameworkInitializer.registerServiceWrappers();
1338 BlobStoreManagerFrameworkInitializer.initialize();
Jayachandran C58fd3eb2019-11-20 19:01:56 -08001339 TelephonyFrameworkInitializer.registerServiceWrappers();
David Su409f3712019-10-24 11:18:41 -07001340 WifiFrameworkInitializer.registerServiceWrappers();
Jeffrey Huangf58800b2020-01-23 13:22:10 -08001341 StatsFrameworkInitializer.registerServiceWrappers();
Makoto Onuki05b14f52019-11-05 12:36:07 -08001342 } finally {
1343 // If any of the above code throws, we're in a pretty bad shape and the process
1344 // will likely crash, but we'll reset it just in case there's an exception handler...
1345 sInitializing = false;
1346 }
Jeff Brown6e539312015-02-24 18:53:21 -08001347 }
1348
Makoto Onuki05b14f52019-11-05 12:36:07 -08001349 /** Throws {@link IllegalStateException} if not during a static initialization. */
1350 private static void ensureInitializing(String methodName) {
1351 Preconditions.checkState(sInitializing, "Internal error: " + methodName
1352 + " can only be called during class initialization.");
1353 }
Jeff Brown6e539312015-02-24 18:53:21 -08001354 /**
1355 * Creates an array which is used to cache per-Context service instances.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001356 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001357 */
1358 public static Object[] createServiceCache() {
1359 return new Object[sServiceCacheSize];
1360 }
1361
1362 /**
1363 * Gets a system service from a given context.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001364 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001365 */
1366 public static Object getSystemService(ContextImpl ctx, String name) {
Zhuoyao Zhang3495b812020-02-23 05:54:41 +00001367 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1368 return fetcher != null ? fetcher.getService(ctx) : null;
Jeff Brown6e539312015-02-24 18:53:21 -08001369 }
1370
1371 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001372 * Gets the name of the system-level service that is represented by the specified class.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001373 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001374 */
1375 public static String getSystemServiceName(Class<?> serviceClass) {
Zhuoyao Zhang3495b812020-02-23 05:54:41 +00001376 return SYSTEM_SERVICE_NAMES.get(serviceClass);
Jeff Brown6e539312015-02-24 18:53:21 -08001377 }
1378
1379 /**
1380 * Statically registers a system service with the context.
1381 * This method must be called during static initialization only.
1382 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001383 private static <T> void registerService(@NonNull String serviceName,
1384 @NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) {
Jeff Brown6e539312015-02-24 18:53:21 -08001385 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1386 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
Charles Chen3d9fc0f2020-03-11 10:19:04 +08001387 SYSTEM_SERVICE_CLASS_NAMES.put(serviceName, serviceClass.getSimpleName());
1388 }
1389
1390 /**
1391 * Returns system service class name by system service name. This method is mostly an inverse of
1392 * {@link #getSystemServiceName(Class)}
1393 *
1394 * @return system service class name. {@code null} if service name is invalid.
1395 * @hide
1396 */
1397 @Nullable
1398 public static String getSystemServiceClassName(@NonNull String name) {
1399 return SYSTEM_SERVICE_CLASS_NAMES.get(name);
Jeff Brown6e539312015-02-24 18:53:21 -08001400 }
1401
1402 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001403 * Callback interface used as a parameter to {@link #registerStaticService(
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001404 * String, Class, StaticServiceProducerWithoutBinder)}, which generates a service wrapper
1405 * instance that's not tied to any context and does not take a service binder object in the
1406 * constructor.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001407 *
1408 * @param <TServiceClass> type of the service wrapper class.
Makoto Onuki792de8d2019-07-16 16:19:01 -07001409 *
1410 * @hide
1411 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001412 @SystemApi
1413 public interface StaticServiceProducerWithoutBinder<TServiceClass> {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001414 /**
1415 * Return a new service wrapper of type {@code TServiceClass}.
1416 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001417 @NonNull
Makoto Onuki05b14f52019-11-05 12:36:07 -08001418 TServiceClass createService();
1419 }
1420
1421 /**
1422 * Callback interface used as a parameter to {@link #registerStaticService(
1423 * String, Class, StaticServiceProducerWithBinder)}, which generates a service wrapper instance
1424 * that's not tied to any context and takes a service binder object in the constructor.
1425 *
1426 * @param <TServiceClass> type of the service wrapper class.
1427 *
1428 * @hide
1429 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001430 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001431 public interface StaticServiceProducerWithBinder<TServiceClass> {
1432 /**
1433 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1434 * service binder object.
1435 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001436 @NonNull
1437 TServiceClass createService(@NonNull IBinder serviceBinder);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001438 }
1439
1440 /**
1441 * Callback interface used as a parameter to {@link #registerContextAwareService(
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001442 * String, Class, ContextAwareServiceProducerWithoutBinder)},
Makoto Onuki05b14f52019-11-05 12:36:07 -08001443 * which generates a service wrapper instance
1444 * that's tied to a specific context and does not take a service binder object in the
1445 * constructor.
1446 *
1447 * @param <TServiceClass> type of the service wrapper class.
1448 *
1449 * @hide
1450 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001451 @SystemApi
1452 public interface ContextAwareServiceProducerWithoutBinder<TServiceClass> {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001453 /**
1454 * Return a new service wrapper of type {@code TServiceClass} tied to a given
1455 * {@code context}.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001456 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001457 @NonNull
1458 //TODO Do we need to pass the "base context" too?
1459 TServiceClass createService(@NonNull Context context);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001460 }
1461
1462 /**
1463 * Callback interface used as a parameter to {@link #registerContextAwareService(
1464 * String, Class, ContextAwareServiceProducerWithBinder)},
1465 * which generates a service wrapper instance
1466 * that's tied to a specific context and takes a service binder object in the constructor.
1467 *
1468 * @param <TServiceClass> type of the service wrapper class.
1469 *
1470 * @hide
1471 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001472 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001473 public interface ContextAwareServiceProducerWithBinder<TServiceClass> {
1474 /**
1475 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1476 * service binder object that's tied to a given {@code context}.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001477 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001478 @NonNull
1479 //TODO Do we need to pass the "base context" too?
1480 TServiceClass createService(@NonNull Context context, @NonNull IBinder serviceBinder);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001481 }
1482
1483 /**
1484 * Used by apex modules to register a "service wrapper" that is not tied to any {@link Context}.
1485 *
1486 * <p>This can only be called from the methods called by the static initializer of
1487 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1488 *
1489 * @param serviceName the name of the binder object, such as
1490 * {@link Context#JOB_SCHEDULER_SERVICE}.
1491 * @param serviceWrapperClass the wrapper class, such as the class of
1492 * {@link android.app.job.JobScheduler}.
1493 * @param serviceProducer Callback that takes the service binder object with the name
1494 * {@code serviceName} and returns an actual service wrapper instance.
1495 *
1496 * @hide
1497 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001498 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001499 public static <TServiceClass> void registerStaticService(
1500 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1501 @NonNull StaticServiceProducerWithBinder<TServiceClass> serviceProducer) {
1502 ensureInitializing("registerStaticService");
1503 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001504 Objects.requireNonNull(serviceWrapperClass);
1505 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001506
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001507 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001508 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki792de8d2019-07-16 16:19:01 -07001509 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001510 public TServiceClass createService() throws ServiceNotFoundException {
1511 return serviceProducer.createService(
1512 ServiceManager.getServiceOrThrow(serviceName));
Makoto Onuki792de8d2019-07-16 16:19:01 -07001513 }});
1514 }
1515
1516 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001517 * Similar to {@link #registerStaticService(String, Class, StaticServiceProducerWithBinder)},
1518 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001519 *
1520 * @hide
1521 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001522 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001523 public static <TServiceClass> void registerStaticService(
1524 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001525 @NonNull StaticServiceProducerWithoutBinder<TServiceClass> serviceProducer) {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001526 ensureInitializing("registerStaticService");
1527 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001528 Objects.requireNonNull(serviceWrapperClass);
1529 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001530
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001531 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001532 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001533 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001534 public TServiceClass createService() {
1535 return serviceProducer.createService();
1536 }});
1537 }
1538
1539 /**
1540 * Used by apex modules to register a "service wrapper" that is tied to a specific
1541 * {@link Context}.
1542 *
1543 * <p>This can only be called from the methods called by the static initializer of
1544 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1545 *
1546 * @param serviceName the name of the binder object, such as
1547 * {@link Context#JOB_SCHEDULER_SERVICE}.
1548 * @param serviceWrapperClass the wrapper class, such as the class of
1549 * {@link android.app.job.JobScheduler}.
1550 * @param serviceProducer lambda that takes the service binder object with the name
1551 * {@code serviceName}, a {@link Context} and returns an actual service wrapper instance.
1552 *
1553 * @hide
1554 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001555 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001556 public static <TServiceClass> void registerContextAwareService(
1557 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1558 @NonNull ContextAwareServiceProducerWithBinder<TServiceClass> serviceProducer) {
1559 ensureInitializing("registerContextAwareService");
1560 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001561 Objects.requireNonNull(serviceWrapperClass);
1562 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001563
1564 registerService(serviceName, serviceWrapperClass,
1565 new CachedServiceFetcher<TServiceClass>() {
1566 @Override
1567 public TServiceClass createService(ContextImpl ctx)
1568 throws ServiceNotFoundException {
1569 return serviceProducer.createService(
1570 ctx.getOuterContext(),
1571 ServiceManager.getServiceOrThrow(serviceName));
1572 }});
1573 }
1574
1575
1576 /**
1577 * Similar to {@link #registerContextAwareService(String, Class,
1578 * ContextAwareServiceProducerWithBinder)},
1579 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
1580 *
1581 * @hide
1582 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001583 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001584 public static <TServiceClass> void registerContextAwareService(
1585 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001586 @NonNull ContextAwareServiceProducerWithoutBinder<TServiceClass> serviceProducer) {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001587 ensureInitializing("registerContextAwareService");
1588 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001589 Objects.requireNonNull(serviceWrapperClass);
1590 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001591
1592 registerService(serviceName, serviceWrapperClass,
1593 new CachedServiceFetcher<TServiceClass>() {
1594 @Override
1595 public TServiceClass createService(ContextImpl ctx) {
1596 return serviceProducer.createService(ctx.getOuterContext());
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001597 }});
1598 }
1599
1600 /**
Jeff Brown6e539312015-02-24 18:53:21 -08001601 * Base interface for classes that fetch services.
1602 * These objects must only be created during static initialization.
1603 */
1604 static abstract interface ServiceFetcher<T> {
1605 T getService(ContextImpl ctx);
1606 }
1607
1608 /**
1609 * Override this class when the system service constructor needs a
1610 * ContextImpl and should be cached and retained by that context.
1611 */
1612 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1613 private final int mCacheIndex;
1614
Makoto Onukid67070e2018-03-30 12:39:14 -07001615 CachedServiceFetcher() {
1616 // Note this class must be instantiated only by the static initializer of the
1617 // outer class (SystemServiceRegistry), which already does the synchronization,
1618 // so bare access to sServiceCacheSize is okay here.
Jeff Brown6e539312015-02-24 18:53:21 -08001619 mCacheIndex = sServiceCacheSize++;
1620 }
1621
1622 @Override
1623 @SuppressWarnings("unchecked")
1624 public final T getService(ContextImpl ctx) {
1625 final Object[] cache = ctx.mServiceCache;
Makoto Onukid67070e2018-03-30 12:39:14 -07001626 final int[] gates = ctx.mServiceInitializationStateArray;
Makoto Onukif896f122018-01-25 09:50:24 -08001627
Makoto Onukid67070e2018-03-30 12:39:14 -07001628 for (;;) {
1629 boolean doInitialize = false;
1630 synchronized (cache) {
1631 // Return it if we already have a cached instance.
1632 T service = (T) cache[mCacheIndex];
1633 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1634 return service;
1635 }
Makoto Onukif896f122018-01-25 09:50:24 -08001636
Makoto Onukid67070e2018-03-30 12:39:14 -07001637 // If we get here, there's no cached instance.
Makoto Onukif896f122018-01-25 09:50:24 -08001638
Makoto Onukid67070e2018-03-30 12:39:14 -07001639 // Grr... if gate is STATE_READY, then this means we initialized the service
1640 // once but someone cleared it.
1641 // We start over from STATE_UNINITIALIZED.
1642 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1643 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1644 }
1645
1646 // It's possible for multiple threads to get here at the same time, so
1647 // use the "gate" to make sure only the first thread will call createService().
1648
1649 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1650 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1651 doInitialize = true;
1652 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1653 }
Makoto Onukif896f122018-01-25 09:50:24 -08001654 }
1655
Makoto Onukid67070e2018-03-30 12:39:14 -07001656 if (doInitialize) {
1657 // Only the first thread gets here.
Makoto Onukif896f122018-01-25 09:50:24 -08001658
Makoto Onukid67070e2018-03-30 12:39:14 -07001659 T service = null;
1660 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001661 try {
Makoto Onukid67070e2018-03-30 12:39:14 -07001662 // This thread is the first one to get here. Instantiate the service
1663 // *without* the cache lock held.
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001664 service = createService(ctx);
Makoto Onukid67070e2018-03-30 12:39:14 -07001665 newState = ContextImpl.STATE_READY;
Makoto Onukif896f122018-01-25 09:50:24 -08001666
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001667 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001668 onServiceNotFound(e);
Makoto Onukif896f122018-01-25 09:50:24 -08001669
Makoto Onukid67070e2018-03-30 12:39:14 -07001670 } finally {
1671 synchronized (cache) {
1672 cache[mCacheIndex] = service;
1673 gates[mCacheIndex] = newState;
1674 cache.notifyAll();
1675 }
1676 }
1677 return service;
1678 }
1679 // The other threads will wait for the first thread to call notifyAll(),
1680 // and go back to the top and retry.
1681 synchronized (cache) {
1682 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1683 try {
1684 cache.wait();
1685 } catch (InterruptedException e) {
Zhuoyao Zhang3495b812020-02-23 05:54:41 +00001686 Log.w(TAG, "getService() interrupted");
Makoto Onukid67070e2018-03-30 12:39:14 -07001687 Thread.currentThread().interrupt();
1688 return null;
1689 }
Makoto Onukif896f122018-01-25 09:50:24 -08001690 }
1691 }
Makoto Onukif896f122018-01-25 09:50:24 -08001692 }
Jeff Brown6e539312015-02-24 18:53:21 -08001693 }
1694
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001695 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001696 }
1697
1698 /**
1699 * Override this class when the system service does not need a ContextImpl
1700 * and should be cached and retained process-wide.
1701 */
1702 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1703 private T mCachedInstance;
1704
1705 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001706 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001707 synchronized (StaticServiceFetcher.this) {
1708 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001709 try {
1710 mCachedInstance = createService();
1711 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001712 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001713 }
Jeff Brown6e539312015-02-24 18:53:21 -08001714 }
1715 return mCachedInstance;
1716 }
1717 }
1718
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001719 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001720 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001721
1722 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001723 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1724 * creating the service for the first time, passes it the application context of the creating
1725 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001726 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001727 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1728 * case where multiple application components each have their own ConnectivityManager object.
1729 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001730 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001731 private T mCachedInstance;
1732
1733 @Override
1734 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001735 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001736 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001737 Context appContext = ctx.getApplicationContext();
1738 // If the application context is null, we're either in the system process or
1739 // it's the application context very early in app initialization. In both these
1740 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1741 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001742 try {
1743 mCachedInstance = createService(appContext != null ? appContext : ctx);
1744 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001745 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001746 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001747 }
1748 return mCachedInstance;
1749 }
1750 }
1751
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001752 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001753 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001754
Makoto Onuki05b14f52019-11-05 12:36:07 -08001755 /** @hide */
Jeff Sharkey589f3092016-11-29 12:54:39 -07001756 public static void onServiceNotFound(ServiceNotFoundException e) {
1757 // We're mostly interested in tracking down long-lived core system
1758 // components that might stumble if they obtain bad references; just
1759 // emit a tidy log message for normal apps
1760 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1761 Log.wtf(TAG, e.getMessage(), e);
1762 } else {
1763 Log.w(TAG, e.getMessage());
1764 }
1765 }
Jeff Brown6e539312015-02-24 18:53:21 -08001766}