blob: 59c171923f2b8ab3246aedb84640ec4aa622d0a6 [file] [log] [blame]
Jeff Brown6e539312015-02-24 18:53:21 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Jeff Brown6e539312015-02-24 18:53:21 -080019import android.accounts.AccountManager;
20import android.accounts.IAccountManager;
Makoto Onuki05b14f52019-11-05 12:36:07 -080021import android.annotation.NonNull;
Makoto Onuki0ceb3e02019-11-18 10:37:10 -080022import android.annotation.SystemApi;
Makoto Onukid67070e2018-03-30 12:39:14 -070023import android.app.ContextImpl.ServiceInitializationState;
Jeff Brown6e539312015-02-24 18:53:21 -080024import android.app.admin.DevicePolicyManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060025import android.app.admin.IDevicePolicyManager;
Terry Wangecc0d1b2019-10-17 17:05:18 -070026import android.app.appsearch.AppSearchManagerFrameworkInitializer;
Sudheer Shankaf5b36962019-10-04 16:16:13 -070027import android.app.blob.BlobStoreManagerFrameworkInitializer;
Winson Chung3fb0f252019-01-08 17:41:55 -080028import android.app.contentsuggestions.ContentSuggestionsManager;
29import android.app.contentsuggestions.IContentSuggestionsManager;
Makoto Onuki54c4e032019-10-30 12:05:20 -070030import android.app.job.JobSchedulerFrameworkInitializer;
Sunny Goyal54e91342018-11-14 11:59:02 -080031import android.app.prediction.AppPredictionManager;
Hai Zhanga4959e52019-03-06 12:21:07 -080032import android.app.role.RoleControllerManager;
Hai Zhang4ef21d02018-11-09 14:43:51 -080033import android.app.role.RoleManager;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040034import android.app.slice.SliceManager;
Neil Fullerfeeee682018-05-30 14:35:24 +010035import android.app.timedetector.TimeDetector;
Neil 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;
Cody Kestingb125776002019-12-17 17:21:40 -0800112import android.net.ConnectivityDiagnosticsManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800113import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700114import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -0800115import android.net.EthernetManager;
116import android.net.IConnectivityManager;
117import android.net.IEthernetManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800118import android.net.IIpSecService;
Jeff Brown6e539312015-02-24 18:53:21 -0800119import android.net.INetworkPolicyManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800120import android.net.ITestNetworkManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800121import android.net.IpSecManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800122import android.net.NetworkPolicyManager;
123import android.net.NetworkScoreManager;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100124import android.net.NetworkWatchlistManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800125import android.net.TestNetworkManager;
markchienae8aa642019-12-16 20:15:20 +0800126import android.net.TetheringManager;
Benedict Wong50b44432019-11-01 16:46:28 -0700127import android.net.VpnManager;
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700128import android.net.lowpan.ILowpanManager;
129import android.net.lowpan.LowpanManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700130import android.net.nsd.INsdManager;
131import android.net.nsd.NsdManager;
David Su409f3712019-10-24 11:18:41 -0700132import android.net.wifi.WifiFrameworkInitializer;
Etan Cohen5abc6d92019-12-18 10:54:28 -0800133import android.net.wifi.wificond.WifiCondManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800134import android.nfc.NfcManager;
135import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600136import android.os.BatteryStats;
Roshan Pius848513e2019-10-11 13:44:00 -0700137import android.os.BatteryStatsManager;
Nandana Duttd11850c2018-12-12 17:26:57 +0000138import android.os.BugreportManager;
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600139import android.os.Build;
Jeff Brown6e539312015-02-24 18:53:21 -0800140import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100141import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600142import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -0800143import android.os.IBinder;
Nandana Duttd11850c2018-12-12 17:26:57 +0000144import android.os.IDumpstate;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100145import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800146import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800147import android.os.IRecoverySystem;
Tao Bao07342dc2017-01-24 15:08:21 -0800148import android.os.ISystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800149import android.os.IUserManager;
Joe Onorato1754d742016-11-21 17:51:35 -0800150import android.os.IncidentManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800151import android.os.PowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800152import android.os.RecoverySystem;
Benedict Wong99a48412018-11-09 14:45:34 -0800153import android.os.RemoteException;
Jeff Brown6e539312015-02-24 18:53:21 -0800154import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600155import android.os.ServiceManager.ServiceNotFoundException;
Jeffrey Huangf58800b2020-01-23 13:22:10 -0800156import android.os.StatsFrameworkInitializer;
Hall Liub38ad5f2019-12-23 18:11:10 -0800157import android.os.SystemConfigManager;
Tao Bao07342dc2017-01-24 15:08:21 -0800158import android.os.SystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800159import android.os.SystemVibrator;
160import android.os.UserHandle;
161import android.os.UserManager;
162import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800163import android.os.health.SystemHealthManager;
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +0800164import android.os.image.DynamicSystemManager;
165import android.os.image.IDynamicSystemService;
Songchun Fan43f18ea2019-12-11 17:54:38 -0800166import android.os.incremental.IIncrementalManagerNative;
167import android.os.incremental.IncrementalManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800168import android.os.storage.StorageManager;
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800169import android.permission.PermissionControllerManager;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700170import android.permission.PermissionManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800171import android.print.IPrintManager;
172import android.print.PrintManager;
Victor Hsieh20fe1f62019-09-30 13:36:21 -0700173import android.security.FileIntegrityManager;
174import android.security.IFileIntegrityService;
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000175import android.service.oemlock.IOemLockService;
176import android.service.oemlock.OemLockManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800177import android.service.persistentdata.IPersistentDataBlockService;
178import android.service.persistentdata.PersistentDataBlockManager;
Zak Cohen56345f42017-01-26 13:54:28 -0800179import android.service.vr.IVrManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800180import android.telecom.TelecomManager;
Sarah Chin4affb512020-01-10 16:09:11 -0800181import android.telephony.MmsManager;
Jayachandran C58fd3eb2019-11-20 19:01:56 -0800182import android.telephony.TelephonyFrameworkInitializer;
Makoto Onuki54c4e032019-10-30 12:05:20 -0700183import android.telephony.TelephonyRegistryManager;
Robert Benea1901a5b2018-05-15 16:36:07 -0700184import android.util.ArrayMap;
Jeff Brown6e539312015-02-24 18:53:21 -0800185import android.util.Log;
186import android.view.ContextThemeWrapper;
187import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800188import android.view.WindowManager;
189import android.view.WindowManagerImpl;
190import android.view.accessibility.AccessibilityManager;
191import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100192import android.view.autofill.AutofillManager;
193import android.view.autofill.IAutoFillManager;
Felipe Leme749b8892018-12-03 16:30:30 -0800194import android.view.contentcapture.ContentCaptureManager;
195import android.view.contentcapture.IContentCaptureManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800196import android.view.inputmethod.InputMethodManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000197import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800198import android.view.textservice.TextServicesManager;
199
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600200import com.android.internal.app.IAppOpsService;
201import com.android.internal.app.IBatteryStats;
202import com.android.internal.app.ISoundTriggerService;
203import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100204import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600205import com.android.internal.os.IDropBoxManagerService;
206import com.android.internal.policy.PhoneLayoutInflater;
Makoto Onuki05b14f52019-11-05 12:36:07 -0800207import com.android.internal.util.Preconditions;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600208
Robert Benea1901a5b2018-05-15 16:36:07 -0700209import java.util.Map;
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +0000210import java.util.Objects;
Jeff Brown6e539312015-02-24 18:53:21 -0800211
212/**
213 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
214 * Used by {@link ContextImpl}.
Makoto Onuki792de8d2019-07-16 16:19:01 -0700215 *
216 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -0800217 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -0800218@SystemApi
Makoto Onuki792de8d2019-07-16 16:19:01 -0700219public final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600220 private static final String TAG = "SystemServiceRegistry";
221
Jeff Brown6e539312015-02-24 18:53:21 -0800222 // Service registry information.
223 // This information is never changed once static initialization has completed.
Robert Benea1901a5b2018-05-15 16:36:07 -0700224 private static final Map<Class<?>, String> SYSTEM_SERVICE_NAMES =
225 new ArrayMap<Class<?>, String>();
226 private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
227 new ArrayMap<String, ServiceFetcher<?>>();
Jeff Brown6e539312015-02-24 18:53:21 -0800228 private static int sServiceCacheSize;
229
Makoto Onuki05b14f52019-11-05 12:36:07 -0800230 private static volatile boolean sInitializing;
231
Jeff Brown6e539312015-02-24 18:53:21 -0800232 // Not instantiable.
233 private SystemServiceRegistry() { }
234
235 static {
Felipe Lemeecb08be2018-11-27 15:48:47 -0800236 //CHECKSTYLE:OFF IndentationCheck
Jeff Brown6e539312015-02-24 18:53:21 -0800237 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
238 new CachedServiceFetcher<AccessibilityManager>() {
239 @Override
240 public AccessibilityManager createService(ContextImpl ctx) {
241 return AccessibilityManager.getInstance(ctx);
242 }});
243
244 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
245 new CachedServiceFetcher<CaptioningManager>() {
246 @Override
247 public CaptioningManager createService(ContextImpl ctx) {
248 return new CaptioningManager(ctx);
249 }});
250
251 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
252 new CachedServiceFetcher<AccountManager>() {
253 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600254 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
255 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800256 IAccountManager service = IAccountManager.Stub.asInterface(b);
257 return new AccountManager(ctx, service);
258 }});
259
260 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
261 new CachedServiceFetcher<ActivityManager>() {
262 @Override
263 public ActivityManager createService(ContextImpl ctx) {
264 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
265 }});
266
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700267 registerService(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class,
268 new CachedServiceFetcher<ActivityTaskManager>() {
269 @Override
270 public ActivityTaskManager createService(ContextImpl ctx) {
271 return new ActivityTaskManager(
272 ctx.getOuterContext(), ctx.mMainThread.getHandler());
273 }});
274
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700275 registerService(Context.URI_GRANTS_SERVICE, UriGrantsManager.class,
276 new CachedServiceFetcher<UriGrantsManager>() {
277 @Override
278 public UriGrantsManager createService(ContextImpl ctx) {
279 return new UriGrantsManager(
280 ctx.getOuterContext(), ctx.mMainThread.getHandler());
281 }});
282
Jeff Brown6e539312015-02-24 18:53:21 -0800283 registerService(Context.ALARM_SERVICE, AlarmManager.class,
284 new CachedServiceFetcher<AlarmManager>() {
285 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600286 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
287 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800288 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
289 return new AlarmManager(service, ctx);
290 }});
291
292 registerService(Context.AUDIO_SERVICE, AudioManager.class,
293 new CachedServiceFetcher<AudioManager>() {
294 @Override
295 public AudioManager createService(ContextImpl ctx) {
296 return new AudioManager(ctx);
297 }});
298
299 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
300 new CachedServiceFetcher<MediaRouter>() {
301 @Override
302 public MediaRouter createService(ContextImpl ctx) {
303 return new MediaRouter(ctx);
304 }});
305
306 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
307 new CachedServiceFetcher<BluetoothManager>() {
308 @Override
309 public BluetoothManager createService(ContextImpl ctx) {
310 return new BluetoothManager(ctx);
311 }});
312
313 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
314 new StaticServiceFetcher<HdmiControlManager>() {
315 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600316 public HdmiControlManager createService() throws ServiceNotFoundException {
317 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800318 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
319 }});
320
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000321 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000322 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000323 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000324 public TextClassificationManager createService(ContextImpl ctx) {
325 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000326 }});
327
Jeff Brown6e539312015-02-24 18:53:21 -0800328 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
329 new CachedServiceFetcher<ClipboardManager>() {
330 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600331 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800332 return new ClipboardManager(ctx.getOuterContext(),
333 ctx.mMainThread.getHandler());
334 }});
335
336 // The clipboard service moved to a new package. If someone asks for the old
337 // interface by class then we want to redirect over to the new interface instead
338 // (which extends it).
339 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
340
341 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900342 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800343 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600344 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
345 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900346 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
347 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800348 }});
349
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900350 registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() {
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900351 @Override
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900352 public IBinder createService() throws ServiceNotFoundException {
353 return ServiceManager.getServiceOrThrow(Context.NETD_SERVICE);
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900354 }
355 });
356
Remi NGUYEN VAN05f7b5c2020-01-07 11:44:14 +0900357 registerService(Context.NETWORK_STACK_SERVICE, IBinder.class,
358 new StaticServiceFetcher<IBinder>() {
359 @Override
360 public IBinder createService() {
361 return ServiceManager.getService(Context.NETWORK_STACK_SERVICE);
362 }
363 });
364
markchienae8aa642019-12-16 20:15:20 +0800365 registerService(Context.TETHERING_SERVICE, TetheringManager.class,
366 new CachedServiceFetcher<TetheringManager>() {
367 @Override
368 public TetheringManager createService(ContextImpl ctx) throws ServiceNotFoundException {
369 IBinder b = ServiceManager.getService(Context.TETHERING_SERVICE);
370 if (b == null) return null;
371
372 return new TetheringManager(ctx, b);
373 }});
374
375
Nathan Harold28084d82017-03-01 18:55:06 -0800376 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Nathan Harold592dadb2018-03-15 18:06:06 -0700377 new CachedServiceFetcher<IpSecManager>() {
Nathan Harold28084d82017-03-01 18:55:06 -0800378 @Override
Nathan Harold592dadb2018-03-15 18:06:06 -0700379 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nathan Harold28084d82017-03-01 18:55:06 -0800380 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
381 IIpSecService service = IIpSecService.Stub.asInterface(b);
Nathan Harold592dadb2018-03-15 18:06:06 -0700382 return new IpSecManager(ctx, service);
Nathan Harold28084d82017-03-01 18:55:06 -0800383 }});
384
Benedict Wong50b44432019-11-01 16:46:28 -0700385 registerService(Context.VPN_MANAGEMENT_SERVICE, VpnManager.class,
386 new CachedServiceFetcher<VpnManager>() {
387 @Override
388 public VpnManager createService(ContextImpl ctx) throws ServiceNotFoundException {
389 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
390 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
391 return new VpnManager(ctx, service);
392 }});
393
Cody Kestingb125776002019-12-17 17:21:40 -0800394 registerService(Context.CONNECTIVITY_DIAGNOSTICS_SERVICE,
395 ConnectivityDiagnosticsManager.class,
396 new CachedServiceFetcher<ConnectivityDiagnosticsManager>() {
397 @Override
398 public ConnectivityDiagnosticsManager createService(ContextImpl ctx)
399 throws ServiceNotFoundException {
400 // ConnectivityDiagnosticsManager is backed by ConnectivityService
401 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
402 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
403 return new ConnectivityDiagnosticsManager(ctx, service);
404 }});
405
Benedict Wong99a48412018-11-09 14:45:34 -0800406 registerService(
407 Context.TEST_NETWORK_SERVICE,
408 TestNetworkManager.class,
409 new StaticApplicationContextServiceFetcher<TestNetworkManager>() {
410 @Override
411 public TestNetworkManager createService(Context context)
412 throws ServiceNotFoundException {
413 IBinder csBinder =
414 ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
415 IConnectivityManager csMgr =
416 IConnectivityManager.Stub.asInterface(csBinder);
417
418 final IBinder tnBinder;
419 try {
420 tnBinder = csMgr.startOrGetTestNetworkService();
421 } catch (RemoteException e) {
422 throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE);
423 }
424 ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder);
Benedict Wongb4e98f32019-03-12 21:54:16 -0700425 return new TestNetworkManager(tnMgr);
Benedict Wong99a48412018-11-09 14:45:34 -0800426 }
427 });
428
Jeff Brown6e539312015-02-24 18:53:21 -0800429 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
430 new StaticServiceFetcher<CountryDetector>() {
431 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600432 public CountryDetector createService() throws ServiceNotFoundException {
433 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800434 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
435 }});
436
437 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
438 new CachedServiceFetcher<DevicePolicyManager>() {
439 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600440 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
441 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
442 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800443 }});
444
445 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
446 new CachedServiceFetcher<DownloadManager>() {
447 @Override
448 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700449 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800450 }});
451
452 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700453 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800454 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700455 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600456 IBatteryStats stats = IBatteryStats.Stub.asInterface(
457 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
458 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
459 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700460 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800461 }});
462
463 registerService(Context.NFC_SERVICE, NfcManager.class,
464 new CachedServiceFetcher<NfcManager>() {
465 @Override
466 public NfcManager createService(ContextImpl ctx) {
467 return new NfcManager(ctx);
468 }});
469
470 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700471 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800472 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600473 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
474 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800475 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700476 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800477 }});
478
479 registerService(Context.INPUT_SERVICE, InputManager.class,
480 new StaticServiceFetcher<InputManager>() {
481 @Override
482 public InputManager createService() {
483 return InputManager.getInstance();
484 }});
485
486 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
487 new CachedServiceFetcher<DisplayManager>() {
488 @Override
489 public DisplayManager createService(ContextImpl ctx) {
490 return new DisplayManager(ctx.getOuterContext());
491 }});
492
Christine Franks39b03112018-07-03 14:46:07 -0700493 registerService(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class,
494 new CachedServiceFetcher<ColorDisplayManager>() {
495 @Override
496 public ColorDisplayManager createService(ContextImpl ctx) {
497 return new ColorDisplayManager();
498 }
499 });
500
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800501 // InputMethodManager has its own cache strategy based on display id to support apps that
502 // still assume InputMethodManager is a per-process singleton and it's safe to directly
503 // access internal fields via reflection. Hence directly use ServiceFetcher instead of
504 // StaticServiceFetcher/CachedServiceFetcher.
Jeff Brown6e539312015-02-24 18:53:21 -0800505 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800506 new ServiceFetcher<InputMethodManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800507 @Override
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800508 public InputMethodManager getService(ContextImpl ctx) {
509 return InputMethodManager.forContext(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800510 }});
511
512 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800513 new CachedServiceFetcher<TextServicesManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800514 @Override
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800515 public TextServicesManager createService(ContextImpl ctx)
516 throws ServiceNotFoundException {
517 return TextServicesManager.createInstance(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800518 }});
519
520 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800521 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800522 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800523 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800524 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800525 }});
526
527 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
528 new CachedServiceFetcher<LayoutInflater>() {
529 @Override
530 public LayoutInflater createService(ContextImpl ctx) {
531 return new PhoneLayoutInflater(ctx.getOuterContext());
532 }});
533
534 registerService(Context.LOCATION_SERVICE, LocationManager.class,
535 new CachedServiceFetcher<LocationManager>() {
536 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600537 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
538 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800539 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
540 }});
541
542 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700543 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800544 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600545 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700546 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600547 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800548 }});
549
550 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
551 new CachedServiceFetcher<NotificationManager>() {
552 @Override
553 public NotificationManager createService(ContextImpl ctx) {
554 final Context outerContext = ctx.getOuterContext();
555 return new NotificationManager(
556 new ContextThemeWrapper(outerContext,
557 Resources.selectSystemTheme(0,
558 outerContext.getApplicationInfo().targetSdkVersion,
559 com.android.internal.R.style.Theme_Dialog,
560 com.android.internal.R.style.Theme_Holo_Dialog,
561 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
562 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
563 ctx.mMainThread.getHandler());
564 }});
565
566 registerService(Context.NSD_SERVICE, NsdManager.class,
567 new CachedServiceFetcher<NsdManager>() {
568 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600569 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
570 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800571 INsdManager service = INsdManager.Stub.asInterface(b);
572 return new NsdManager(ctx.getOuterContext(), service);
573 }});
574
575 registerService(Context.POWER_SERVICE, PowerManager.class,
576 new CachedServiceFetcher<PowerManager>() {
577 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600578 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
579 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800580 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800581 return new PowerManager(ctx.getOuterContext(),
582 service, ctx.mMainThread.getHandler());
583 }});
584
Tao Baoe8a403d2015-12-31 07:44:55 -0800585 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
586 new CachedServiceFetcher<RecoverySystem>() {
587 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600588 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
589 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800590 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800591 return new RecoverySystem(service);
592 }});
593
Jeff Brown6e539312015-02-24 18:53:21 -0800594 registerService(Context.SEARCH_SERVICE, SearchManager.class,
595 new CachedServiceFetcher<SearchManager>() {
596 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600597 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800598 return new SearchManager(ctx.getOuterContext(),
599 ctx.mMainThread.getHandler());
600 }});
601
602 registerService(Context.SENSOR_SERVICE, SensorManager.class,
603 new CachedServiceFetcher<SensorManager>() {
604 @Override
605 public SensorManager createService(ContextImpl ctx) {
606 return new SystemSensorManager(ctx.getOuterContext(),
607 ctx.mMainThread.getHandler().getLooper());
608 }});
609
Chad Brubaker90f391f2018-10-19 10:26:19 -0700610 registerService(Context.SENSOR_PRIVACY_SERVICE, SensorPrivacyManager.class,
611 new CachedServiceFetcher<SensorPrivacyManager>() {
612 @Override
613 public SensorPrivacyManager createService(ContextImpl ctx) {
614 return SensorPrivacyManager.getInstance(ctx);
615 }});
616
Jeff Brown6e539312015-02-24 18:53:21 -0800617 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
618 new CachedServiceFetcher<StatusBarManager>() {
619 @Override
620 public StatusBarManager createService(ContextImpl ctx) {
621 return new StatusBarManager(ctx.getOuterContext());
622 }});
623
624 registerService(Context.STORAGE_SERVICE, StorageManager.class,
625 new CachedServiceFetcher<StorageManager>() {
626 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600627 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700628 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800629 }});
630
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700631 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
632 new CachedServiceFetcher<StorageStatsManager>() {
633 @Override
634 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
635 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
636 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
637 return new StorageStatsManager(ctx, service);
638 }});
639
Tao Bao07342dc2017-01-24 15:08:21 -0800640 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
641 new CachedServiceFetcher<SystemUpdateManager>() {
642 @Override
643 public SystemUpdateManager createService(ContextImpl ctx)
644 throws ServiceNotFoundException {
645 IBinder b = ServiceManager.getServiceOrThrow(
646 Context.SYSTEM_UPDATE_SERVICE);
647 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
648 return new SystemUpdateManager(service);
649 }});
650
Hall Liub38ad5f2019-12-23 18:11:10 -0800651 registerService(Context.SYSTEM_CONFIG_SERVICE, SystemConfigManager.class,
652 new CachedServiceFetcher<SystemConfigManager>() {
653 @Override
654 public SystemConfigManager createService(ContextImpl ctx) {
655 return new SystemConfigManager();
656 }});
657
Chen Xu288b71c2019-09-15 18:28:21 -0700658 registerService(Context.TELEPHONY_REGISTRY_SERVICE, TelephonyRegistryManager.class,
659 new CachedServiceFetcher<TelephonyRegistryManager>() {
660 @Override
661 public TelephonyRegistryManager createService(ContextImpl ctx) {
Chen Xu1f6cfa52019-10-13 17:30:32 -0700662 return new TelephonyRegistryManager(ctx);
Chen Xu288b71c2019-09-15 18:28:21 -0700663 }});
664
Jeff Brown6e539312015-02-24 18:53:21 -0800665 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
666 new CachedServiceFetcher<TelecomManager>() {
667 @Override
668 public TelecomManager createService(ContextImpl ctx) {
669 return new TelecomManager(ctx.getOuterContext());
670 }});
671
Sarah Chin4affb512020-01-10 16:09:11 -0800672 registerService(Context.MMS_SERVICE, MmsManager.class,
673 new CachedServiceFetcher<MmsManager>() {
674 @Override
675 public MmsManager createService(ContextImpl ctx) {
676 return new MmsManager(ctx.getOuterContext());
677 }});
678
Jeff Brown6e539312015-02-24 18:53:21 -0800679 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
680 new CachedServiceFetcher<UiModeManager>() {
681 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600682 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Tyler Gunndaef6682019-09-30 15:12:24 -0700683 return new UiModeManager(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800684 }});
685
686 registerService(Context.USB_SERVICE, UsbManager.class,
687 new CachedServiceFetcher<UsbManager>() {
688 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600689 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
690 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800691 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
692 }});
693
Kenny Rootf74bfde2018-01-18 15:42:48 -0800694 registerService(Context.ADB_SERVICE, AdbManager.class,
695 new CachedServiceFetcher<AdbManager>() {
696 @Override
697 public AdbManager createService(ContextImpl ctx)
698 throws ServiceNotFoundException {
699 IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE);
700 return new AdbManager(ctx, IAdbManager.Stub.asInterface(b));
701 }});
702
Jeff Brown6e539312015-02-24 18:53:21 -0800703 registerService(Context.SERIAL_SERVICE, SerialManager.class,
704 new CachedServiceFetcher<SerialManager>() {
705 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600706 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
707 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800708 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
709 }});
710
711 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
712 new CachedServiceFetcher<Vibrator>() {
713 @Override
714 public Vibrator createService(ContextImpl ctx) {
715 return new SystemVibrator(ctx);
716 }});
717
718 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
719 new CachedServiceFetcher<WallpaperManager>() {
720 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800721 public WallpaperManager createService(ContextImpl ctx)
722 throws ServiceNotFoundException {
Felipe Leme34a861a2019-08-05 16:00:12 -0700723 final IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
724 if (b == null) {
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800725 ApplicationInfo appInfo = ctx.getApplicationInfo();
726 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.P
727 && appInfo.isInstantApp()) {
Felipe Leme34a861a2019-08-05 16:00:12 -0700728 // Instant app
729 throw new ServiceNotFoundException(Context.WALLPAPER_SERVICE);
730 }
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800731 final boolean enabled = Resources.getSystem()
732 .getBoolean(com.android.internal.R.bool.config_enableWallpaperService);
733 if (!enabled) {
734 // Device doesn't support wallpaper, return a limited manager
735 return DisabledWallpaperManager.getInstance();
736 }
Felipe Leme34a861a2019-08-05 16:00:12 -0700737 // Bad state - WallpaperManager methods will throw exception
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800738 Log.e(TAG, "No wallpaper service");
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800739 }
740 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
741 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800742 ctx.mMainThread.getHandler());
743 }});
744
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700745 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
746 new CachedServiceFetcher<LowpanManager>() {
747 @Override
748 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
749 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
750 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
751 return new LowpanManager(ctx.getOuterContext(), service,
752 ConnectivityThread.getInstanceLooper());
753 }});
754
Jeff Brown6e539312015-02-24 18:53:21 -0800755 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
756 new CachedServiceFetcher<EthernetManager>() {
757 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600758 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
759 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800760 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
761 return new EthernetManager(ctx.getOuterContext(), service);
762 }});
763
Etan Cohen73450f22019-12-12 23:29:25 -0800764 registerService(Context.WIFI_COND_SERVICE, WifiCondManager.class,
765 new CachedServiceFetcher<WifiCondManager>() {
766 @Override
767 public WifiCondManager createService(ContextImpl ctx) {
768 return new WifiCondManager(ctx.getOuterContext());
769 }
770 });
771
Jeff Brown6e539312015-02-24 18:53:21 -0800772 registerService(Context.WINDOW_SERVICE, WindowManager.class,
773 new CachedServiceFetcher<WindowManager>() {
774 @Override
775 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700776 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800777 }});
778
779 registerService(Context.USER_SERVICE, UserManager.class,
780 new CachedServiceFetcher<UserManager>() {
781 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600782 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
783 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800784 IUserManager service = IUserManager.Stub.asInterface(b);
785 return new UserManager(ctx, service);
786 }});
787
788 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
789 new CachedServiceFetcher<AppOpsManager>() {
790 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600791 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
792 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800793 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
794 return new AppOpsManager(ctx, service);
795 }});
796
797 registerService(Context.CAMERA_SERVICE, CameraManager.class,
798 new CachedServiceFetcher<CameraManager>() {
799 @Override
800 public CameraManager createService(ContextImpl ctx) {
801 return new CameraManager(ctx);
802 }});
803
804 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
805 new CachedServiceFetcher<LauncherApps>() {
806 @Override
807 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700808 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800809 }});
810
811 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
812 new CachedServiceFetcher<RestrictionsManager>() {
813 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600814 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
815 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800816 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
817 return new RestrictionsManager(ctx, service);
818 }});
819
820 registerService(Context.PRINT_SERVICE, PrintManager.class,
821 new CachedServiceFetcher<PrintManager>() {
822 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600823 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700824 IPrintManager service = null;
825 // If the feature not present, don't try to look up every time
826 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
827 service = IPrintManager.Stub.asInterface(ServiceManager
828 .getServiceOrThrow(Context.PRINT_SERVICE));
829 }
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700830 final int userId = ctx.getUserId();
831 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
832 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Jeff Brown6e539312015-02-24 18:53:21 -0800833 }});
834
Eugene Susla6ed45d82017-01-22 13:52:51 -0800835 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
836 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700837 @Override
838 public CompanionDeviceManager createService(ContextImpl ctx)
839 throws ServiceNotFoundException {
840 ICompanionDeviceManager service = null;
841 // If the feature not present, don't try to look up every time
842 if (ctx.getPackageManager().hasSystemFeature(
843 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
844 service = ICompanionDeviceManager.Stub.asInterface(
845 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
846 }
847 return new CompanionDeviceManager(service, ctx.getOuterContext());
848 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800849
Jeff Brown6e539312015-02-24 18:53:21 -0800850 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
851 new CachedServiceFetcher<ConsumerIrManager>() {
852 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600853 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800854 return new ConsumerIrManager(ctx);
855 }});
856
857 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
858 new CachedServiceFetcher<MediaSessionManager>() {
859 @Override
860 public MediaSessionManager createService(ContextImpl ctx) {
861 return new MediaSessionManager(ctx);
862 }});
863
864 registerService(Context.TRUST_SERVICE, TrustManager.class,
865 new StaticServiceFetcher<TrustManager>() {
866 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600867 public TrustManager createService() throws ServiceNotFoundException {
868 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800869 return new TrustManager(b);
870 }});
871
872 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
873 new CachedServiceFetcher<FingerprintManager>() {
874 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600875 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600876 final IBinder binder;
877 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
878 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
879 } else {
880 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
881 }
Jeff Brown6e539312015-02-24 18:53:21 -0800882 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
883 return new FingerprintManager(ctx.getOuterContext(), service);
884 }});
885
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200886 registerService(Context.FACE_SERVICE, FaceManager.class,
887 new CachedServiceFetcher<FaceManager>() {
888 @Override
889 public FaceManager createService(ContextImpl ctx)
890 throws ServiceNotFoundException {
891 final IBinder binder;
892 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
893 binder = ServiceManager.getServiceOrThrow(Context.FACE_SERVICE);
894 } else {
895 binder = ServiceManager.getService(Context.FACE_SERVICE);
896 }
897 IFaceService service = IFaceService.Stub.asInterface(binder);
898 return new FaceManager(ctx.getOuterContext(), service);
899 }
900 });
901
Kevin Chyn51676d22018-11-05 18:00:43 -0800902 registerService(Context.IRIS_SERVICE, IrisManager.class,
903 new CachedServiceFetcher<IrisManager>() {
904 @Override
905 public IrisManager createService(ContextImpl ctx)
906 throws ServiceNotFoundException {
907 final IBinder binder =
908 ServiceManager.getServiceOrThrow(Context.IRIS_SERVICE);
909 IIrisService service = IIrisService.Stub.asInterface(binder);
910 return new IrisManager(ctx.getOuterContext(), service);
911 }
912 });
913
Kevin Chyn05c21502018-09-18 13:07:19 -0700914 registerService(Context.BIOMETRIC_SERVICE, BiometricManager.class,
915 new CachedServiceFetcher<BiometricManager>() {
916 @Override
917 public BiometricManager createService(ContextImpl ctx)
918 throws ServiceNotFoundException {
Kevin Chyne739daf2018-11-06 17:11:06 -0800919 if (BiometricManager.hasBiometrics(ctx)) {
920 final IBinder binder =
Ilya Matyukhine4675b32019-11-07 16:07:19 -0800921 ServiceManager.getServiceOrThrow(Context.AUTH_SERVICE);
922 final IAuthService service =
923 IAuthService.Stub.asInterface(binder);
Kevin Chyne739daf2018-11-06 17:11:06 -0800924 return new BiometricManager(ctx.getOuterContext(), service);
925 } else {
926 // Allow access to the manager when service is null. This saves memory
927 // on devices without biometric hardware.
928 return new BiometricManager(ctx.getOuterContext(), null);
929 }
Kevin Chyn05c21502018-09-18 13:07:19 -0700930 }
931 });
932
Jeff Brown6e539312015-02-24 18:53:21 -0800933 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700934 new CachedServiceFetcher<TvInputManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800935 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700936 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600937 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800938 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700939 return new TvInputManager(service, ctx.getUserId());
Jeff Brown6e539312015-02-24 18:53:21 -0800940 }});
941
942 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
943 new CachedServiceFetcher<NetworkScoreManager>() {
944 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600945 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800946 return new NetworkScoreManager(ctx);
947 }});
948
949 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
950 new CachedServiceFetcher<UsageStatsManager>() {
951 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600952 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
953 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800954 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
955 return new UsageStatsManager(ctx.getOuterContext(), service);
956 }});
957
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000958 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
959 new CachedServiceFetcher<NetworkStatsManager>() {
960 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600961 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000962 return new NetworkStatsManager(ctx.getOuterContext());
963 }});
964
Jeff Brown6e539312015-02-24 18:53:21 -0800965 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
966 new StaticServiceFetcher<PersistentDataBlockManager>() {
967 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600968 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
969 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800970 IPersistentDataBlockService persistentDataBlockService =
971 IPersistentDataBlockService.Stub.asInterface(b);
972 if (persistentDataBlockService != null) {
973 return new PersistentDataBlockManager(persistentDataBlockService);
974 } else {
975 // not supported
976 return null;
977 }
978 }});
979
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000980 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
981 new StaticServiceFetcher<OemLockManager>() {
982 @Override
983 public OemLockManager createService() throws ServiceNotFoundException {
984 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
985 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
986 if (oemLockService != null) {
987 return new OemLockManager(oemLockService);
988 } else {
989 // not supported
990 return null;
991 }
992 }});
993
Jeff Brown6e539312015-02-24 18:53:21 -0800994 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
995 new CachedServiceFetcher<MediaProjectionManager>() {
996 @Override
997 public MediaProjectionManager createService(ContextImpl ctx) {
998 return new MediaProjectionManager(ctx);
999 }});
1000
1001 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
1002 new CachedServiceFetcher<AppWidgetManager>() {
1003 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001004 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1005 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -08001006 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
1007 }});
1008
1009 registerService(Context.MIDI_SERVICE, MidiManager.class,
1010 new CachedServiceFetcher<MidiManager>() {
1011 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001012 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1013 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -07001014 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -08001015 }});
Eric Laurent2035ac82015-03-05 15:18:44 -08001016
1017 registerService(Context.RADIO_SERVICE, RadioManager.class,
1018 new CachedServiceFetcher<RadioManager>() {
1019 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -07001020 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -08001021 return new RadioManager(ctx);
1022 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001023
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001024 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
1025 new CachedServiceFetcher<HardwarePropertiesManager>() {
1026 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001027 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1028 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
1029 IHardwarePropertiesManager service =
1030 IHardwarePropertiesManager.Stub.asInterface(b);
1031 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001032 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001033
1034 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
1035 new CachedServiceFetcher<SoundTriggerManager>() {
1036 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001037 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1038 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001039 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
1040 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001041
1042 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
1043 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -08001044 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001045 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1046 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
1047 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -08001048 }});
Joe Onorato713fec82016-03-04 10:34:02 -08001049
Hyunyoung Song880a9512018-12-20 11:24:48 -08001050 registerService(Context.OVERLAY_SERVICE, OverlayManager.class,
1051 new CachedServiceFetcher<OverlayManager>() {
1052 @Override
1053 public OverlayManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1054 IBinder b = ServiceManager.getServiceOrThrow(Context.OVERLAY_SERVICE);
1055 return new OverlayManager(ctx, IOverlayManager.Stub.asInterface(b));
1056 }});
1057
Ricky Wai1a6e6672017-10-27 14:46:01 +01001058 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
1059 new CachedServiceFetcher<NetworkWatchlistManager>() {
1060 @Override
1061 public NetworkWatchlistManager createService(ContextImpl ctx)
1062 throws ServiceNotFoundException {
1063 IBinder b =
1064 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
1065 return new NetworkWatchlistManager(ctx,
1066 INetworkWatchlistManager.Stub.asInterface(b));
1067 }});
1068
Joe Onorato713fec82016-03-04 10:34:02 -08001069 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
1070 new CachedServiceFetcher<SystemHealthManager>() {
1071 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001072 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1073 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
1074 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -08001075 }});
Peng Xu9ff7d222016-02-11 13:02:05 -08001076
1077 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
1078 new CachedServiceFetcher<ContextHubManager>() {
1079 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001080 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -08001081 return new ContextHubManager(ctx.getOuterContext(),
1082 ctx.mMainThread.getHandler().getLooper());
1083 }});
Joe Onorato1754d742016-11-21 17:51:35 -08001084
1085 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
1086 new CachedServiceFetcher<IncidentManager>() {
1087 @Override
1088 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1089 return new IncidentManager(ctx);
1090 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -08001091
Nandana Duttd11850c2018-12-12 17:26:57 +00001092 registerService(Context.BUGREPORT_SERVICE, BugreportManager.class,
1093 new CachedServiceFetcher<BugreportManager>() {
1094 @Override
1095 public BugreportManager createService(ContextImpl ctx)
1096 throws ServiceNotFoundException {
1097 IBinder b = ServiceManager.getServiceOrThrow(Context.BUGREPORT_SERVICE);
1098 return new BugreportManager(ctx.getOuterContext(),
1099 IDumpstate.Stub.asInterface(b));
1100 }});
1101
Felipe Leme640f30a2017-03-06 15:44:06 -08001102 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
1103 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -08001104 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -08001105 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -07001106 // Get the services without throwing as this is an optional feature
1107 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +00001108 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -08001109 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -08001110 }});
Zak Cohen56345f42017-01-26 13:54:28 -08001111
Felipe Lemeecb08be2018-11-27 15:48:47 -08001112 registerService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class,
1113 new CachedServiceFetcher<ContentCaptureManager>() {
Felipe Lemee348dc32018-11-05 12:35:29 -08001114 @Override
Felipe Lemeecb08be2018-11-27 15:48:47 -08001115 public ContentCaptureManager createService(ContextImpl ctx)
Felipe Lemee348dc32018-11-05 12:35:29 -08001116 throws ServiceNotFoundException {
1117 // Get the services without throwing as this is an optional feature
Felipe Lemeecb08be2018-11-27 15:48:47 -08001118 Context outerContext = ctx.getOuterContext();
Felipe Leme326f15a2019-02-19 09:42:24 -08001119 ContentCaptureOptions options = outerContext.getContentCaptureOptions();
1120 // Options is null when the service didn't whitelist the activity or package
Felipe Lemecbf7f262019-04-17 13:57:59 -07001121 if (options != null && (options.lite || options.isWhitelisted(outerContext))) {
Felipe Lemeecb08be2018-11-27 15:48:47 -08001122 IBinder b = ServiceManager
1123 .getService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
Felipe Leme749b8892018-12-03 16:30:30 -08001124 IContentCaptureManager service = IContentCaptureManager.Stub.asInterface(b);
Felipe Leme326f15a2019-02-19 09:42:24 -08001125 // Service is null when not provided by OEM or disabled by kill-switch.
Felipe Lemed49d52c2019-02-15 09:48:20 -08001126 if (service != null) {
Felipe Leme326f15a2019-02-19 09:42:24 -08001127 return new ContentCaptureManager(outerContext, service, options);
Felipe Lemed49d52c2019-02-15 09:48:20 -08001128 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001129 }
Felipe Leme326f15a2019-02-19 09:42:24 -08001130 // When feature is disabled or app / package not whitelisted, we return a null
1131 // manager to apps so the performance impact is practically zero
Felipe Lemeecb08be2018-11-27 15:48:47 -08001132 return null;
Felipe Lemee348dc32018-11-05 12:35:29 -08001133 }});
1134
Sunny Goyal54e91342018-11-14 11:59:02 -08001135 registerService(Context.APP_PREDICTION_SERVICE, AppPredictionManager.class,
1136 new CachedServiceFetcher<AppPredictionManager>() {
1137 @Override
1138 public AppPredictionManager createService(ContextImpl ctx)
1139 throws ServiceNotFoundException {
Felipe Leme6378bd42019-08-14 18:14:55 -07001140 IBinder b = ServiceManager.getService(Context.APP_PREDICTION_SERVICE);
1141 return b == null ? null : new AppPredictionManager(ctx);
Sunny Goyal54e91342018-11-14 11:59:02 -08001142 }
1143 });
1144
Winson Chung3fb0f252019-01-08 17:41:55 -08001145 registerService(Context.CONTENT_SUGGESTIONS_SERVICE,
1146 ContentSuggestionsManager.class,
1147 new CachedServiceFetcher<ContentSuggestionsManager>() {
1148 @Override
1149 public ContentSuggestionsManager createService(ContextImpl ctx) {
1150 // No throw as this is an optional service
1151 IBinder b = ServiceManager.getService(
1152 Context.CONTENT_SUGGESTIONS_SERVICE);
1153 IContentSuggestionsManager service =
1154 IContentSuggestionsManager.Stub.asInterface(b);
Zak Cohen4834e9f2019-03-08 12:59:01 -08001155 return new ContentSuggestionsManager(ctx.getUserId(), service);
Winson Chung3fb0f252019-01-08 17:41:55 -08001156 }
1157 });
1158
Zak Cohen56345f42017-01-26 13:54:28 -08001159 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
1160 @Override
1161 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1162 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
1163 return new VrManager(IVrManager.Stub.asInterface(b));
1164 }
1165 });
Neil Fuller328532a2017-03-16 18:32:21 +00001166
1167 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
1168 new CachedServiceFetcher<RulesManager>() {
1169 @Override
1170 public RulesManager createService(ContextImpl ctx) {
1171 return new RulesManager(ctx.getOuterContext());
1172 }});
Tony Mak1b708e62017-10-12 10:59:11 +01001173
1174 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
1175 new CachedServiceFetcher<CrossProfileApps>() {
1176 @Override
1177 public CrossProfileApps createService(ContextImpl ctx)
1178 throws ServiceNotFoundException {
1179 IBinder b = ServiceManager.getServiceOrThrow(
1180 Context.CROSS_PROFILE_APPS_SERVICE);
1181 return new CrossProfileApps(ctx.getOuterContext(),
1182 ICrossProfileApps.Stub.asInterface(b));
1183 }
1184 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -04001185
1186 registerService(Context.SLICE_SERVICE, SliceManager.class,
1187 new CachedServiceFetcher<SliceManager>() {
1188 @Override
1189 public SliceManager createService(ContextImpl ctx)
1190 throws ServiceNotFoundException {
1191 return new SliceManager(ctx.getOuterContext(),
1192 ctx.mMainThread.getHandler());
1193 }
1194 });
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001195
Neil Fullerfeeee682018-05-30 14:35:24 +01001196 registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1197 new CachedServiceFetcher<TimeDetector>() {
1198 @Override
1199 public TimeDetector createService(ContextImpl ctx)
1200 throws ServiceNotFoundException {
Neil Fullerad6eb2b2020-01-28 10:55:15 +00001201 return new TimeDetectorImpl();
Neil Fullerfeeee682018-05-30 14:35:24 +01001202 }});
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001203
Neil Fuller3e3b5402019-11-07 15:35:05 +00001204 registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class,
1205 new CachedServiceFetcher<TimeZoneDetector>() {
1206 @Override
1207 public TimeZoneDetector createService(ContextImpl ctx)
1208 throws ServiceNotFoundException {
Neil Fullerad6eb2b2020-01-28 10:55:15 +00001209 return new TimeZoneDetectorImpl();
Neil Fuller3e3b5402019-11-07 15:35:05 +00001210 }});
1211
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001212 registerService(Context.PERMISSION_SERVICE, PermissionManager.class,
1213 new CachedServiceFetcher<PermissionManager>() {
1214 @Override
Peter Wang56dec3f2019-10-25 11:24:33 -07001215 public PermissionManager createService(ContextImpl ctx)
1216 throws ServiceNotFoundException {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001217 IPackageManager packageManager = AppGlobals.getPackageManager();
1218 return new PermissionManager(ctx.getOuterContext(), packageManager);
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001219 }});
Hai Zhang4ef21d02018-11-09 14:43:51 -08001220
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001221 registerService(Context.PERMISSION_CONTROLLER_SERVICE, PermissionControllerManager.class,
1222 new CachedServiceFetcher<PermissionControllerManager>() {
1223 @Override
1224 public PermissionControllerManager createService(ContextImpl ctx) {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001225 return new PermissionControllerManager(ctx.getOuterContext(),
1226 ctx.getMainThreadHandler());
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001227 }});
1228
Hai Zhang4ef21d02018-11-09 14:43:51 -08001229 registerService(Context.ROLE_SERVICE, RoleManager.class,
1230 new CachedServiceFetcher<RoleManager>() {
1231 @Override
1232 public RoleManager createService(ContextImpl ctx)
1233 throws ServiceNotFoundException {
1234 return new RoleManager(ctx.getOuterContext());
1235 }});
Richard Uhlerb29f1452018-09-12 16:38:15 +01001236
Hai Zhanga4959e52019-03-06 12:21:07 -08001237 registerService(Context.ROLE_CONTROLLER_SERVICE, RoleControllerManager.class,
1238 new CachedServiceFetcher<RoleControllerManager>() {
1239 @Override
1240 public RoleControllerManager createService(ContextImpl ctx)
1241 throws ServiceNotFoundException {
1242 return new RoleControllerManager(ctx.getOuterContext());
1243 }});
1244
Richard Uhlerb29f1452018-09-12 16:38:15 +01001245 registerService(Context.ROLLBACK_SERVICE, RollbackManager.class,
1246 new CachedServiceFetcher<RollbackManager>() {
1247 @Override
1248 public RollbackManager createService(ContextImpl ctx)
1249 throws ServiceNotFoundException {
1250 IBinder b = ServiceManager.getServiceOrThrow(Context.ROLLBACK_SERVICE);
1251 return new RollbackManager(ctx.getOuterContext(),
1252 IRollbackManager.Stub.asInterface(b));
1253 }});
Howard Chen0a947642019-01-07 14:10:44 +08001254
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001255 registerService(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class,
1256 new CachedServiceFetcher<DynamicSystemManager>() {
Howard Chen0a947642019-01-07 14:10:44 +08001257 @Override
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001258 public DynamicSystemManager createService(ContextImpl ctx)
Howard Chen0a947642019-01-07 14:10:44 +08001259 throws ServiceNotFoundException {
1260 IBinder b = ServiceManager.getServiceOrThrow(
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001261 Context.DYNAMIC_SYSTEM_SERVICE);
1262 return new DynamicSystemManager(
1263 IDynamicSystemService.Stub.asInterface(b));
Howard Chen0a947642019-01-07 14:10:44 +08001264 }});
Victor Hsieh20fe1f62019-09-30 13:36:21 -07001265
Roshan Pius848513e2019-10-11 13:44:00 -07001266 registerService(Context.BATTERY_STATS_SERVICE, BatteryStatsManager.class,
1267 new CachedServiceFetcher<BatteryStatsManager>() {
1268 @Override
1269 public BatteryStatsManager createService(ContextImpl ctx)
1270 throws ServiceNotFoundException {
1271 IBinder b = ServiceManager.getServiceOrThrow(
1272 Context.BATTERY_STATS_SERVICE);
1273 return new BatteryStatsManager(
1274 IBatteryStats.Stub.asInterface(b));
1275 }});
Songchun Fan6cde9d92019-12-10 10:23:54 -08001276 registerService(Context.DATA_LOADER_MANAGER_SERVICE, DataLoaderManager.class,
1277 new CachedServiceFetcher<DataLoaderManager>() {
1278 @Override
1279 public DataLoaderManager createService(ContextImpl ctx)
1280 throws ServiceNotFoundException {
1281 IBinder b = ServiceManager.getServiceOrThrow(
1282 Context.DATA_LOADER_MANAGER_SERVICE);
1283 return new DataLoaderManager(IDataLoaderManager.Stub.asInterface(b));
1284 }});
Ivailo Karamanolev090d02c2020-01-09 17:02:49 +01001285 registerService(Context.LIGHTS_SERVICE, LightsManager.class,
1286 new CachedServiceFetcher<LightsManager>() {
1287 @Override
1288 public LightsManager createService(ContextImpl ctx)
1289 throws ServiceNotFoundException {
1290 return new LightsManager(ctx);
1291 }});
Songchun Fan43f18ea2019-12-11 17:54:38 -08001292 //TODO(b/136132412): refactor this: 1) merge IIncrementalManager.aidl and
1293 //IIncrementalManagerNative.aidl, 2) implement the binder interface in
1294 //IncrementalManagerService.java, 3) use JNI to call native functions
1295 registerService(Context.INCREMENTAL_SERVICE, IncrementalManager.class,
1296 new CachedServiceFetcher<IncrementalManager>() {
1297 @Override
1298 public IncrementalManager createService(ContextImpl ctx) {
1299 IBinder b = ServiceManager.getService(Context.INCREMENTAL_SERVICE);
1300 if (b == null) {
1301 return null;
1302 }
1303 return new IncrementalManager(
1304 IIncrementalManagerNative.Stub.asInterface(b));
1305 }});
Victor Hsieh20fe1f62019-09-30 13:36:21 -07001306
1307 registerService(Context.FILE_INTEGRITY_SERVICE, FileIntegrityManager.class,
1308 new CachedServiceFetcher<FileIntegrityManager>() {
1309 @Override
1310 public FileIntegrityManager createService(ContextImpl ctx)
1311 throws ServiceNotFoundException {
1312 IBinder b = ServiceManager.getServiceOrThrow(
1313 Context.FILE_INTEGRITY_SERVICE);
1314 return new FileIntegrityManager(
1315 IFileIntegrityService.Stub.asInterface(b));
1316 }});
Felipe Lemeecb08be2018-11-27 15:48:47 -08001317 //CHECKSTYLE:ON IndentationCheck
Song Pan6e3677c2019-10-29 14:19:26 +00001318 registerService(Context.APP_INTEGRITY_SERVICE, AppIntegrityManager.class,
1319 new CachedServiceFetcher<AppIntegrityManager>() {
1320 @Override
1321 public AppIntegrityManager createService(ContextImpl ctx)
1322 throws ServiceNotFoundException {
1323 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_INTEGRITY_SERVICE);
1324 return new AppIntegrityManager(IAppIntegrityManager.Stub.asInterface(b));
1325 }});
Makoto Onuki54c4e032019-10-30 12:05:20 -07001326
Makoto Onuki05b14f52019-11-05 12:36:07 -08001327 sInitializing = true;
1328 try {
1329 // Note: the following functions need to be @SystemApis, once they become mainline
1330 // modules.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001331 JobSchedulerFrameworkInitializer.registerServiceWrappers();
1332 BlobStoreManagerFrameworkInitializer.initialize();
Jayachandran C58fd3eb2019-11-20 19:01:56 -08001333 TelephonyFrameworkInitializer.registerServiceWrappers();
Terry Wangecc0d1b2019-10-17 17:05:18 -07001334 AppSearchManagerFrameworkInitializer.initialize();
David Su409f3712019-10-24 11:18:41 -07001335 WifiFrameworkInitializer.registerServiceWrappers();
Jeffrey Huangf58800b2020-01-23 13:22:10 -08001336 StatsFrameworkInitializer.registerServiceWrappers();
Makoto Onuki05b14f52019-11-05 12:36:07 -08001337 } finally {
1338 // If any of the above code throws, we're in a pretty bad shape and the process
1339 // will likely crash, but we'll reset it just in case there's an exception handler...
1340 sInitializing = false;
1341 }
Jeff Brown6e539312015-02-24 18:53:21 -08001342 }
1343
Makoto Onuki05b14f52019-11-05 12:36:07 -08001344 /** Throws {@link IllegalStateException} if not during a static initialization. */
1345 private static void ensureInitializing(String methodName) {
1346 Preconditions.checkState(sInitializing, "Internal error: " + methodName
1347 + " can only be called during class initialization.");
1348 }
Jeff Brown6e539312015-02-24 18:53:21 -08001349 /**
1350 * Creates an array which is used to cache per-Context service instances.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001351 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001352 */
1353 public static Object[] createServiceCache() {
1354 return new Object[sServiceCacheSize];
1355 }
1356
1357 /**
1358 * Gets a system service from a given context.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001359 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001360 */
1361 public static Object getSystemService(ContextImpl ctx, String name) {
1362 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1363 return fetcher != null ? fetcher.getService(ctx) : null;
1364 }
1365
1366 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001367 * Gets the name of the system-level service that is represented by the specified class.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001368 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001369 */
1370 public static String getSystemServiceName(Class<?> serviceClass) {
1371 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1372 }
1373
1374 /**
1375 * Statically registers a system service with the context.
1376 * This method must be called during static initialization only.
1377 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001378 private static <T> void registerService(@NonNull String serviceName,
1379 @NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) {
Jeff Brown6e539312015-02-24 18:53:21 -08001380 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1381 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1382 }
1383
1384 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001385 * Callback interface used as a parameter to {@link #registerStaticService(
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001386 * String, Class, StaticServiceProducerWithoutBinder)}, which generates a service wrapper
1387 * instance that's not tied to any context and does not take a service binder object in the
1388 * constructor.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001389 *
1390 * @param <TServiceClass> type of the service wrapper class.
Makoto Onuki792de8d2019-07-16 16:19:01 -07001391 *
1392 * @hide
1393 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001394 @SystemApi
1395 public interface StaticServiceProducerWithoutBinder<TServiceClass> {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001396 /**
1397 * Return a new service wrapper of type {@code TServiceClass}.
1398 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001399 @NonNull
Makoto Onuki05b14f52019-11-05 12:36:07 -08001400 TServiceClass createService();
1401 }
1402
1403 /**
1404 * Callback interface used as a parameter to {@link #registerStaticService(
1405 * String, Class, StaticServiceProducerWithBinder)}, which generates a service wrapper instance
1406 * that's not tied to any context and takes a service binder object in the constructor.
1407 *
1408 * @param <TServiceClass> type of the service wrapper class.
1409 *
1410 * @hide
1411 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001412 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001413 public interface StaticServiceProducerWithBinder<TServiceClass> {
1414 /**
1415 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1416 * service binder object.
1417 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001418 @NonNull
1419 TServiceClass createService(@NonNull IBinder serviceBinder);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001420 }
1421
1422 /**
1423 * Callback interface used as a parameter to {@link #registerContextAwareService(
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001424 * String, Class, ContextAwareServiceProducerWithoutBinder)},
Makoto Onuki05b14f52019-11-05 12:36:07 -08001425 * which generates a service wrapper instance
1426 * that's tied to a specific context and does not take a service binder object in the
1427 * constructor.
1428 *
1429 * @param <TServiceClass> type of the service wrapper class.
1430 *
1431 * @hide
1432 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001433 @SystemApi
1434 public interface ContextAwareServiceProducerWithoutBinder<TServiceClass> {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001435 /**
1436 * Return a new service wrapper of type {@code TServiceClass} tied to a given
1437 * {@code context}.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001438 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001439 @NonNull
1440 //TODO Do we need to pass the "base context" too?
1441 TServiceClass createService(@NonNull Context context);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001442 }
1443
1444 /**
1445 * Callback interface used as a parameter to {@link #registerContextAwareService(
1446 * String, Class, ContextAwareServiceProducerWithBinder)},
1447 * which generates a service wrapper instance
1448 * that's tied to a specific context and takes a service binder object in the constructor.
1449 *
1450 * @param <TServiceClass> type of the service wrapper class.
1451 *
1452 * @hide
1453 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001454 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001455 public interface ContextAwareServiceProducerWithBinder<TServiceClass> {
1456 /**
1457 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1458 * service binder object that's tied to a given {@code context}.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001459 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001460 @NonNull
1461 //TODO Do we need to pass the "base context" too?
1462 TServiceClass createService(@NonNull Context context, @NonNull IBinder serviceBinder);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001463 }
1464
1465 /**
1466 * Used by apex modules to register a "service wrapper" that is not tied to any {@link Context}.
1467 *
1468 * <p>This can only be called from the methods called by the static initializer of
1469 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1470 *
1471 * @param serviceName the name of the binder object, such as
1472 * {@link Context#JOB_SCHEDULER_SERVICE}.
1473 * @param serviceWrapperClass the wrapper class, such as the class of
1474 * {@link android.app.job.JobScheduler}.
1475 * @param serviceProducer Callback that takes the service binder object with the name
1476 * {@code serviceName} and returns an actual service wrapper instance.
1477 *
1478 * @hide
1479 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001480 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001481 public static <TServiceClass> void registerStaticService(
1482 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1483 @NonNull StaticServiceProducerWithBinder<TServiceClass> serviceProducer) {
1484 ensureInitializing("registerStaticService");
1485 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001486 Objects.requireNonNull(serviceWrapperClass);
1487 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001488
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001489 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001490 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki792de8d2019-07-16 16:19:01 -07001491 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001492 public TServiceClass createService() throws ServiceNotFoundException {
1493 return serviceProducer.createService(
1494 ServiceManager.getServiceOrThrow(serviceName));
Makoto Onuki792de8d2019-07-16 16:19:01 -07001495 }});
1496 }
1497
1498 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001499 * Similar to {@link #registerStaticService(String, Class, StaticServiceProducerWithBinder)},
1500 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001501 *
1502 * @hide
1503 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001504 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001505 public static <TServiceClass> void registerStaticService(
1506 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001507 @NonNull StaticServiceProducerWithoutBinder<TServiceClass> serviceProducer) {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001508 ensureInitializing("registerStaticService");
1509 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001510 Objects.requireNonNull(serviceWrapperClass);
1511 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001512
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001513 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001514 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001515 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001516 public TServiceClass createService() {
1517 return serviceProducer.createService();
1518 }});
1519 }
1520
1521 /**
1522 * Used by apex modules to register a "service wrapper" that is tied to a specific
1523 * {@link Context}.
1524 *
1525 * <p>This can only be called from the methods called by the static initializer of
1526 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1527 *
1528 * @param serviceName the name of the binder object, such as
1529 * {@link Context#JOB_SCHEDULER_SERVICE}.
1530 * @param serviceWrapperClass the wrapper class, such as the class of
1531 * {@link android.app.job.JobScheduler}.
1532 * @param serviceProducer lambda that takes the service binder object with the name
1533 * {@code serviceName}, a {@link Context} and returns an actual service wrapper instance.
1534 *
1535 * @hide
1536 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001537 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001538 public static <TServiceClass> void registerContextAwareService(
1539 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1540 @NonNull ContextAwareServiceProducerWithBinder<TServiceClass> serviceProducer) {
1541 ensureInitializing("registerContextAwareService");
1542 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001543 Objects.requireNonNull(serviceWrapperClass);
1544 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001545
1546 registerService(serviceName, serviceWrapperClass,
1547 new CachedServiceFetcher<TServiceClass>() {
1548 @Override
1549 public TServiceClass createService(ContextImpl ctx)
1550 throws ServiceNotFoundException {
1551 return serviceProducer.createService(
1552 ctx.getOuterContext(),
1553 ServiceManager.getServiceOrThrow(serviceName));
1554 }});
1555 }
1556
1557
1558 /**
1559 * Similar to {@link #registerContextAwareService(String, Class,
1560 * ContextAwareServiceProducerWithBinder)},
1561 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
1562 *
1563 * @hide
1564 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001565 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001566 public static <TServiceClass> void registerContextAwareService(
1567 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001568 @NonNull ContextAwareServiceProducerWithoutBinder<TServiceClass> serviceProducer) {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001569 ensureInitializing("registerContextAwareService");
1570 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001571 Objects.requireNonNull(serviceWrapperClass);
1572 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001573
1574 registerService(serviceName, serviceWrapperClass,
1575 new CachedServiceFetcher<TServiceClass>() {
1576 @Override
1577 public TServiceClass createService(ContextImpl ctx) {
1578 return serviceProducer.createService(ctx.getOuterContext());
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001579 }});
1580 }
1581
1582 /**
Jeff Brown6e539312015-02-24 18:53:21 -08001583 * Base interface for classes that fetch services.
1584 * These objects must only be created during static initialization.
1585 */
1586 static abstract interface ServiceFetcher<T> {
1587 T getService(ContextImpl ctx);
1588 }
1589
1590 /**
1591 * Override this class when the system service constructor needs a
1592 * ContextImpl and should be cached and retained by that context.
1593 */
1594 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1595 private final int mCacheIndex;
1596
Makoto Onukid67070e2018-03-30 12:39:14 -07001597 CachedServiceFetcher() {
1598 // Note this class must be instantiated only by the static initializer of the
1599 // outer class (SystemServiceRegistry), which already does the synchronization,
1600 // so bare access to sServiceCacheSize is okay here.
Jeff Brown6e539312015-02-24 18:53:21 -08001601 mCacheIndex = sServiceCacheSize++;
1602 }
1603
1604 @Override
1605 @SuppressWarnings("unchecked")
1606 public final T getService(ContextImpl ctx) {
1607 final Object[] cache = ctx.mServiceCache;
Makoto Onukid67070e2018-03-30 12:39:14 -07001608 final int[] gates = ctx.mServiceInitializationStateArray;
Makoto Onukif896f122018-01-25 09:50:24 -08001609
Makoto Onukid67070e2018-03-30 12:39:14 -07001610 for (;;) {
1611 boolean doInitialize = false;
1612 synchronized (cache) {
1613 // Return it if we already have a cached instance.
1614 T service = (T) cache[mCacheIndex];
1615 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1616 return service;
1617 }
Makoto Onukif896f122018-01-25 09:50:24 -08001618
Makoto Onukid67070e2018-03-30 12:39:14 -07001619 // If we get here, there's no cached instance.
Makoto Onukif896f122018-01-25 09:50:24 -08001620
Makoto Onukid67070e2018-03-30 12:39:14 -07001621 // Grr... if gate is STATE_READY, then this means we initialized the service
1622 // once but someone cleared it.
1623 // We start over from STATE_UNINITIALIZED.
1624 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1625 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1626 }
1627
1628 // It's possible for multiple threads to get here at the same time, so
1629 // use the "gate" to make sure only the first thread will call createService().
1630
1631 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1632 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1633 doInitialize = true;
1634 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1635 }
Makoto Onukif896f122018-01-25 09:50:24 -08001636 }
1637
Makoto Onukid67070e2018-03-30 12:39:14 -07001638 if (doInitialize) {
1639 // Only the first thread gets here.
Makoto Onukif896f122018-01-25 09:50:24 -08001640
Makoto Onukid67070e2018-03-30 12:39:14 -07001641 T service = null;
1642 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001643 try {
Makoto Onukid67070e2018-03-30 12:39:14 -07001644 // This thread is the first one to get here. Instantiate the service
1645 // *without* the cache lock held.
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001646 service = createService(ctx);
Makoto Onukid67070e2018-03-30 12:39:14 -07001647 newState = ContextImpl.STATE_READY;
Makoto Onukif896f122018-01-25 09:50:24 -08001648
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001649 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001650 onServiceNotFound(e);
Makoto Onukif896f122018-01-25 09:50:24 -08001651
Makoto Onukid67070e2018-03-30 12:39:14 -07001652 } finally {
1653 synchronized (cache) {
1654 cache[mCacheIndex] = service;
1655 gates[mCacheIndex] = newState;
1656 cache.notifyAll();
1657 }
1658 }
1659 return service;
1660 }
1661 // The other threads will wait for the first thread to call notifyAll(),
1662 // and go back to the top and retry.
1663 synchronized (cache) {
1664 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1665 try {
1666 cache.wait();
1667 } catch (InterruptedException e) {
1668 Log.w(TAG, "getService() interrupted");
1669 Thread.currentThread().interrupt();
1670 return null;
1671 }
Makoto Onukif896f122018-01-25 09:50:24 -08001672 }
1673 }
Makoto Onukif896f122018-01-25 09:50:24 -08001674 }
Jeff Brown6e539312015-02-24 18:53:21 -08001675 }
1676
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001677 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001678 }
1679
1680 /**
1681 * Override this class when the system service does not need a ContextImpl
1682 * and should be cached and retained process-wide.
1683 */
1684 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1685 private T mCachedInstance;
1686
1687 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001688 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001689 synchronized (StaticServiceFetcher.this) {
1690 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001691 try {
1692 mCachedInstance = createService();
1693 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001694 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001695 }
Jeff Brown6e539312015-02-24 18:53:21 -08001696 }
1697 return mCachedInstance;
1698 }
1699 }
1700
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001701 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001702 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001703
1704 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001705 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1706 * creating the service for the first time, passes it the application context of the creating
1707 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001708 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001709 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1710 * case where multiple application components each have their own ConnectivityManager object.
1711 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001712 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001713 private T mCachedInstance;
1714
1715 @Override
1716 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001717 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001718 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001719 Context appContext = ctx.getApplicationContext();
1720 // If the application context is null, we're either in the system process or
1721 // it's the application context very early in app initialization. In both these
1722 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1723 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001724 try {
1725 mCachedInstance = createService(appContext != null ? appContext : ctx);
1726 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001727 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001728 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001729 }
1730 return mCachedInstance;
1731 }
1732 }
1733
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001734 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001735 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001736
Makoto Onuki05b14f52019-11-05 12:36:07 -08001737 /** @hide */
Jeff Sharkey589f3092016-11-29 12:54:39 -07001738 public static void onServiceNotFound(ServiceNotFoundException e) {
1739 // We're mostly interested in tracking down long-lived core system
1740 // components that might stumble if they obtain bad references; just
1741 // emit a tidy log message for normal apps
1742 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1743 Log.wtf(TAG, e.getMessage(), e);
1744 } else {
1745 Log.w(TAG, e.getMessage());
1746 }
1747 }
Jeff Brown6e539312015-02-24 18:53:21 -08001748}