blob: 4310434c70be4f2ff7a8d7a7c3a90abf2a46a33e [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;
21import android.app.admin.DevicePolicyManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060022import android.app.admin.IDevicePolicyManager;
Jeff Brown6e539312015-02-24 18:53:21 -080023import android.app.job.IJobScheduler;
24import android.app.job.JobScheduler;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040025import android.app.slice.SliceManager;
Neil Fuller328532a2017-03-16 18:32:21 +000026import android.app.timezone.RulesManager;
Jeff Brown6e539312015-02-24 18:53:21 -080027import android.app.trust.TrustManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070028import android.app.usage.IStorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080029import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000030import android.app.usage.NetworkStatsManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070031import android.app.usage.StorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080032import android.app.usage.UsageStatsManager;
33import android.appwidget.AppWidgetManager;
34import android.bluetooth.BluetoothManager;
Eugene Susla6ed45d82017-01-22 13:52:51 -080035import android.companion.CompanionDeviceManager;
36import android.companion.ICompanionDeviceManager;
Jeff Brown6e539312015-02-24 18:53:21 -080037import android.content.ClipboardManager;
38import android.content.Context;
39import android.content.IRestrictionsManager;
40import android.content.RestrictionsManager;
Tony Makb0d22622018-01-18 12:49:49 +000041import android.content.pm.CrossProfileApps;
42import android.content.pm.ICrossProfileApps;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080043import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080044import android.content.pm.LauncherApps;
Svet Ganovec7daa52017-06-30 12:02:59 -070045import android.content.pm.PackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080046import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080047import android.content.res.Resources;
48import android.hardware.ConsumerIrManager;
49import android.hardware.ISerialManager;
50import android.hardware.SensorManager;
51import android.hardware.SerialManager;
52import android.hardware.SystemSensorManager;
53import android.hardware.camera2.CameraManager;
54import android.hardware.display.DisplayManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060055import android.hardware.fingerprint.FingerprintManager;
56import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -080057import android.hardware.hdmi.HdmiControlManager;
58import android.hardware.hdmi.IHdmiControlService;
59import android.hardware.input.InputManager;
Peng Xu9ff7d222016-02-11 13:02:05 -080060import android.hardware.location.ContextHubManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060061import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080062import android.hardware.usb.IUsbManager;
63import android.hardware.usb.UsbManager;
64import android.location.CountryDetector;
65import android.location.ICountryDetector;
66import android.location.ILocationManager;
67import android.location.LocationManager;
68import android.media.AudioManager;
69import android.media.MediaRouter;
70import android.media.midi.IMidiManager;
71import android.media.midi.MidiManager;
72import android.media.projection.MediaProjectionManager;
73import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080074import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -080075import android.media.tv.ITvInputManager;
76import android.media.tv.TvInputManager;
77import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -070078import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -080079import android.net.EthernetManager;
80import android.net.IConnectivityManager;
81import android.net.IEthernetManager;
Nathan Harold28084d82017-03-01 18:55:06 -080082import android.net.IIpSecService;
Jeff Brown6e539312015-02-24 18:53:21 -080083import android.net.INetworkPolicyManager;
Nathan Harold28084d82017-03-01 18:55:06 -080084import android.net.IpSecManager;
Jeff Brown6e539312015-02-24 18:53:21 -080085import android.net.NetworkPolicyManager;
86import android.net.NetworkScoreManager;
Ricky Wai1a6e6672017-10-27 14:46:01 +010087import android.net.NetworkWatchlistManager;
Robert Quattlebaum87a71042017-05-15 15:45:20 -070088import android.net.lowpan.ILowpanManager;
89import android.net.lowpan.LowpanManager;
Etan Cohen17ba4722017-08-21 10:52:17 -070090import android.net.nsd.INsdManager;
91import android.net.nsd.NsdManager;
Jeff Brown6e539312015-02-24 18:53:21 -080092import android.net.wifi.IRttManager;
93import android.net.wifi.IWifiManager;
94import android.net.wifi.IWifiScanner;
95import android.net.wifi.RttManager;
96import android.net.wifi.WifiManager;
97import android.net.wifi.WifiScanner;
Etan Cohen04133272016-10-26 11:22:06 -070098import android.net.wifi.aware.IWifiAwareManager;
99import android.net.wifi.aware.WifiAwareManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800100import android.net.wifi.p2p.IWifiP2pManager;
101import android.net.wifi.p2p.WifiP2pManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700102import android.net.wifi.rtt.IWifiRttManager;
103import android.net.wifi.rtt.WifiRttManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800104import android.nfc.NfcManager;
105import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600106import android.os.BatteryStats;
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600107import android.os.Build;
Jeff Brown6e539312015-02-24 18:53:21 -0800108import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100109import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600110import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -0800111import android.os.IBinder;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100112import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800113import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800114import android.os.IRecoverySystem;
Tao Bao07342dc2017-01-24 15:08:21 -0800115import android.os.ISystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800116import android.os.IUserManager;
Joe Onorato1754d742016-11-21 17:51:35 -0800117import android.os.IncidentManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800118import android.os.PowerManager;
119import android.os.Process;
Tao Baoe8a403d2015-12-31 07:44:55 -0800120import android.os.RecoverySystem;
Jeff Brown6e539312015-02-24 18:53:21 -0800121import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600122import android.os.ServiceManager.ServiceNotFoundException;
Tao Bao07342dc2017-01-24 15:08:21 -0800123import android.os.SystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800124import android.os.SystemVibrator;
125import android.os.UserHandle;
126import android.os.UserManager;
127import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800128import android.os.health.SystemHealthManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800129import android.os.storage.StorageManager;
130import android.print.IPrintManager;
131import android.print.PrintManager;
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000132import android.service.oemlock.IOemLockService;
133import android.service.oemlock.OemLockManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800134import android.service.persistentdata.IPersistentDataBlockService;
135import android.service.persistentdata.PersistentDataBlockManager;
Zak Cohen56345f42017-01-26 13:54:28 -0800136import android.service.vr.IVrManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800137import android.telecom.TelecomManager;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800138import android.telephony.CarrierConfigManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800139import android.telephony.SubscriptionManager;
140import android.telephony.TelephonyManager;
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800141import android.telephony.euicc.EuiccCardManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100142import android.telephony.euicc.EuiccManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800143import android.util.Log;
144import android.view.ContextThemeWrapper;
145import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800146import android.view.WindowManager;
147import android.view.WindowManagerImpl;
148import android.view.accessibility.AccessibilityManager;
149import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100150import android.view.autofill.AutofillManager;
151import android.view.autofill.IAutoFillManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800152import android.view.inputmethod.InputMethodManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000153import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800154import android.view.textservice.TextServicesManager;
155
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600156import com.android.internal.app.IAppOpsService;
157import com.android.internal.app.IBatteryStats;
158import com.android.internal.app.ISoundTriggerService;
159import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100160import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600161import com.android.internal.os.IDropBoxManagerService;
162import com.android.internal.policy.PhoneLayoutInflater;
163
Jeff Brown6e539312015-02-24 18:53:21 -0800164import java.util.HashMap;
165
166/**
167 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
168 * Used by {@link ContextImpl}.
169 */
170final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600171 private static final String TAG = "SystemServiceRegistry";
172
Jeff Brown6e539312015-02-24 18:53:21 -0800173 // Service registry information.
174 // This information is never changed once static initialization has completed.
175 private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
176 new HashMap<Class<?>, String>();
177 private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
178 new HashMap<String, ServiceFetcher<?>>();
179 private static int sServiceCacheSize;
180
181 // Not instantiable.
182 private SystemServiceRegistry() { }
183
184 static {
185 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
186 new CachedServiceFetcher<AccessibilityManager>() {
187 @Override
188 public AccessibilityManager createService(ContextImpl ctx) {
189 return AccessibilityManager.getInstance(ctx);
190 }});
191
192 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
193 new CachedServiceFetcher<CaptioningManager>() {
194 @Override
195 public CaptioningManager createService(ContextImpl ctx) {
196 return new CaptioningManager(ctx);
197 }});
198
199 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
200 new CachedServiceFetcher<AccountManager>() {
201 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600202 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
203 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800204 IAccountManager service = IAccountManager.Stub.asInterface(b);
205 return new AccountManager(ctx, service);
206 }});
207
208 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
209 new CachedServiceFetcher<ActivityManager>() {
210 @Override
211 public ActivityManager createService(ContextImpl ctx) {
212 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
213 }});
214
215 registerService(Context.ALARM_SERVICE, AlarmManager.class,
216 new CachedServiceFetcher<AlarmManager>() {
217 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600218 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
219 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800220 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
221 return new AlarmManager(service, ctx);
222 }});
223
224 registerService(Context.AUDIO_SERVICE, AudioManager.class,
225 new CachedServiceFetcher<AudioManager>() {
226 @Override
227 public AudioManager createService(ContextImpl ctx) {
228 return new AudioManager(ctx);
229 }});
230
231 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
232 new CachedServiceFetcher<MediaRouter>() {
233 @Override
234 public MediaRouter createService(ContextImpl ctx) {
235 return new MediaRouter(ctx);
236 }});
237
238 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
239 new CachedServiceFetcher<BluetoothManager>() {
240 @Override
241 public BluetoothManager createService(ContextImpl ctx) {
242 return new BluetoothManager(ctx);
243 }});
244
245 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
246 new StaticServiceFetcher<HdmiControlManager>() {
247 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600248 public HdmiControlManager createService() throws ServiceNotFoundException {
249 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800250 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
251 }});
252
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000253 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000254 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000255 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000256 public TextClassificationManager createService(ContextImpl ctx) {
257 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000258 }});
259
Jeff Brown6e539312015-02-24 18:53:21 -0800260 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
261 new CachedServiceFetcher<ClipboardManager>() {
262 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600263 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800264 return new ClipboardManager(ctx.getOuterContext(),
265 ctx.mMainThread.getHandler());
266 }});
267
268 // The clipboard service moved to a new package. If someone asks for the old
269 // interface by class then we want to redirect over to the new interface instead
270 // (which extends it).
271 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
272
273 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900274 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800275 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600276 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
277 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900278 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
279 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800280 }});
281
Nathan Harold28084d82017-03-01 18:55:06 -0800282 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
283 new StaticServiceFetcher<IpSecManager>() {
284 @Override
285 public IpSecManager createService() {
286 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
287 IIpSecService service = IIpSecService.Stub.asInterface(b);
288 return new IpSecManager(service);
289 }});
290
Jeff Brown6e539312015-02-24 18:53:21 -0800291 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
292 new StaticServiceFetcher<CountryDetector>() {
293 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600294 public CountryDetector createService() throws ServiceNotFoundException {
295 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800296 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
297 }});
298
299 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
300 new CachedServiceFetcher<DevicePolicyManager>() {
301 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600302 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
303 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
304 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800305 }});
306
307 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
308 new CachedServiceFetcher<DownloadManager>() {
309 @Override
310 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700311 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800312 }});
313
314 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700315 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800316 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700317 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600318 IBatteryStats stats = IBatteryStats.Stub.asInterface(
319 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
320 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
321 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700322 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800323 }});
324
325 registerService(Context.NFC_SERVICE, NfcManager.class,
326 new CachedServiceFetcher<NfcManager>() {
327 @Override
328 public NfcManager createService(ContextImpl ctx) {
329 return new NfcManager(ctx);
330 }});
331
332 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700333 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800334 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600335 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
336 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800337 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700338 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800339 }});
340
341 registerService(Context.INPUT_SERVICE, InputManager.class,
342 new StaticServiceFetcher<InputManager>() {
343 @Override
344 public InputManager createService() {
345 return InputManager.getInstance();
346 }});
347
348 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
349 new CachedServiceFetcher<DisplayManager>() {
350 @Override
351 public DisplayManager createService(ContextImpl ctx) {
352 return new DisplayManager(ctx.getOuterContext());
353 }});
354
355 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
356 new StaticServiceFetcher<InputMethodManager>() {
357 @Override
358 public InputMethodManager createService() {
359 return InputMethodManager.getInstance();
360 }});
361
362 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
363 new StaticServiceFetcher<TextServicesManager>() {
364 @Override
365 public TextServicesManager createService() {
366 return TextServicesManager.getInstance();
367 }});
368
369 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800370 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800371 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800372 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800373 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800374 }});
375
376 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
377 new CachedServiceFetcher<LayoutInflater>() {
378 @Override
379 public LayoutInflater createService(ContextImpl ctx) {
380 return new PhoneLayoutInflater(ctx.getOuterContext());
381 }});
382
383 registerService(Context.LOCATION_SERVICE, LocationManager.class,
384 new CachedServiceFetcher<LocationManager>() {
385 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600386 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
387 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800388 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
389 }});
390
391 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700392 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800393 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600394 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700395 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600396 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800397 }});
398
399 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
400 new CachedServiceFetcher<NotificationManager>() {
401 @Override
402 public NotificationManager createService(ContextImpl ctx) {
403 final Context outerContext = ctx.getOuterContext();
404 return new NotificationManager(
405 new ContextThemeWrapper(outerContext,
406 Resources.selectSystemTheme(0,
407 outerContext.getApplicationInfo().targetSdkVersion,
408 com.android.internal.R.style.Theme_Dialog,
409 com.android.internal.R.style.Theme_Holo_Dialog,
410 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
411 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
412 ctx.mMainThread.getHandler());
413 }});
414
415 registerService(Context.NSD_SERVICE, NsdManager.class,
416 new CachedServiceFetcher<NsdManager>() {
417 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600418 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
419 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800420 INsdManager service = INsdManager.Stub.asInterface(b);
421 return new NsdManager(ctx.getOuterContext(), service);
422 }});
423
424 registerService(Context.POWER_SERVICE, PowerManager.class,
425 new CachedServiceFetcher<PowerManager>() {
426 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600427 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
428 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800429 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800430 return new PowerManager(ctx.getOuterContext(),
431 service, ctx.mMainThread.getHandler());
432 }});
433
Tao Baoe8a403d2015-12-31 07:44:55 -0800434 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
435 new CachedServiceFetcher<RecoverySystem>() {
436 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600437 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
438 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800439 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800440 return new RecoverySystem(service);
441 }});
442
Jeff Brown6e539312015-02-24 18:53:21 -0800443 registerService(Context.SEARCH_SERVICE, SearchManager.class,
444 new CachedServiceFetcher<SearchManager>() {
445 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600446 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800447 return new SearchManager(ctx.getOuterContext(),
448 ctx.mMainThread.getHandler());
449 }});
450
451 registerService(Context.SENSOR_SERVICE, SensorManager.class,
452 new CachedServiceFetcher<SensorManager>() {
453 @Override
454 public SensorManager createService(ContextImpl ctx) {
455 return new SystemSensorManager(ctx.getOuterContext(),
456 ctx.mMainThread.getHandler().getLooper());
457 }});
458
David Chenadaf8b32017-11-03 15:42:08 -0700459 registerService(Context.STATS_MANAGER, StatsManager.class,
460 new StaticServiceFetcher<StatsManager>() {
461 @Override
462 public StatsManager createService() throws ServiceNotFoundException {
463 return new StatsManager();
464 }});
465
Jeff Brown6e539312015-02-24 18:53:21 -0800466 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
467 new CachedServiceFetcher<StatusBarManager>() {
468 @Override
469 public StatusBarManager createService(ContextImpl ctx) {
470 return new StatusBarManager(ctx.getOuterContext());
471 }});
472
473 registerService(Context.STORAGE_SERVICE, StorageManager.class,
474 new CachedServiceFetcher<StorageManager>() {
475 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600476 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700477 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800478 }});
479
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700480 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
481 new CachedServiceFetcher<StorageStatsManager>() {
482 @Override
483 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
484 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
485 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
486 return new StorageStatsManager(ctx, service);
487 }});
488
Tao Bao07342dc2017-01-24 15:08:21 -0800489 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
490 new CachedServiceFetcher<SystemUpdateManager>() {
491 @Override
492 public SystemUpdateManager createService(ContextImpl ctx)
493 throws ServiceNotFoundException {
494 IBinder b = ServiceManager.getServiceOrThrow(
495 Context.SYSTEM_UPDATE_SERVICE);
496 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
497 return new SystemUpdateManager(service);
498 }});
499
Jeff Brown6e539312015-02-24 18:53:21 -0800500 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
501 new CachedServiceFetcher<TelephonyManager>() {
502 @Override
503 public TelephonyManager createService(ContextImpl ctx) {
504 return new TelephonyManager(ctx.getOuterContext());
505 }});
506
507 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
508 new CachedServiceFetcher<SubscriptionManager>() {
509 @Override
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700510 public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800511 return new SubscriptionManager(ctx.getOuterContext());
512 }});
513
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800514 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
515 new CachedServiceFetcher<CarrierConfigManager>() {
516 @Override
517 public CarrierConfigManager createService(ContextImpl ctx) {
518 return new CarrierConfigManager();
519 }});
520
Jeff Brown6e539312015-02-24 18:53:21 -0800521 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
522 new CachedServiceFetcher<TelecomManager>() {
523 @Override
524 public TelecomManager createService(ContextImpl ctx) {
525 return new TelecomManager(ctx.getOuterContext());
526 }});
527
Jeff Davidson35cda392017-02-27 09:46:00 -0800528 registerService(Context.EUICC_SERVICE, EuiccManager.class,
529 new CachedServiceFetcher<EuiccManager>() {
530 @Override
531 public EuiccManager createService(ContextImpl ctx) {
532 return new EuiccManager(ctx.getOuterContext());
533 }});
534
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800535 registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class,
536 new CachedServiceFetcher<EuiccCardManager>() {
537 @Override
538 public EuiccCardManager createService(ContextImpl ctx) {
539 return new EuiccCardManager(ctx.getOuterContext());
540 }});
541
Jeff Brown6e539312015-02-24 18:53:21 -0800542 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
543 new CachedServiceFetcher<UiModeManager>() {
544 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600545 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800546 return new UiModeManager();
547 }});
548
549 registerService(Context.USB_SERVICE, UsbManager.class,
550 new CachedServiceFetcher<UsbManager>() {
551 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600552 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
553 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800554 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
555 }});
556
557 registerService(Context.SERIAL_SERVICE, SerialManager.class,
558 new CachedServiceFetcher<SerialManager>() {
559 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600560 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
561 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800562 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
563 }});
564
565 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
566 new CachedServiceFetcher<Vibrator>() {
567 @Override
568 public Vibrator createService(ContextImpl ctx) {
569 return new SystemVibrator(ctx);
570 }});
571
572 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
573 new CachedServiceFetcher<WallpaperManager>() {
574 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800575 public WallpaperManager createService(ContextImpl ctx)
576 throws ServiceNotFoundException {
577 final IBinder b;
578 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
579 b = ServiceManager.getServiceOrThrow(Context.WALLPAPER_SERVICE);
580 } else {
581 b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
582 }
583 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
584 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800585 ctx.mMainThread.getHandler());
586 }});
587
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700588 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
589 new CachedServiceFetcher<LowpanManager>() {
590 @Override
591 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
592 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
593 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
594 return new LowpanManager(ctx.getOuterContext(), service,
595 ConnectivityThread.getInstanceLooper());
596 }});
597
Jeff Brown6e539312015-02-24 18:53:21 -0800598 registerService(Context.WIFI_SERVICE, WifiManager.class,
599 new CachedServiceFetcher<WifiManager>() {
600 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600601 public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
602 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800603 IWifiManager service = IWifiManager.Stub.asInterface(b);
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700604 return new WifiManager(ctx.getOuterContext(), service,
605 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800606 }});
607
Jeff Brown6e539312015-02-24 18:53:21 -0800608 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
609 new StaticServiceFetcher<WifiP2pManager>() {
610 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600611 public WifiP2pManager createService() throws ServiceNotFoundException {
612 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800613 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
614 return new WifiP2pManager(service);
615 }});
616
Etan Cohen04133272016-10-26 11:22:06 -0700617 registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
618 new CachedServiceFetcher<WifiAwareManager>() {
Etan Cohen20d329b2015-09-29 13:49:02 -0700619 @Override
Etan Cohen04133272016-10-26 11:22:06 -0700620 public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey589f3092016-11-29 12:54:39 -0700621 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
Etan Cohen04133272016-10-26 11:22:06 -0700622 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
Etan Cohen20d329b2015-09-29 13:49:02 -0700623 if (service == null) {
624 return null;
625 }
Etan Cohen04133272016-10-26 11:22:06 -0700626 return new WifiAwareManager(ctx.getOuterContext(), service);
Etan Cohen20d329b2015-09-29 13:49:02 -0700627 }});
628
Jeff Brown6e539312015-02-24 18:53:21 -0800629 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
630 new CachedServiceFetcher<WifiScanner>() {
631 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600632 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
633 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800634 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
Mitchell Wills91312c72016-04-06 17:03:35 -0700635 return new WifiScanner(ctx.getOuterContext(), service,
636 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800637 }});
638
639 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
640 new CachedServiceFetcher<RttManager>() {
641 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600642 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
643 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800644 IRttManager service = IRttManager.Stub.asInterface(b);
Wei Wangbd3353a2016-04-08 14:24:09 -0700645 return new RttManager(ctx.getOuterContext(), service,
646 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800647 }});
648
Etan Cohen46efb482017-12-07 13:50:57 -0800649 registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class,
Etan Cohen17ba4722017-08-21 10:52:17 -0700650 new CachedServiceFetcher<WifiRttManager>() {
651 @Override
652 public WifiRttManager createService(ContextImpl ctx)
653 throws ServiceNotFoundException {
Etan Cohen46efb482017-12-07 13:50:57 -0800654 IBinder b = ServiceManager.getServiceOrThrow(
655 Context.WIFI_RTT_RANGING_SERVICE);
Etan Cohen17ba4722017-08-21 10:52:17 -0700656 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
657 return new WifiRttManager(ctx.getOuterContext(), service);
658 }});
659
Jeff Brown6e539312015-02-24 18:53:21 -0800660 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
661 new CachedServiceFetcher<EthernetManager>() {
662 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600663 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
664 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800665 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
666 return new EthernetManager(ctx.getOuterContext(), service);
667 }});
668
669 registerService(Context.WINDOW_SERVICE, WindowManager.class,
670 new CachedServiceFetcher<WindowManager>() {
671 @Override
672 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700673 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800674 }});
675
676 registerService(Context.USER_SERVICE, UserManager.class,
677 new CachedServiceFetcher<UserManager>() {
678 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600679 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
680 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800681 IUserManager service = IUserManager.Stub.asInterface(b);
682 return new UserManager(ctx, service);
683 }});
684
685 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
686 new CachedServiceFetcher<AppOpsManager>() {
687 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600688 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
689 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800690 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
691 return new AppOpsManager(ctx, service);
692 }});
693
694 registerService(Context.CAMERA_SERVICE, CameraManager.class,
695 new CachedServiceFetcher<CameraManager>() {
696 @Override
697 public CameraManager createService(ContextImpl ctx) {
698 return new CameraManager(ctx);
699 }});
700
701 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
702 new CachedServiceFetcher<LauncherApps>() {
703 @Override
704 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700705 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800706 }});
707
708 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
709 new CachedServiceFetcher<RestrictionsManager>() {
710 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600711 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
712 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800713 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
714 return new RestrictionsManager(ctx, service);
715 }});
716
717 registerService(Context.PRINT_SERVICE, PrintManager.class,
718 new CachedServiceFetcher<PrintManager>() {
719 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600720 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700721 IPrintManager service = null;
722 // If the feature not present, don't try to look up every time
723 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
724 service = IPrintManager.Stub.asInterface(ServiceManager
725 .getServiceOrThrow(Context.PRINT_SERVICE));
726 }
Jeff Brown6e539312015-02-24 18:53:21 -0800727 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
728 UserHandle.getAppId(Process.myUid()));
729 }});
730
Eugene Susla6ed45d82017-01-22 13:52:51 -0800731 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
732 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700733 @Override
734 public CompanionDeviceManager createService(ContextImpl ctx)
735 throws ServiceNotFoundException {
736 ICompanionDeviceManager service = null;
737 // If the feature not present, don't try to look up every time
738 if (ctx.getPackageManager().hasSystemFeature(
739 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
740 service = ICompanionDeviceManager.Stub.asInterface(
741 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
742 }
743 return new CompanionDeviceManager(service, ctx.getOuterContext());
744 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800745
Jeff Brown6e539312015-02-24 18:53:21 -0800746 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
747 new CachedServiceFetcher<ConsumerIrManager>() {
748 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600749 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800750 return new ConsumerIrManager(ctx);
751 }});
752
753 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
754 new CachedServiceFetcher<MediaSessionManager>() {
755 @Override
756 public MediaSessionManager createService(ContextImpl ctx) {
757 return new MediaSessionManager(ctx);
758 }});
759
760 registerService(Context.TRUST_SERVICE, TrustManager.class,
761 new StaticServiceFetcher<TrustManager>() {
762 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600763 public TrustManager createService() throws ServiceNotFoundException {
764 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800765 return new TrustManager(b);
766 }});
767
768 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
769 new CachedServiceFetcher<FingerprintManager>() {
770 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600771 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600772 final IBinder binder;
773 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
774 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
775 } else {
776 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
777 }
Jeff Brown6e539312015-02-24 18:53:21 -0800778 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
779 return new FingerprintManager(ctx.getOuterContext(), service);
780 }});
781
782 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
783 new StaticServiceFetcher<TvInputManager>() {
784 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600785 public TvInputManager createService() throws ServiceNotFoundException {
786 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800787 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
788 return new TvInputManager(service, UserHandle.myUserId());
789 }});
790
791 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
792 new CachedServiceFetcher<NetworkScoreManager>() {
793 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600794 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800795 return new NetworkScoreManager(ctx);
796 }});
797
798 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
799 new CachedServiceFetcher<UsageStatsManager>() {
800 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600801 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
802 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800803 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
804 return new UsageStatsManager(ctx.getOuterContext(), service);
805 }});
806
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000807 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
808 new CachedServiceFetcher<NetworkStatsManager>() {
809 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600810 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000811 return new NetworkStatsManager(ctx.getOuterContext());
812 }});
813
Jeff Brown6e539312015-02-24 18:53:21 -0800814 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
815 new StaticServiceFetcher<JobScheduler>() {
816 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600817 public JobScheduler createService() throws ServiceNotFoundException {
818 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800819 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
820 }});
821
822 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
823 new StaticServiceFetcher<PersistentDataBlockManager>() {
824 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600825 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
826 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800827 IPersistentDataBlockService persistentDataBlockService =
828 IPersistentDataBlockService.Stub.asInterface(b);
829 if (persistentDataBlockService != null) {
830 return new PersistentDataBlockManager(persistentDataBlockService);
831 } else {
832 // not supported
833 return null;
834 }
835 }});
836
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000837 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
838 new StaticServiceFetcher<OemLockManager>() {
839 @Override
840 public OemLockManager createService() throws ServiceNotFoundException {
841 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
842 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
843 if (oemLockService != null) {
844 return new OemLockManager(oemLockService);
845 } else {
846 // not supported
847 return null;
848 }
849 }});
850
Jeff Brown6e539312015-02-24 18:53:21 -0800851 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
852 new CachedServiceFetcher<MediaProjectionManager>() {
853 @Override
854 public MediaProjectionManager createService(ContextImpl ctx) {
855 return new MediaProjectionManager(ctx);
856 }});
857
858 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
859 new CachedServiceFetcher<AppWidgetManager>() {
860 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600861 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
862 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800863 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
864 }});
865
866 registerService(Context.MIDI_SERVICE, MidiManager.class,
867 new CachedServiceFetcher<MidiManager>() {
868 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600869 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
870 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -0700871 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800872 }});
Eric Laurent2035ac82015-03-05 15:18:44 -0800873
874 registerService(Context.RADIO_SERVICE, RadioManager.class,
875 new CachedServiceFetcher<RadioManager>() {
876 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700877 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -0800878 return new RadioManager(ctx);
879 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800880
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100881 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
882 new CachedServiceFetcher<HardwarePropertiesManager>() {
883 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600884 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
885 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
886 IHardwarePropertiesManager service =
887 IHardwarePropertiesManager.Stub.asInterface(b);
888 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100889 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800890
891 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
892 new CachedServiceFetcher<SoundTriggerManager>() {
893 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600894 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
895 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800896 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
897 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800898
899 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
900 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -0800901 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600902 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
903 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
904 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -0800905 }});
Joe Onorato713fec82016-03-04 10:34:02 -0800906
Ricky Wai1a6e6672017-10-27 14:46:01 +0100907 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
908 new CachedServiceFetcher<NetworkWatchlistManager>() {
909 @Override
910 public NetworkWatchlistManager createService(ContextImpl ctx)
911 throws ServiceNotFoundException {
912 IBinder b =
913 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
914 return new NetworkWatchlistManager(ctx,
915 INetworkWatchlistManager.Stub.asInterface(b));
916 }});
917
Joe Onorato713fec82016-03-04 10:34:02 -0800918 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
919 new CachedServiceFetcher<SystemHealthManager>() {
920 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600921 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
922 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
923 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -0800924 }});
Peng Xu9ff7d222016-02-11 13:02:05 -0800925
926 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
927 new CachedServiceFetcher<ContextHubManager>() {
928 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600929 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -0800930 return new ContextHubManager(ctx.getOuterContext(),
931 ctx.mMainThread.getHandler().getLooper());
932 }});
Joe Onorato1754d742016-11-21 17:51:35 -0800933
934 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
935 new CachedServiceFetcher<IncidentManager>() {
936 @Override
937 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
938 return new IncidentManager(ctx);
939 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -0800940
Felipe Leme640f30a2017-03-06 15:44:06 -0800941 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
942 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -0800943 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800944 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -0700945 // Get the services without throwing as this is an optional feature
946 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +0000947 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -0800948 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -0800949 }});
Zak Cohen56345f42017-01-26 13:54:28 -0800950
951 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
952 @Override
953 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
954 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
955 return new VrManager(IVrManager.Stub.asInterface(b));
956 }
957 });
Neil Fuller328532a2017-03-16 18:32:21 +0000958
959 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
960 new CachedServiceFetcher<RulesManager>() {
961 @Override
962 public RulesManager createService(ContextImpl ctx) {
963 return new RulesManager(ctx.getOuterContext());
964 }});
Tony Mak1b708e62017-10-12 10:59:11 +0100965
966 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
967 new CachedServiceFetcher<CrossProfileApps>() {
968 @Override
969 public CrossProfileApps createService(ContextImpl ctx)
970 throws ServiceNotFoundException {
971 IBinder b = ServiceManager.getServiceOrThrow(
972 Context.CROSS_PROFILE_APPS_SERVICE);
973 return new CrossProfileApps(ctx.getOuterContext(),
974 ICrossProfileApps.Stub.asInterface(b));
975 }
976 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -0400977
978 registerService(Context.SLICE_SERVICE, SliceManager.class,
979 new CachedServiceFetcher<SliceManager>() {
980 @Override
981 public SliceManager createService(ContextImpl ctx)
982 throws ServiceNotFoundException {
983 return new SliceManager(ctx.getOuterContext(),
984 ctx.mMainThread.getHandler());
985 }
986 });
Jeff Brown6e539312015-02-24 18:53:21 -0800987 }
988
989 /**
990 * Creates an array which is used to cache per-Context service instances.
991 */
992 public static Object[] createServiceCache() {
993 return new Object[sServiceCacheSize];
994 }
995
996 /**
997 * Gets a system service from a given context.
998 */
999 public static Object getSystemService(ContextImpl ctx, String name) {
1000 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1001 return fetcher != null ? fetcher.getService(ctx) : null;
1002 }
1003
1004 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001005 * Gets the name of the system-level service that is represented by the specified class.
Jeff Brown6e539312015-02-24 18:53:21 -08001006 */
1007 public static String getSystemServiceName(Class<?> serviceClass) {
1008 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1009 }
1010
1011 /**
1012 * Statically registers a system service with the context.
1013 * This method must be called during static initialization only.
1014 */
1015 private static <T> void registerService(String serviceName, Class<T> serviceClass,
1016 ServiceFetcher<T> serviceFetcher) {
1017 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1018 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1019 }
1020
1021 /**
1022 * Base interface for classes that fetch services.
1023 * These objects must only be created during static initialization.
1024 */
1025 static abstract interface ServiceFetcher<T> {
1026 T getService(ContextImpl ctx);
1027 }
1028
1029 /**
1030 * Override this class when the system service constructor needs a
1031 * ContextImpl and should be cached and retained by that context.
1032 */
1033 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1034 private final int mCacheIndex;
1035
1036 public CachedServiceFetcher() {
1037 mCacheIndex = sServiceCacheSize++;
1038 }
1039
1040 @Override
1041 @SuppressWarnings("unchecked")
1042 public final T getService(ContextImpl ctx) {
1043 final Object[] cache = ctx.mServiceCache;
1044 synchronized (cache) {
1045 // Fetch or create the service.
1046 Object service = cache[mCacheIndex];
1047 if (service == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001048 try {
1049 service = createService(ctx);
1050 cache[mCacheIndex] = service;
1051 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001052 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001053 }
Jeff Brown6e539312015-02-24 18:53:21 -08001054 }
1055 return (T)service;
1056 }
1057 }
1058
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001059 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001060 }
1061
1062 /**
1063 * Override this class when the system service does not need a ContextImpl
1064 * and should be cached and retained process-wide.
1065 */
1066 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1067 private T mCachedInstance;
1068
1069 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001070 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001071 synchronized (StaticServiceFetcher.this) {
1072 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001073 try {
1074 mCachedInstance = createService();
1075 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001076 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001077 }
Jeff Brown6e539312015-02-24 18:53:21 -08001078 }
1079 return mCachedInstance;
1080 }
1081 }
1082
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001083 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001084 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001085
1086 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001087 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1088 * creating the service for the first time, passes it the application context of the creating
1089 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001090 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001091 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1092 * case where multiple application components each have their own ConnectivityManager object.
1093 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001094 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001095 private T mCachedInstance;
1096
1097 @Override
1098 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001099 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001100 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001101 Context appContext = ctx.getApplicationContext();
1102 // If the application context is null, we're either in the system process or
1103 // it's the application context very early in app initialization. In both these
1104 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1105 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001106 try {
1107 mCachedInstance = createService(appContext != null ? appContext : ctx);
1108 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001109 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001110 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001111 }
1112 return mCachedInstance;
1113 }
1114 }
1115
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001116 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001117 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001118
1119 public static void onServiceNotFound(ServiceNotFoundException e) {
1120 // We're mostly interested in tracking down long-lived core system
1121 // components that might stumble if they obtain bad references; just
1122 // emit a tidy log message for normal apps
1123 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1124 Log.wtf(TAG, e.getMessage(), e);
1125 } else {
1126 Log.w(TAG, e.getMessage());
1127 }
1128 }
Jeff Brown6e539312015-02-24 18:53:21 -08001129}