blob: fdda85d5122a143bc48d4fc1e15ac13a57977273 [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 Onukid67070e2018-03-30 12:39:14 -070021import android.app.ContextImpl.ServiceInitializationState;
Jeff Brown6e539312015-02-24 18:53:21 -080022import android.app.admin.DevicePolicyManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060023import android.app.admin.IDevicePolicyManager;
Jeff Brown6e539312015-02-24 18:53:21 -080024import android.app.job.IJobScheduler;
25import android.app.job.JobScheduler;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040026import android.app.slice.SliceManager;
Neil Fullerb5579072018-05-30 14:35:24 +010027import android.app.timedetector.TimeDetector;
Neil Fuller328532a2017-03-16 18:32:21 +000028import android.app.timezone.RulesManager;
Neil Fullercccc48d2018-06-19 12:53:47 +010029import android.app.timezonedetector.TimeZoneDetector;
Jeff Brown6e539312015-02-24 18:53:21 -080030import android.app.trust.TrustManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070031import android.app.usage.IStorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080032import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000033import android.app.usage.NetworkStatsManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070034import android.app.usage.StorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080035import android.app.usage.UsageStatsManager;
36import android.appwidget.AppWidgetManager;
37import android.bluetooth.BluetoothManager;
Eugene Susla6ed45d82017-01-22 13:52:51 -080038import android.companion.CompanionDeviceManager;
39import android.companion.ICompanionDeviceManager;
Jeff Brown6e539312015-02-24 18:53:21 -080040import android.content.ClipboardManager;
41import android.content.Context;
42import android.content.IRestrictionsManager;
43import android.content.RestrictionsManager;
Tony Makb0d22622018-01-18 12:49:49 +000044import android.content.pm.CrossProfileApps;
45import android.content.pm.ICrossProfileApps;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080046import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080047import android.content.pm.LauncherApps;
Svet Ganovec7daa52017-06-30 12:02:59 -070048import android.content.pm.PackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080049import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080050import android.content.res.Resources;
51import android.hardware.ConsumerIrManager;
52import android.hardware.ISerialManager;
53import android.hardware.SensorManager;
54import android.hardware.SerialManager;
55import android.hardware.SystemSensorManager;
56import android.hardware.camera2.CameraManager;
57import android.hardware.display.DisplayManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060058import android.hardware.fingerprint.FingerprintManager;
59import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -080060import android.hardware.hdmi.HdmiControlManager;
61import android.hardware.hdmi.IHdmiControlService;
62import android.hardware.input.InputManager;
Peng Xu9ff7d222016-02-11 13:02:05 -080063import android.hardware.location.ContextHubManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060064import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080065import android.hardware.usb.IUsbManager;
66import android.hardware.usb.UsbManager;
67import android.location.CountryDetector;
68import android.location.ICountryDetector;
69import android.location.ILocationManager;
70import android.location.LocationManager;
71import android.media.AudioManager;
72import android.media.MediaRouter;
73import android.media.midi.IMidiManager;
74import android.media.midi.MidiManager;
75import android.media.projection.MediaProjectionManager;
76import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080077import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -080078import android.media.tv.ITvInputManager;
79import android.media.tv.TvInputManager;
80import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -070081import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -080082import android.net.EthernetManager;
83import android.net.IConnectivityManager;
84import android.net.IEthernetManager;
Nathan Harold28084d82017-03-01 18:55:06 -080085import android.net.IIpSecService;
Jeff Brown6e539312015-02-24 18:53:21 -080086import android.net.INetworkPolicyManager;
Nathan Harold28084d82017-03-01 18:55:06 -080087import android.net.IpSecManager;
Jeff Brown6e539312015-02-24 18:53:21 -080088import android.net.NetworkPolicyManager;
89import android.net.NetworkScoreManager;
Ricky Wai1a6e6672017-10-27 14:46:01 +010090import android.net.NetworkWatchlistManager;
Robert Quattlebaum87a71042017-05-15 15:45:20 -070091import android.net.lowpan.ILowpanManager;
92import android.net.lowpan.LowpanManager;
Etan Cohen17ba4722017-08-21 10:52:17 -070093import android.net.nsd.INsdManager;
94import android.net.nsd.NsdManager;
Jeff Brown6e539312015-02-24 18:53:21 -080095import android.net.wifi.IWifiManager;
96import android.net.wifi.IWifiScanner;
97import android.net.wifi.RttManager;
98import android.net.wifi.WifiManager;
99import android.net.wifi.WifiScanner;
Etan Cohen04133272016-10-26 11:22:06 -0700100import android.net.wifi.aware.IWifiAwareManager;
101import android.net.wifi.aware.WifiAwareManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800102import android.net.wifi.p2p.IWifiP2pManager;
103import android.net.wifi.p2p.WifiP2pManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700104import android.net.wifi.rtt.IWifiRttManager;
105import android.net.wifi.rtt.WifiRttManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800106import android.nfc.NfcManager;
107import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600108import android.os.BatteryStats;
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600109import android.os.Build;
Philip P. Moltmannf80809f2018-04-04 11:20:44 -0700110import android.os.DeviceIdleManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800111import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100112import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600113import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -0800114import android.os.IBinder;
Philip P. Moltmannf80809f2018-04-04 11:20:44 -0700115import android.os.IDeviceIdleController;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100116import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800117import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800118import android.os.IRecoverySystem;
Tao Bao07342dc2017-01-24 15:08:21 -0800119import android.os.ISystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800120import android.os.IUserManager;
Joe Onorato1754d742016-11-21 17:51:35 -0800121import android.os.IncidentManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800122import android.os.PowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800123import android.os.RecoverySystem;
Jeff Brown6e539312015-02-24 18:53:21 -0800124import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600125import android.os.ServiceManager.ServiceNotFoundException;
Tao Bao07342dc2017-01-24 15:08:21 -0800126import android.os.SystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800127import android.os.SystemVibrator;
128import android.os.UserHandle;
129import android.os.UserManager;
130import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800131import android.os.health.SystemHealthManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800132import android.os.storage.StorageManager;
133import android.print.IPrintManager;
134import android.print.PrintManager;
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000135import android.service.oemlock.IOemLockService;
136import android.service.oemlock.OemLockManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800137import android.service.persistentdata.IPersistentDataBlockService;
138import android.service.persistentdata.PersistentDataBlockManager;
Zak Cohen56345f42017-01-26 13:54:28 -0800139import android.service.vr.IVrManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800140import android.telecom.TelecomManager;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800141import android.telephony.CarrierConfigManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800142import android.telephony.SubscriptionManager;
143import android.telephony.TelephonyManager;
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800144import android.telephony.euicc.EuiccCardManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100145import android.telephony.euicc.EuiccManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800146import android.util.Log;
147import android.view.ContextThemeWrapper;
148import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800149import android.view.WindowManager;
150import android.view.WindowManagerImpl;
151import android.view.accessibility.AccessibilityManager;
152import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100153import android.view.autofill.AutofillManager;
154import android.view.autofill.IAutoFillManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800155import android.view.inputmethod.InputMethodManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000156import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800157import android.view.textservice.TextServicesManager;
158
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600159import com.android.internal.app.IAppOpsService;
160import com.android.internal.app.IBatteryStats;
161import com.android.internal.app.ISoundTriggerService;
162import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100163import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600164import com.android.internal.os.IDropBoxManagerService;
165import com.android.internal.policy.PhoneLayoutInflater;
166
Jeff Brown6e539312015-02-24 18:53:21 -0800167import java.util.HashMap;
168
169/**
170 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
171 * Used by {@link ContextImpl}.
172 */
173final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600174 private static final String TAG = "SystemServiceRegistry";
175
Jeff Brown6e539312015-02-24 18:53:21 -0800176 // Service registry information.
177 // This information is never changed once static initialization has completed.
178 private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
179 new HashMap<Class<?>, String>();
180 private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
181 new HashMap<String, ServiceFetcher<?>>();
182 private static int sServiceCacheSize;
183
184 // Not instantiable.
185 private SystemServiceRegistry() { }
186
187 static {
188 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
189 new CachedServiceFetcher<AccessibilityManager>() {
190 @Override
191 public AccessibilityManager createService(ContextImpl ctx) {
192 return AccessibilityManager.getInstance(ctx);
193 }});
194
195 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
196 new CachedServiceFetcher<CaptioningManager>() {
197 @Override
198 public CaptioningManager createService(ContextImpl ctx) {
199 return new CaptioningManager(ctx);
200 }});
201
202 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
203 new CachedServiceFetcher<AccountManager>() {
204 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600205 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
206 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800207 IAccountManager service = IAccountManager.Stub.asInterface(b);
208 return new AccountManager(ctx, service);
209 }});
210
211 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
212 new CachedServiceFetcher<ActivityManager>() {
213 @Override
214 public ActivityManager createService(ContextImpl ctx) {
215 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
216 }});
217
218 registerService(Context.ALARM_SERVICE, AlarmManager.class,
219 new CachedServiceFetcher<AlarmManager>() {
220 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600221 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
222 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800223 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
224 return new AlarmManager(service, ctx);
225 }});
226
227 registerService(Context.AUDIO_SERVICE, AudioManager.class,
228 new CachedServiceFetcher<AudioManager>() {
229 @Override
230 public AudioManager createService(ContextImpl ctx) {
231 return new AudioManager(ctx);
232 }});
233
234 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
235 new CachedServiceFetcher<MediaRouter>() {
236 @Override
237 public MediaRouter createService(ContextImpl ctx) {
238 return new MediaRouter(ctx);
239 }});
240
241 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
242 new CachedServiceFetcher<BluetoothManager>() {
243 @Override
244 public BluetoothManager createService(ContextImpl ctx) {
245 return new BluetoothManager(ctx);
246 }});
247
248 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
249 new StaticServiceFetcher<HdmiControlManager>() {
250 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600251 public HdmiControlManager createService() throws ServiceNotFoundException {
252 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800253 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
254 }});
255
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000256 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000257 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000258 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000259 public TextClassificationManager createService(ContextImpl ctx) {
260 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000261 }});
262
Jeff Brown6e539312015-02-24 18:53:21 -0800263 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
264 new CachedServiceFetcher<ClipboardManager>() {
265 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600266 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800267 return new ClipboardManager(ctx.getOuterContext(),
268 ctx.mMainThread.getHandler());
269 }});
270
271 // The clipboard service moved to a new package. If someone asks for the old
272 // interface by class then we want to redirect over to the new interface instead
273 // (which extends it).
274 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
275
276 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900277 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800278 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600279 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
280 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900281 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
282 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800283 }});
284
Nathan Harold28084d82017-03-01 18:55:06 -0800285 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Nathan Harold592dadb2018-03-15 18:06:06 -0700286 new CachedServiceFetcher<IpSecManager>() {
Nathan Harold28084d82017-03-01 18:55:06 -0800287 @Override
Nathan Harold592dadb2018-03-15 18:06:06 -0700288 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nathan Harold28084d82017-03-01 18:55:06 -0800289 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
290 IIpSecService service = IIpSecService.Stub.asInterface(b);
Nathan Harold592dadb2018-03-15 18:06:06 -0700291 return new IpSecManager(ctx, service);
Nathan Harold28084d82017-03-01 18:55:06 -0800292 }});
293
Jeff Brown6e539312015-02-24 18:53:21 -0800294 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
295 new StaticServiceFetcher<CountryDetector>() {
296 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600297 public CountryDetector createService() throws ServiceNotFoundException {
298 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800299 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
300 }});
301
302 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
303 new CachedServiceFetcher<DevicePolicyManager>() {
304 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600305 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
306 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
307 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800308 }});
309
310 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
311 new CachedServiceFetcher<DownloadManager>() {
312 @Override
313 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700314 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800315 }});
316
317 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700318 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800319 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700320 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600321 IBatteryStats stats = IBatteryStats.Stub.asInterface(
322 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
323 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
324 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700325 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800326 }});
327
328 registerService(Context.NFC_SERVICE, NfcManager.class,
329 new CachedServiceFetcher<NfcManager>() {
330 @Override
331 public NfcManager createService(ContextImpl ctx) {
332 return new NfcManager(ctx);
333 }});
334
335 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700336 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800337 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600338 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
339 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800340 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700341 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800342 }});
343
344 registerService(Context.INPUT_SERVICE, InputManager.class,
345 new StaticServiceFetcher<InputManager>() {
346 @Override
347 public InputManager createService() {
348 return InputManager.getInstance();
349 }});
350
351 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
352 new CachedServiceFetcher<DisplayManager>() {
353 @Override
354 public DisplayManager createService(ContextImpl ctx) {
355 return new DisplayManager(ctx.getOuterContext());
356 }});
357
358 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
359 new StaticServiceFetcher<InputMethodManager>() {
360 @Override
361 public InputMethodManager createService() {
362 return InputMethodManager.getInstance();
363 }});
364
365 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
366 new StaticServiceFetcher<TextServicesManager>() {
367 @Override
368 public TextServicesManager createService() {
369 return TextServicesManager.getInstance();
370 }});
371
372 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800373 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800374 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800375 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800376 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800377 }});
378
379 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
380 new CachedServiceFetcher<LayoutInflater>() {
381 @Override
382 public LayoutInflater createService(ContextImpl ctx) {
383 return new PhoneLayoutInflater(ctx.getOuterContext());
384 }});
385
386 registerService(Context.LOCATION_SERVICE, LocationManager.class,
387 new CachedServiceFetcher<LocationManager>() {
388 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600389 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
390 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800391 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
392 }});
393
394 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700395 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800396 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600397 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700398 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600399 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800400 }});
401
402 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
403 new CachedServiceFetcher<NotificationManager>() {
404 @Override
405 public NotificationManager createService(ContextImpl ctx) {
406 final Context outerContext = ctx.getOuterContext();
407 return new NotificationManager(
408 new ContextThemeWrapper(outerContext,
409 Resources.selectSystemTheme(0,
410 outerContext.getApplicationInfo().targetSdkVersion,
411 com.android.internal.R.style.Theme_Dialog,
412 com.android.internal.R.style.Theme_Holo_Dialog,
413 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
414 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
415 ctx.mMainThread.getHandler());
416 }});
417
418 registerService(Context.NSD_SERVICE, NsdManager.class,
419 new CachedServiceFetcher<NsdManager>() {
420 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600421 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
422 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800423 INsdManager service = INsdManager.Stub.asInterface(b);
424 return new NsdManager(ctx.getOuterContext(), service);
425 }});
426
427 registerService(Context.POWER_SERVICE, PowerManager.class,
428 new CachedServiceFetcher<PowerManager>() {
429 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600430 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
431 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800432 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800433 return new PowerManager(ctx.getOuterContext(),
434 service, ctx.mMainThread.getHandler());
435 }});
436
Tao Baoe8a403d2015-12-31 07:44:55 -0800437 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
438 new CachedServiceFetcher<RecoverySystem>() {
439 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600440 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
441 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800442 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800443 return new RecoverySystem(service);
444 }});
445
Jeff Brown6e539312015-02-24 18:53:21 -0800446 registerService(Context.SEARCH_SERVICE, SearchManager.class,
447 new CachedServiceFetcher<SearchManager>() {
448 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600449 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800450 return new SearchManager(ctx.getOuterContext(),
451 ctx.mMainThread.getHandler());
452 }});
453
454 registerService(Context.SENSOR_SERVICE, SensorManager.class,
455 new CachedServiceFetcher<SensorManager>() {
456 @Override
457 public SensorManager createService(ContextImpl ctx) {
458 return new SystemSensorManager(ctx.getOuterContext(),
459 ctx.mMainThread.getHandler().getLooper());
460 }});
461
David Chenadaf8b32017-11-03 15:42:08 -0700462 registerService(Context.STATS_MANAGER, StatsManager.class,
Jeff Sharkey6b649252018-04-16 09:50:22 -0600463 new CachedServiceFetcher<StatsManager>() {
464 @Override
465 public StatsManager createService(ContextImpl ctx) {
466 return new StatsManager(ctx.getOuterContext());
467 }});
David Chenadaf8b32017-11-03 15:42:08 -0700468
Jeff Brown6e539312015-02-24 18:53:21 -0800469 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
470 new CachedServiceFetcher<StatusBarManager>() {
471 @Override
472 public StatusBarManager createService(ContextImpl ctx) {
473 return new StatusBarManager(ctx.getOuterContext());
474 }});
475
476 registerService(Context.STORAGE_SERVICE, StorageManager.class,
477 new CachedServiceFetcher<StorageManager>() {
478 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600479 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700480 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800481 }});
482
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700483 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
484 new CachedServiceFetcher<StorageStatsManager>() {
485 @Override
486 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
487 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
488 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
489 return new StorageStatsManager(ctx, service);
490 }});
491
Tao Bao07342dc2017-01-24 15:08:21 -0800492 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
493 new CachedServiceFetcher<SystemUpdateManager>() {
494 @Override
495 public SystemUpdateManager createService(ContextImpl ctx)
496 throws ServiceNotFoundException {
497 IBinder b = ServiceManager.getServiceOrThrow(
498 Context.SYSTEM_UPDATE_SERVICE);
499 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
500 return new SystemUpdateManager(service);
501 }});
502
Jeff Brown6e539312015-02-24 18:53:21 -0800503 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
504 new CachedServiceFetcher<TelephonyManager>() {
505 @Override
506 public TelephonyManager createService(ContextImpl ctx) {
507 return new TelephonyManager(ctx.getOuterContext());
508 }});
509
510 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
511 new CachedServiceFetcher<SubscriptionManager>() {
512 @Override
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700513 public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800514 return new SubscriptionManager(ctx.getOuterContext());
515 }});
516
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800517 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
518 new CachedServiceFetcher<CarrierConfigManager>() {
519 @Override
520 public CarrierConfigManager createService(ContextImpl ctx) {
Malcolm Chen9afe59b2018-02-28 15:20:45 -0800521 return new CarrierConfigManager(ctx.getOuterContext());
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800522 }});
523
Jeff Brown6e539312015-02-24 18:53:21 -0800524 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
525 new CachedServiceFetcher<TelecomManager>() {
526 @Override
527 public TelecomManager createService(ContextImpl ctx) {
528 return new TelecomManager(ctx.getOuterContext());
529 }});
530
Jeff Davidson35cda392017-02-27 09:46:00 -0800531 registerService(Context.EUICC_SERVICE, EuiccManager.class,
532 new CachedServiceFetcher<EuiccManager>() {
533 @Override
534 public EuiccManager createService(ContextImpl ctx) {
535 return new EuiccManager(ctx.getOuterContext());
536 }});
537
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800538 registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class,
539 new CachedServiceFetcher<EuiccCardManager>() {
540 @Override
541 public EuiccCardManager createService(ContextImpl ctx) {
542 return new EuiccCardManager(ctx.getOuterContext());
543 }});
544
Jeff Brown6e539312015-02-24 18:53:21 -0800545 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
546 new CachedServiceFetcher<UiModeManager>() {
547 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600548 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800549 return new UiModeManager();
550 }});
551
552 registerService(Context.USB_SERVICE, UsbManager.class,
553 new CachedServiceFetcher<UsbManager>() {
554 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600555 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
556 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800557 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
558 }});
559
560 registerService(Context.SERIAL_SERVICE, SerialManager.class,
561 new CachedServiceFetcher<SerialManager>() {
562 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600563 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
564 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800565 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
566 }});
567
568 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
569 new CachedServiceFetcher<Vibrator>() {
570 @Override
571 public Vibrator createService(ContextImpl ctx) {
572 return new SystemVibrator(ctx);
573 }});
574
575 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
576 new CachedServiceFetcher<WallpaperManager>() {
577 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800578 public WallpaperManager createService(ContextImpl ctx)
579 throws ServiceNotFoundException {
580 final IBinder b;
581 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
582 b = ServiceManager.getServiceOrThrow(Context.WALLPAPER_SERVICE);
583 } else {
584 b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
585 }
586 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
587 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800588 ctx.mMainThread.getHandler());
589 }});
590
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700591 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
592 new CachedServiceFetcher<LowpanManager>() {
593 @Override
594 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
595 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
596 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
597 return new LowpanManager(ctx.getOuterContext(), service,
598 ConnectivityThread.getInstanceLooper());
599 }});
600
Jeff Brown6e539312015-02-24 18:53:21 -0800601 registerService(Context.WIFI_SERVICE, WifiManager.class,
602 new CachedServiceFetcher<WifiManager>() {
603 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600604 public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
605 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800606 IWifiManager service = IWifiManager.Stub.asInterface(b);
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700607 return new WifiManager(ctx.getOuterContext(), service,
608 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800609 }});
610
Jeff Brown6e539312015-02-24 18:53:21 -0800611 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
612 new StaticServiceFetcher<WifiP2pManager>() {
613 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600614 public WifiP2pManager createService() throws ServiceNotFoundException {
615 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800616 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
617 return new WifiP2pManager(service);
618 }});
619
Etan Cohen04133272016-10-26 11:22:06 -0700620 registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
621 new CachedServiceFetcher<WifiAwareManager>() {
Etan Cohen20d329b2015-09-29 13:49:02 -0700622 @Override
Etan Cohen04133272016-10-26 11:22:06 -0700623 public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey589f3092016-11-29 12:54:39 -0700624 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
Etan Cohen04133272016-10-26 11:22:06 -0700625 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
Etan Cohen20d329b2015-09-29 13:49:02 -0700626 if (service == null) {
627 return null;
628 }
Etan Cohen04133272016-10-26 11:22:06 -0700629 return new WifiAwareManager(ctx.getOuterContext(), service);
Etan Cohen20d329b2015-09-29 13:49:02 -0700630 }});
631
Jeff Brown6e539312015-02-24 18:53:21 -0800632 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
633 new CachedServiceFetcher<WifiScanner>() {
634 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600635 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
636 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800637 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
Mitchell Wills91312c72016-04-06 17:03:35 -0700638 return new WifiScanner(ctx.getOuterContext(), service,
639 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800640 }});
641
642 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
643 new CachedServiceFetcher<RttManager>() {
Etan Cohen9accbba2018-01-30 16:41:00 -0800644 @Override
645 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
646 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE);
647 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
648 return new RttManager(ctx.getOuterContext(),
649 new WifiRttManager(ctx.getOuterContext(), service));
650 }});
Jeff Brown6e539312015-02-24 18:53:21 -0800651
Etan Cohen46efb482017-12-07 13:50:57 -0800652 registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class,
Etan Cohen17ba4722017-08-21 10:52:17 -0700653 new CachedServiceFetcher<WifiRttManager>() {
654 @Override
655 public WifiRttManager createService(ContextImpl ctx)
656 throws ServiceNotFoundException {
Etan Cohen46efb482017-12-07 13:50:57 -0800657 IBinder b = ServiceManager.getServiceOrThrow(
658 Context.WIFI_RTT_RANGING_SERVICE);
Etan Cohen17ba4722017-08-21 10:52:17 -0700659 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
660 return new WifiRttManager(ctx.getOuterContext(), service);
661 }});
662
Jeff Brown6e539312015-02-24 18:53:21 -0800663 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
664 new CachedServiceFetcher<EthernetManager>() {
665 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600666 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
667 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800668 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
669 return new EthernetManager(ctx.getOuterContext(), service);
670 }});
671
672 registerService(Context.WINDOW_SERVICE, WindowManager.class,
673 new CachedServiceFetcher<WindowManager>() {
674 @Override
675 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700676 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800677 }});
678
679 registerService(Context.USER_SERVICE, UserManager.class,
680 new CachedServiceFetcher<UserManager>() {
681 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600682 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
683 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800684 IUserManager service = IUserManager.Stub.asInterface(b);
685 return new UserManager(ctx, service);
686 }});
687
688 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
689 new CachedServiceFetcher<AppOpsManager>() {
690 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600691 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
692 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800693 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
694 return new AppOpsManager(ctx, service);
695 }});
696
697 registerService(Context.CAMERA_SERVICE, CameraManager.class,
698 new CachedServiceFetcher<CameraManager>() {
699 @Override
700 public CameraManager createService(ContextImpl ctx) {
701 return new CameraManager(ctx);
702 }});
703
704 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
705 new CachedServiceFetcher<LauncherApps>() {
706 @Override
707 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700708 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800709 }});
710
711 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
712 new CachedServiceFetcher<RestrictionsManager>() {
713 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600714 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
715 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800716 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
717 return new RestrictionsManager(ctx, service);
718 }});
719
720 registerService(Context.PRINT_SERVICE, PrintManager.class,
721 new CachedServiceFetcher<PrintManager>() {
722 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600723 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700724 IPrintManager service = null;
725 // If the feature not present, don't try to look up every time
726 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
727 service = IPrintManager.Stub.asInterface(ServiceManager
728 .getServiceOrThrow(Context.PRINT_SERVICE));
729 }
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700730 final int userId = ctx.getUserId();
731 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
732 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Jeff Brown6e539312015-02-24 18:53:21 -0800733 }});
734
Eugene Susla6ed45d82017-01-22 13:52:51 -0800735 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
736 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700737 @Override
738 public CompanionDeviceManager createService(ContextImpl ctx)
739 throws ServiceNotFoundException {
740 ICompanionDeviceManager service = null;
741 // If the feature not present, don't try to look up every time
742 if (ctx.getPackageManager().hasSystemFeature(
743 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
744 service = ICompanionDeviceManager.Stub.asInterface(
745 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
746 }
747 return new CompanionDeviceManager(service, ctx.getOuterContext());
748 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800749
Jeff Brown6e539312015-02-24 18:53:21 -0800750 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
751 new CachedServiceFetcher<ConsumerIrManager>() {
752 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600753 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800754 return new ConsumerIrManager(ctx);
755 }});
756
757 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
758 new CachedServiceFetcher<MediaSessionManager>() {
759 @Override
760 public MediaSessionManager createService(ContextImpl ctx) {
761 return new MediaSessionManager(ctx);
762 }});
763
764 registerService(Context.TRUST_SERVICE, TrustManager.class,
765 new StaticServiceFetcher<TrustManager>() {
766 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600767 public TrustManager createService() throws ServiceNotFoundException {
768 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800769 return new TrustManager(b);
770 }});
771
772 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
773 new CachedServiceFetcher<FingerprintManager>() {
774 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600775 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600776 final IBinder binder;
777 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
778 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
779 } else {
780 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
781 }
Jeff Brown6e539312015-02-24 18:53:21 -0800782 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
783 return new FingerprintManager(ctx.getOuterContext(), service);
784 }});
785
786 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700787 new CachedServiceFetcher<TvInputManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800788 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700789 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600790 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800791 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700792 return new TvInputManager(service, ctx.getUserId());
Jeff Brown6e539312015-02-24 18:53:21 -0800793 }});
794
795 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
796 new CachedServiceFetcher<NetworkScoreManager>() {
797 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600798 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800799 return new NetworkScoreManager(ctx);
800 }});
801
802 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
803 new CachedServiceFetcher<UsageStatsManager>() {
804 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600805 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
806 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800807 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
808 return new UsageStatsManager(ctx.getOuterContext(), service);
809 }});
810
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000811 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
812 new CachedServiceFetcher<NetworkStatsManager>() {
813 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600814 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000815 return new NetworkStatsManager(ctx.getOuterContext());
816 }});
817
Jeff Brown6e539312015-02-24 18:53:21 -0800818 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
819 new StaticServiceFetcher<JobScheduler>() {
820 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600821 public JobScheduler createService() throws ServiceNotFoundException {
822 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800823 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
824 }});
825
826 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
827 new StaticServiceFetcher<PersistentDataBlockManager>() {
828 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600829 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
830 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800831 IPersistentDataBlockService persistentDataBlockService =
832 IPersistentDataBlockService.Stub.asInterface(b);
833 if (persistentDataBlockService != null) {
834 return new PersistentDataBlockManager(persistentDataBlockService);
835 } else {
836 // not supported
837 return null;
838 }
839 }});
840
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000841 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
842 new StaticServiceFetcher<OemLockManager>() {
843 @Override
844 public OemLockManager createService() throws ServiceNotFoundException {
845 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
846 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
847 if (oemLockService != null) {
848 return new OemLockManager(oemLockService);
849 } else {
850 // not supported
851 return null;
852 }
853 }});
854
Jeff Brown6e539312015-02-24 18:53:21 -0800855 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
856 new CachedServiceFetcher<MediaProjectionManager>() {
857 @Override
858 public MediaProjectionManager createService(ContextImpl ctx) {
859 return new MediaProjectionManager(ctx);
860 }});
861
862 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
863 new CachedServiceFetcher<AppWidgetManager>() {
864 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600865 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
866 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800867 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
868 }});
869
870 registerService(Context.MIDI_SERVICE, MidiManager.class,
871 new CachedServiceFetcher<MidiManager>() {
872 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600873 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
874 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -0700875 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800876 }});
Eric Laurent2035ac82015-03-05 15:18:44 -0800877
878 registerService(Context.RADIO_SERVICE, RadioManager.class,
879 new CachedServiceFetcher<RadioManager>() {
880 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700881 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -0800882 return new RadioManager(ctx);
883 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800884
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100885 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
886 new CachedServiceFetcher<HardwarePropertiesManager>() {
887 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600888 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
889 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
890 IHardwarePropertiesManager service =
891 IHardwarePropertiesManager.Stub.asInterface(b);
892 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100893 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800894
895 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
896 new CachedServiceFetcher<SoundTriggerManager>() {
897 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600898 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
899 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800900 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
901 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800902
903 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
904 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -0800905 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600906 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
907 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
908 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -0800909 }});
Joe Onorato713fec82016-03-04 10:34:02 -0800910
Ricky Wai1a6e6672017-10-27 14:46:01 +0100911 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
912 new CachedServiceFetcher<NetworkWatchlistManager>() {
913 @Override
914 public NetworkWatchlistManager createService(ContextImpl ctx)
915 throws ServiceNotFoundException {
916 IBinder b =
917 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
918 return new NetworkWatchlistManager(ctx,
919 INetworkWatchlistManager.Stub.asInterface(b));
920 }});
921
Joe Onorato713fec82016-03-04 10:34:02 -0800922 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
923 new CachedServiceFetcher<SystemHealthManager>() {
924 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600925 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
926 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
927 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -0800928 }});
Peng Xu9ff7d222016-02-11 13:02:05 -0800929
930 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
931 new CachedServiceFetcher<ContextHubManager>() {
932 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600933 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -0800934 return new ContextHubManager(ctx.getOuterContext(),
935 ctx.mMainThread.getHandler().getLooper());
936 }});
Joe Onorato1754d742016-11-21 17:51:35 -0800937
938 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
939 new CachedServiceFetcher<IncidentManager>() {
940 @Override
941 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
942 return new IncidentManager(ctx);
943 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -0800944
Felipe Leme640f30a2017-03-06 15:44:06 -0800945 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
946 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -0800947 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800948 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -0700949 // Get the services without throwing as this is an optional feature
950 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +0000951 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -0800952 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -0800953 }});
Zak Cohen56345f42017-01-26 13:54:28 -0800954
955 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
956 @Override
957 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
958 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
959 return new VrManager(IVrManager.Stub.asInterface(b));
960 }
961 });
Neil Fuller328532a2017-03-16 18:32:21 +0000962
963 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
964 new CachedServiceFetcher<RulesManager>() {
965 @Override
966 public RulesManager createService(ContextImpl ctx) {
967 return new RulesManager(ctx.getOuterContext());
968 }});
Tony Mak1b708e62017-10-12 10:59:11 +0100969
970 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
971 new CachedServiceFetcher<CrossProfileApps>() {
972 @Override
973 public CrossProfileApps createService(ContextImpl ctx)
974 throws ServiceNotFoundException {
975 IBinder b = ServiceManager.getServiceOrThrow(
976 Context.CROSS_PROFILE_APPS_SERVICE);
977 return new CrossProfileApps(ctx.getOuterContext(),
978 ICrossProfileApps.Stub.asInterface(b));
979 }
980 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -0400981
982 registerService(Context.SLICE_SERVICE, SliceManager.class,
983 new CachedServiceFetcher<SliceManager>() {
984 @Override
985 public SliceManager createService(ContextImpl ctx)
986 throws ServiceNotFoundException {
987 return new SliceManager(ctx.getOuterContext(),
988 ctx.mMainThread.getHandler());
989 }
990 });
Philip P. Moltmannf80809f2018-04-04 11:20:44 -0700991
992 registerService(Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class,
993 new CachedServiceFetcher<DeviceIdleManager>() {
994 @Override
995 public DeviceIdleManager createService(ContextImpl ctx)
996 throws ServiceNotFoundException {
997 IDeviceIdleController service = IDeviceIdleController.Stub.asInterface(
998 ServiceManager.getServiceOrThrow(
999 Context.DEVICE_IDLE_CONTROLLER));
1000 return new DeviceIdleManager(ctx.getOuterContext(), service);
Neil Fuller10e276a2018-06-12 20:10:22 +01001001 }});
Neil Fuller79d42a62018-06-12 14:14:13 +01001002
Neil Fullerb5579072018-05-30 14:35:24 +01001003 registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1004 new CachedServiceFetcher<TimeDetector>() {
1005 @Override
1006 public TimeDetector createService(ContextImpl ctx)
1007 throws ServiceNotFoundException {
1008 return new TimeDetector();
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001009 }});
Neil Fullercccc48d2018-06-19 12:53:47 +01001010 registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class,
1011 new CachedServiceFetcher<TimeZoneDetector>() {
1012 @Override
1013 public TimeZoneDetector createService(ContextImpl ctx)
1014 throws ServiceNotFoundException {
1015 return new TimeZoneDetector();
1016 }});
Jeff Brown6e539312015-02-24 18:53:21 -08001017 }
1018
1019 /**
1020 * Creates an array which is used to cache per-Context service instances.
1021 */
1022 public static Object[] createServiceCache() {
1023 return new Object[sServiceCacheSize];
1024 }
1025
1026 /**
1027 * Gets a system service from a given context.
1028 */
1029 public static Object getSystemService(ContextImpl ctx, String name) {
1030 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1031 return fetcher != null ? fetcher.getService(ctx) : null;
1032 }
1033
1034 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001035 * Gets the name of the system-level service that is represented by the specified class.
Jeff Brown6e539312015-02-24 18:53:21 -08001036 */
1037 public static String getSystemServiceName(Class<?> serviceClass) {
1038 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1039 }
1040
1041 /**
1042 * Statically registers a system service with the context.
1043 * This method must be called during static initialization only.
1044 */
1045 private static <T> void registerService(String serviceName, Class<T> serviceClass,
1046 ServiceFetcher<T> serviceFetcher) {
1047 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1048 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1049 }
1050
1051 /**
1052 * Base interface for classes that fetch services.
1053 * These objects must only be created during static initialization.
1054 */
1055 static abstract interface ServiceFetcher<T> {
1056 T getService(ContextImpl ctx);
1057 }
1058
1059 /**
1060 * Override this class when the system service constructor needs a
1061 * ContextImpl and should be cached and retained by that context.
1062 */
1063 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1064 private final int mCacheIndex;
1065
Makoto Onukid67070e2018-03-30 12:39:14 -07001066 CachedServiceFetcher() {
1067 // Note this class must be instantiated only by the static initializer of the
1068 // outer class (SystemServiceRegistry), which already does the synchronization,
1069 // so bare access to sServiceCacheSize is okay here.
Jeff Brown6e539312015-02-24 18:53:21 -08001070 mCacheIndex = sServiceCacheSize++;
1071 }
1072
1073 @Override
1074 @SuppressWarnings("unchecked")
1075 public final T getService(ContextImpl ctx) {
1076 final Object[] cache = ctx.mServiceCache;
Makoto Onukid67070e2018-03-30 12:39:14 -07001077 final int[] gates = ctx.mServiceInitializationStateArray;
Makoto Onukif896f122018-01-25 09:50:24 -08001078
Makoto Onukid67070e2018-03-30 12:39:14 -07001079 for (;;) {
1080 boolean doInitialize = false;
1081 synchronized (cache) {
1082 // Return it if we already have a cached instance.
1083 T service = (T) cache[mCacheIndex];
1084 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1085 return service;
1086 }
Makoto Onukif896f122018-01-25 09:50:24 -08001087
Makoto Onukid67070e2018-03-30 12:39:14 -07001088 // If we get here, there's no cached instance.
Makoto Onukif896f122018-01-25 09:50:24 -08001089
Makoto Onukid67070e2018-03-30 12:39:14 -07001090 // Grr... if gate is STATE_READY, then this means we initialized the service
1091 // once but someone cleared it.
1092 // We start over from STATE_UNINITIALIZED.
1093 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1094 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1095 }
1096
1097 // It's possible for multiple threads to get here at the same time, so
1098 // use the "gate" to make sure only the first thread will call createService().
1099
1100 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1101 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1102 doInitialize = true;
1103 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1104 }
Makoto Onukif896f122018-01-25 09:50:24 -08001105 }
1106
Makoto Onukid67070e2018-03-30 12:39:14 -07001107 if (doInitialize) {
1108 // Only the first thread gets here.
Makoto Onukif896f122018-01-25 09:50:24 -08001109
Makoto Onukid67070e2018-03-30 12:39:14 -07001110 T service = null;
1111 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001112 try {
Makoto Onukid67070e2018-03-30 12:39:14 -07001113 // This thread is the first one to get here. Instantiate the service
1114 // *without* the cache lock held.
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001115 service = createService(ctx);
Makoto Onukid67070e2018-03-30 12:39:14 -07001116 newState = ContextImpl.STATE_READY;
Makoto Onukif896f122018-01-25 09:50:24 -08001117
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001118 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001119 onServiceNotFound(e);
Makoto Onukif896f122018-01-25 09:50:24 -08001120
Makoto Onukid67070e2018-03-30 12:39:14 -07001121 } finally {
1122 synchronized (cache) {
1123 cache[mCacheIndex] = service;
1124 gates[mCacheIndex] = newState;
1125 cache.notifyAll();
1126 }
1127 }
1128 return service;
1129 }
1130 // The other threads will wait for the first thread to call notifyAll(),
1131 // and go back to the top and retry.
1132 synchronized (cache) {
1133 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1134 try {
1135 cache.wait();
1136 } catch (InterruptedException e) {
1137 Log.w(TAG, "getService() interrupted");
1138 Thread.currentThread().interrupt();
1139 return null;
1140 }
Makoto Onukif896f122018-01-25 09:50:24 -08001141 }
1142 }
Makoto Onukif896f122018-01-25 09:50:24 -08001143 }
Jeff Brown6e539312015-02-24 18:53:21 -08001144 }
1145
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001146 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001147 }
1148
1149 /**
1150 * Override this class when the system service does not need a ContextImpl
1151 * and should be cached and retained process-wide.
1152 */
1153 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1154 private T mCachedInstance;
1155
1156 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001157 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001158 synchronized (StaticServiceFetcher.this) {
1159 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001160 try {
1161 mCachedInstance = createService();
1162 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001163 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001164 }
Jeff Brown6e539312015-02-24 18:53:21 -08001165 }
1166 return mCachedInstance;
1167 }
1168 }
1169
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001170 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001171 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001172
1173 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001174 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1175 * creating the service for the first time, passes it the application context of the creating
1176 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001177 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001178 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1179 * case where multiple application components each have their own ConnectivityManager object.
1180 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001181 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001182 private T mCachedInstance;
1183
1184 @Override
1185 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001186 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001187 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001188 Context appContext = ctx.getApplicationContext();
1189 // If the application context is null, we're either in the system process or
1190 // it's the application context very early in app initialization. In both these
1191 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1192 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001193 try {
1194 mCachedInstance = createService(appContext != null ? appContext : ctx);
1195 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001196 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001197 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001198 }
1199 return mCachedInstance;
1200 }
1201 }
1202
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001203 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001204 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001205
1206 public static void onServiceNotFound(ServiceNotFoundException e) {
1207 // We're mostly interested in tracking down long-lived core system
1208 // components that might stumble if they obtain bad references; just
1209 // emit a tidy log message for normal apps
1210 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1211 Log.wtf(TAG, e.getMessage(), e);
1212 } else {
1213 Log.w(TAG, e.getMessage());
1214 }
1215 }
Jeff Brown6e539312015-02-24 18:53:21 -08001216}