blob: 83c43e7b83e12063fb1ebdd2289b18aa4ed4d794 [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;
Sahin Caliskan12564862018-10-31 13:23:29 -0700146import android.telephony.rcs.RcsManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800147import android.util.Log;
148import android.view.ContextThemeWrapper;
149import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800150import android.view.WindowManager;
151import android.view.WindowManagerImpl;
152import android.view.accessibility.AccessibilityManager;
153import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100154import android.view.autofill.AutofillManager;
155import android.view.autofill.IAutoFillManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800156import android.view.inputmethod.InputMethodManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000157import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800158import android.view.textservice.TextServicesManager;
159
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600160import com.android.internal.app.IAppOpsService;
161import com.android.internal.app.IBatteryStats;
162import com.android.internal.app.ISoundTriggerService;
163import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100164import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600165import com.android.internal.os.IDropBoxManagerService;
166import com.android.internal.policy.PhoneLayoutInflater;
167
Jeff Brown6e539312015-02-24 18:53:21 -0800168import java.util.HashMap;
169
170/**
171 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
172 * Used by {@link ContextImpl}.
173 */
174final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600175 private static final String TAG = "SystemServiceRegistry";
176
Jeff Brown6e539312015-02-24 18:53:21 -0800177 // Service registry information.
178 // This information is never changed once static initialization has completed.
179 private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
180 new HashMap<Class<?>, String>();
181 private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
182 new HashMap<String, ServiceFetcher<?>>();
183 private static int sServiceCacheSize;
184
185 // Not instantiable.
186 private SystemServiceRegistry() { }
187
188 static {
189 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
190 new CachedServiceFetcher<AccessibilityManager>() {
191 @Override
192 public AccessibilityManager createService(ContextImpl ctx) {
193 return AccessibilityManager.getInstance(ctx);
194 }});
195
196 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
197 new CachedServiceFetcher<CaptioningManager>() {
198 @Override
199 public CaptioningManager createService(ContextImpl ctx) {
200 return new CaptioningManager(ctx);
201 }});
202
203 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
204 new CachedServiceFetcher<AccountManager>() {
205 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600206 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
207 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800208 IAccountManager service = IAccountManager.Stub.asInterface(b);
209 return new AccountManager(ctx, service);
210 }});
211
212 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
213 new CachedServiceFetcher<ActivityManager>() {
214 @Override
215 public ActivityManager createService(ContextImpl ctx) {
216 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
217 }});
218
219 registerService(Context.ALARM_SERVICE, AlarmManager.class,
220 new CachedServiceFetcher<AlarmManager>() {
221 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600222 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
223 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800224 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
225 return new AlarmManager(service, ctx);
226 }});
227
228 registerService(Context.AUDIO_SERVICE, AudioManager.class,
229 new CachedServiceFetcher<AudioManager>() {
230 @Override
231 public AudioManager createService(ContextImpl ctx) {
232 return new AudioManager(ctx);
233 }});
234
235 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
236 new CachedServiceFetcher<MediaRouter>() {
237 @Override
238 public MediaRouter createService(ContextImpl ctx) {
239 return new MediaRouter(ctx);
240 }});
241
242 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
243 new CachedServiceFetcher<BluetoothManager>() {
244 @Override
245 public BluetoothManager createService(ContextImpl ctx) {
246 return new BluetoothManager(ctx);
247 }});
248
249 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
250 new StaticServiceFetcher<HdmiControlManager>() {
251 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600252 public HdmiControlManager createService() throws ServiceNotFoundException {
253 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800254 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
255 }});
256
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000257 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000258 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000259 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000260 public TextClassificationManager createService(ContextImpl ctx) {
261 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000262 }});
263
Jeff Brown6e539312015-02-24 18:53:21 -0800264 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
265 new CachedServiceFetcher<ClipboardManager>() {
266 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600267 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800268 return new ClipboardManager(ctx.getOuterContext(),
269 ctx.mMainThread.getHandler());
270 }});
271
272 // The clipboard service moved to a new package. If someone asks for the old
273 // interface by class then we want to redirect over to the new interface instead
274 // (which extends it).
275 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
276
277 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900278 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800279 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600280 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
281 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900282 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
283 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800284 }});
285
Nathan Harold28084d82017-03-01 18:55:06 -0800286 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Nathan Harold592dadb2018-03-15 18:06:06 -0700287 new CachedServiceFetcher<IpSecManager>() {
Nathan Harold28084d82017-03-01 18:55:06 -0800288 @Override
Nathan Harold592dadb2018-03-15 18:06:06 -0700289 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nathan Harold28084d82017-03-01 18:55:06 -0800290 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
291 IIpSecService service = IIpSecService.Stub.asInterface(b);
Nathan Harold592dadb2018-03-15 18:06:06 -0700292 return new IpSecManager(ctx, service);
Nathan Harold28084d82017-03-01 18:55:06 -0800293 }});
294
Jeff Brown6e539312015-02-24 18:53:21 -0800295 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
296 new StaticServiceFetcher<CountryDetector>() {
297 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600298 public CountryDetector createService() throws ServiceNotFoundException {
299 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800300 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
301 }});
302
303 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
304 new CachedServiceFetcher<DevicePolicyManager>() {
305 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600306 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
307 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
308 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800309 }});
310
311 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
312 new CachedServiceFetcher<DownloadManager>() {
313 @Override
314 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700315 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800316 }});
317
318 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700319 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800320 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700321 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600322 IBatteryStats stats = IBatteryStats.Stub.asInterface(
323 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
324 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
325 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700326 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800327 }});
328
329 registerService(Context.NFC_SERVICE, NfcManager.class,
330 new CachedServiceFetcher<NfcManager>() {
331 @Override
332 public NfcManager createService(ContextImpl ctx) {
333 return new NfcManager(ctx);
334 }});
335
336 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700337 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800338 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600339 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
340 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800341 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700342 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800343 }});
344
345 registerService(Context.INPUT_SERVICE, InputManager.class,
346 new StaticServiceFetcher<InputManager>() {
347 @Override
348 public InputManager createService() {
349 return InputManager.getInstance();
350 }});
351
352 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
353 new CachedServiceFetcher<DisplayManager>() {
354 @Override
355 public DisplayManager createService(ContextImpl ctx) {
356 return new DisplayManager(ctx.getOuterContext());
357 }});
358
359 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
360 new StaticServiceFetcher<InputMethodManager>() {
361 @Override
362 public InputMethodManager createService() {
363 return InputMethodManager.getInstance();
364 }});
365
366 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
367 new StaticServiceFetcher<TextServicesManager>() {
368 @Override
369 public TextServicesManager createService() {
370 return TextServicesManager.getInstance();
371 }});
372
373 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800374 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800375 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800376 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800377 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800378 }});
379
380 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
381 new CachedServiceFetcher<LayoutInflater>() {
382 @Override
383 public LayoutInflater createService(ContextImpl ctx) {
384 return new PhoneLayoutInflater(ctx.getOuterContext());
385 }});
386
387 registerService(Context.LOCATION_SERVICE, LocationManager.class,
388 new CachedServiceFetcher<LocationManager>() {
389 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600390 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
391 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800392 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
393 }});
394
395 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700396 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800397 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600398 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700399 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600400 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800401 }});
402
403 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
404 new CachedServiceFetcher<NotificationManager>() {
405 @Override
406 public NotificationManager createService(ContextImpl ctx) {
407 final Context outerContext = ctx.getOuterContext();
408 return new NotificationManager(
409 new ContextThemeWrapper(outerContext,
410 Resources.selectSystemTheme(0,
411 outerContext.getApplicationInfo().targetSdkVersion,
412 com.android.internal.R.style.Theme_Dialog,
413 com.android.internal.R.style.Theme_Holo_Dialog,
414 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
415 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
416 ctx.mMainThread.getHandler());
417 }});
418
419 registerService(Context.NSD_SERVICE, NsdManager.class,
420 new CachedServiceFetcher<NsdManager>() {
421 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600422 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
423 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800424 INsdManager service = INsdManager.Stub.asInterface(b);
425 return new NsdManager(ctx.getOuterContext(), service);
426 }});
427
428 registerService(Context.POWER_SERVICE, PowerManager.class,
429 new CachedServiceFetcher<PowerManager>() {
430 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600431 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
432 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800433 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800434 return new PowerManager(ctx.getOuterContext(),
435 service, ctx.mMainThread.getHandler());
436 }});
437
Tao Baoe8a403d2015-12-31 07:44:55 -0800438 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
439 new CachedServiceFetcher<RecoverySystem>() {
440 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600441 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
442 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800443 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800444 return new RecoverySystem(service);
445 }});
446
Jeff Brown6e539312015-02-24 18:53:21 -0800447 registerService(Context.SEARCH_SERVICE, SearchManager.class,
448 new CachedServiceFetcher<SearchManager>() {
449 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600450 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800451 return new SearchManager(ctx.getOuterContext(),
452 ctx.mMainThread.getHandler());
453 }});
454
455 registerService(Context.SENSOR_SERVICE, SensorManager.class,
456 new CachedServiceFetcher<SensorManager>() {
457 @Override
458 public SensorManager createService(ContextImpl ctx) {
459 return new SystemSensorManager(ctx.getOuterContext(),
460 ctx.mMainThread.getHandler().getLooper());
461 }});
462
David Chenadaf8b32017-11-03 15:42:08 -0700463 registerService(Context.STATS_MANAGER, StatsManager.class,
Jeff Sharkey6b649252018-04-16 09:50:22 -0600464 new CachedServiceFetcher<StatsManager>() {
465 @Override
466 public StatsManager createService(ContextImpl ctx) {
467 return new StatsManager(ctx.getOuterContext());
468 }});
David Chenadaf8b32017-11-03 15:42:08 -0700469
Jeff Brown6e539312015-02-24 18:53:21 -0800470 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
471 new CachedServiceFetcher<StatusBarManager>() {
472 @Override
473 public StatusBarManager createService(ContextImpl ctx) {
474 return new StatusBarManager(ctx.getOuterContext());
475 }});
476
477 registerService(Context.STORAGE_SERVICE, StorageManager.class,
478 new CachedServiceFetcher<StorageManager>() {
479 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600480 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700481 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800482 }});
483
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700484 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
485 new CachedServiceFetcher<StorageStatsManager>() {
486 @Override
487 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
488 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
489 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
490 return new StorageStatsManager(ctx, service);
491 }});
492
Tao Bao07342dc2017-01-24 15:08:21 -0800493 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
494 new CachedServiceFetcher<SystemUpdateManager>() {
495 @Override
496 public SystemUpdateManager createService(ContextImpl ctx)
497 throws ServiceNotFoundException {
498 IBinder b = ServiceManager.getServiceOrThrow(
499 Context.SYSTEM_UPDATE_SERVICE);
500 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
501 return new SystemUpdateManager(service);
502 }});
503
Jeff Brown6e539312015-02-24 18:53:21 -0800504 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
505 new CachedServiceFetcher<TelephonyManager>() {
506 @Override
507 public TelephonyManager createService(ContextImpl ctx) {
508 return new TelephonyManager(ctx.getOuterContext());
509 }});
510
511 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
512 new CachedServiceFetcher<SubscriptionManager>() {
513 @Override
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700514 public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800515 return new SubscriptionManager(ctx.getOuterContext());
516 }});
517
Sahin Caliskan12564862018-10-31 13:23:29 -0700518 registerService(Context.TELEPHONY_RCS_SERVICE, RcsManager.class,
519 new CachedServiceFetcher<RcsManager>() {
520 @Override
521 public RcsManager createService(ContextImpl ctx) {
522 return new RcsManager();
523 }
524 });
525
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800526 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
527 new CachedServiceFetcher<CarrierConfigManager>() {
528 @Override
529 public CarrierConfigManager createService(ContextImpl ctx) {
Malcolm Chen9afe59b2018-02-28 15:20:45 -0800530 return new CarrierConfigManager(ctx.getOuterContext());
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800531 }});
532
Jeff Brown6e539312015-02-24 18:53:21 -0800533 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
534 new CachedServiceFetcher<TelecomManager>() {
535 @Override
536 public TelecomManager createService(ContextImpl ctx) {
537 return new TelecomManager(ctx.getOuterContext());
538 }});
539
Jeff Davidson35cda392017-02-27 09:46:00 -0800540 registerService(Context.EUICC_SERVICE, EuiccManager.class,
541 new CachedServiceFetcher<EuiccManager>() {
542 @Override
543 public EuiccManager createService(ContextImpl ctx) {
544 return new EuiccManager(ctx.getOuterContext());
545 }});
546
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800547 registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class,
548 new CachedServiceFetcher<EuiccCardManager>() {
549 @Override
550 public EuiccCardManager createService(ContextImpl ctx) {
551 return new EuiccCardManager(ctx.getOuterContext());
552 }});
553
Jeff Brown6e539312015-02-24 18:53:21 -0800554 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
555 new CachedServiceFetcher<UiModeManager>() {
556 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600557 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800558 return new UiModeManager();
559 }});
560
561 registerService(Context.USB_SERVICE, UsbManager.class,
562 new CachedServiceFetcher<UsbManager>() {
563 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600564 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
565 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800566 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
567 }});
568
569 registerService(Context.SERIAL_SERVICE, SerialManager.class,
570 new CachedServiceFetcher<SerialManager>() {
571 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600572 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
573 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800574 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
575 }});
576
577 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
578 new CachedServiceFetcher<Vibrator>() {
579 @Override
580 public Vibrator createService(ContextImpl ctx) {
581 return new SystemVibrator(ctx);
582 }});
583
584 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
585 new CachedServiceFetcher<WallpaperManager>() {
586 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800587 public WallpaperManager createService(ContextImpl ctx)
588 throws ServiceNotFoundException {
589 final IBinder b;
590 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
591 b = ServiceManager.getServiceOrThrow(Context.WALLPAPER_SERVICE);
592 } else {
593 b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
594 }
595 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
596 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800597 ctx.mMainThread.getHandler());
598 }});
599
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700600 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
601 new CachedServiceFetcher<LowpanManager>() {
602 @Override
603 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
604 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
605 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
606 return new LowpanManager(ctx.getOuterContext(), service,
607 ConnectivityThread.getInstanceLooper());
608 }});
609
Jeff Brown6e539312015-02-24 18:53:21 -0800610 registerService(Context.WIFI_SERVICE, WifiManager.class,
611 new CachedServiceFetcher<WifiManager>() {
612 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600613 public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
614 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800615 IWifiManager service = IWifiManager.Stub.asInterface(b);
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700616 return new WifiManager(ctx.getOuterContext(), service,
617 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800618 }});
619
Jeff Brown6e539312015-02-24 18:53:21 -0800620 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
621 new StaticServiceFetcher<WifiP2pManager>() {
622 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600623 public WifiP2pManager createService() throws ServiceNotFoundException {
624 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800625 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
626 return new WifiP2pManager(service);
627 }});
628
Etan Cohen04133272016-10-26 11:22:06 -0700629 registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
630 new CachedServiceFetcher<WifiAwareManager>() {
Etan Cohen20d329b2015-09-29 13:49:02 -0700631 @Override
Etan Cohen04133272016-10-26 11:22:06 -0700632 public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey589f3092016-11-29 12:54:39 -0700633 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
Etan Cohen04133272016-10-26 11:22:06 -0700634 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
Etan Cohen20d329b2015-09-29 13:49:02 -0700635 if (service == null) {
636 return null;
637 }
Etan Cohen04133272016-10-26 11:22:06 -0700638 return new WifiAwareManager(ctx.getOuterContext(), service);
Etan Cohen20d329b2015-09-29 13:49:02 -0700639 }});
640
Jeff Brown6e539312015-02-24 18:53:21 -0800641 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
642 new CachedServiceFetcher<WifiScanner>() {
643 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600644 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
645 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800646 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
Mitchell Wills91312c72016-04-06 17:03:35 -0700647 return new WifiScanner(ctx.getOuterContext(), service,
648 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800649 }});
650
651 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
652 new CachedServiceFetcher<RttManager>() {
Etan Cohen9accbba2018-01-30 16:41:00 -0800653 @Override
654 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
655 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE);
656 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
657 return new RttManager(ctx.getOuterContext(),
658 new WifiRttManager(ctx.getOuterContext(), service));
659 }});
Jeff Brown6e539312015-02-24 18:53:21 -0800660
Etan Cohen46efb482017-12-07 13:50:57 -0800661 registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class,
Etan Cohen17ba4722017-08-21 10:52:17 -0700662 new CachedServiceFetcher<WifiRttManager>() {
663 @Override
664 public WifiRttManager createService(ContextImpl ctx)
665 throws ServiceNotFoundException {
Etan Cohen46efb482017-12-07 13:50:57 -0800666 IBinder b = ServiceManager.getServiceOrThrow(
667 Context.WIFI_RTT_RANGING_SERVICE);
Etan Cohen17ba4722017-08-21 10:52:17 -0700668 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
669 return new WifiRttManager(ctx.getOuterContext(), service);
670 }});
671
Jeff Brown6e539312015-02-24 18:53:21 -0800672 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
673 new CachedServiceFetcher<EthernetManager>() {
674 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600675 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
676 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800677 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
678 return new EthernetManager(ctx.getOuterContext(), service);
679 }});
680
681 registerService(Context.WINDOW_SERVICE, WindowManager.class,
682 new CachedServiceFetcher<WindowManager>() {
683 @Override
684 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700685 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800686 }});
687
688 registerService(Context.USER_SERVICE, UserManager.class,
689 new CachedServiceFetcher<UserManager>() {
690 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600691 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
692 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800693 IUserManager service = IUserManager.Stub.asInterface(b);
694 return new UserManager(ctx, service);
695 }});
696
697 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
698 new CachedServiceFetcher<AppOpsManager>() {
699 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600700 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
701 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800702 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
703 return new AppOpsManager(ctx, service);
704 }});
705
706 registerService(Context.CAMERA_SERVICE, CameraManager.class,
707 new CachedServiceFetcher<CameraManager>() {
708 @Override
709 public CameraManager createService(ContextImpl ctx) {
710 return new CameraManager(ctx);
711 }});
712
713 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
714 new CachedServiceFetcher<LauncherApps>() {
715 @Override
716 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700717 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800718 }});
719
720 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
721 new CachedServiceFetcher<RestrictionsManager>() {
722 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600723 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
724 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800725 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
726 return new RestrictionsManager(ctx, service);
727 }});
728
729 registerService(Context.PRINT_SERVICE, PrintManager.class,
730 new CachedServiceFetcher<PrintManager>() {
731 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600732 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700733 IPrintManager service = null;
734 // If the feature not present, don't try to look up every time
735 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
736 service = IPrintManager.Stub.asInterface(ServiceManager
737 .getServiceOrThrow(Context.PRINT_SERVICE));
738 }
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700739 final int userId = ctx.getUserId();
740 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
741 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Jeff Brown6e539312015-02-24 18:53:21 -0800742 }});
743
Eugene Susla6ed45d82017-01-22 13:52:51 -0800744 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
745 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700746 @Override
747 public CompanionDeviceManager createService(ContextImpl ctx)
748 throws ServiceNotFoundException {
749 ICompanionDeviceManager service = null;
750 // If the feature not present, don't try to look up every time
751 if (ctx.getPackageManager().hasSystemFeature(
752 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
753 service = ICompanionDeviceManager.Stub.asInterface(
754 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
755 }
756 return new CompanionDeviceManager(service, ctx.getOuterContext());
757 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800758
Jeff Brown6e539312015-02-24 18:53:21 -0800759 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
760 new CachedServiceFetcher<ConsumerIrManager>() {
761 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600762 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800763 return new ConsumerIrManager(ctx);
764 }});
765
766 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
767 new CachedServiceFetcher<MediaSessionManager>() {
768 @Override
769 public MediaSessionManager createService(ContextImpl ctx) {
770 return new MediaSessionManager(ctx);
771 }});
772
773 registerService(Context.TRUST_SERVICE, TrustManager.class,
774 new StaticServiceFetcher<TrustManager>() {
775 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600776 public TrustManager createService() throws ServiceNotFoundException {
777 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800778 return new TrustManager(b);
779 }});
780
781 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
782 new CachedServiceFetcher<FingerprintManager>() {
783 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600784 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600785 final IBinder binder;
786 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
787 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
788 } else {
789 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
790 }
Jeff Brown6e539312015-02-24 18:53:21 -0800791 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
792 return new FingerprintManager(ctx.getOuterContext(), service);
793 }});
794
795 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700796 new CachedServiceFetcher<TvInputManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800797 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700798 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600799 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800800 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700801 return new TvInputManager(service, ctx.getUserId());
Jeff Brown6e539312015-02-24 18:53:21 -0800802 }});
803
804 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
805 new CachedServiceFetcher<NetworkScoreManager>() {
806 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600807 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800808 return new NetworkScoreManager(ctx);
809 }});
810
811 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
812 new CachedServiceFetcher<UsageStatsManager>() {
813 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600814 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
815 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800816 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
817 return new UsageStatsManager(ctx.getOuterContext(), service);
818 }});
819
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000820 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
821 new CachedServiceFetcher<NetworkStatsManager>() {
822 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600823 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000824 return new NetworkStatsManager(ctx.getOuterContext());
825 }});
826
Jeff Brown6e539312015-02-24 18:53:21 -0800827 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
828 new StaticServiceFetcher<JobScheduler>() {
829 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600830 public JobScheduler createService() throws ServiceNotFoundException {
831 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800832 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
833 }});
834
835 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
836 new StaticServiceFetcher<PersistentDataBlockManager>() {
837 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600838 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
839 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800840 IPersistentDataBlockService persistentDataBlockService =
841 IPersistentDataBlockService.Stub.asInterface(b);
842 if (persistentDataBlockService != null) {
843 return new PersistentDataBlockManager(persistentDataBlockService);
844 } else {
845 // not supported
846 return null;
847 }
848 }});
849
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000850 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
851 new StaticServiceFetcher<OemLockManager>() {
852 @Override
853 public OemLockManager createService() throws ServiceNotFoundException {
854 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
855 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
856 if (oemLockService != null) {
857 return new OemLockManager(oemLockService);
858 } else {
859 // not supported
860 return null;
861 }
862 }});
863
Jeff Brown6e539312015-02-24 18:53:21 -0800864 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
865 new CachedServiceFetcher<MediaProjectionManager>() {
866 @Override
867 public MediaProjectionManager createService(ContextImpl ctx) {
868 return new MediaProjectionManager(ctx);
869 }});
870
871 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
872 new CachedServiceFetcher<AppWidgetManager>() {
873 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600874 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
875 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800876 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
877 }});
878
879 registerService(Context.MIDI_SERVICE, MidiManager.class,
880 new CachedServiceFetcher<MidiManager>() {
881 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600882 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
883 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -0700884 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800885 }});
Eric Laurent2035ac82015-03-05 15:18:44 -0800886
887 registerService(Context.RADIO_SERVICE, RadioManager.class,
888 new CachedServiceFetcher<RadioManager>() {
889 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700890 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -0800891 return new RadioManager(ctx);
892 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800893
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100894 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
895 new CachedServiceFetcher<HardwarePropertiesManager>() {
896 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600897 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
898 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
899 IHardwarePropertiesManager service =
900 IHardwarePropertiesManager.Stub.asInterface(b);
901 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100902 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800903
904 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
905 new CachedServiceFetcher<SoundTriggerManager>() {
906 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600907 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
908 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800909 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
910 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800911
912 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
913 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -0800914 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600915 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
916 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
917 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -0800918 }});
Joe Onorato713fec82016-03-04 10:34:02 -0800919
Ricky Wai1a6e6672017-10-27 14:46:01 +0100920 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
921 new CachedServiceFetcher<NetworkWatchlistManager>() {
922 @Override
923 public NetworkWatchlistManager createService(ContextImpl ctx)
924 throws ServiceNotFoundException {
925 IBinder b =
926 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
927 return new NetworkWatchlistManager(ctx,
928 INetworkWatchlistManager.Stub.asInterface(b));
929 }});
930
Joe Onorato713fec82016-03-04 10:34:02 -0800931 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
932 new CachedServiceFetcher<SystemHealthManager>() {
933 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600934 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
935 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
936 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -0800937 }});
Peng Xu9ff7d222016-02-11 13:02:05 -0800938
939 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
940 new CachedServiceFetcher<ContextHubManager>() {
941 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600942 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -0800943 return new ContextHubManager(ctx.getOuterContext(),
944 ctx.mMainThread.getHandler().getLooper());
945 }});
Joe Onorato1754d742016-11-21 17:51:35 -0800946
947 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
948 new CachedServiceFetcher<IncidentManager>() {
949 @Override
950 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
951 return new IncidentManager(ctx);
952 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -0800953
Felipe Leme640f30a2017-03-06 15:44:06 -0800954 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
955 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -0800956 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -0800957 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -0700958 // Get the services without throwing as this is an optional feature
959 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +0000960 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -0800961 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -0800962 }});
Zak Cohen56345f42017-01-26 13:54:28 -0800963
964 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
965 @Override
966 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
967 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
968 return new VrManager(IVrManager.Stub.asInterface(b));
969 }
970 });
Neil Fuller328532a2017-03-16 18:32:21 +0000971
972 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
973 new CachedServiceFetcher<RulesManager>() {
974 @Override
975 public RulesManager createService(ContextImpl ctx) {
976 return new RulesManager(ctx.getOuterContext());
977 }});
Tony Mak1b708e62017-10-12 10:59:11 +0100978
979 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
980 new CachedServiceFetcher<CrossProfileApps>() {
981 @Override
982 public CrossProfileApps createService(ContextImpl ctx)
983 throws ServiceNotFoundException {
984 IBinder b = ServiceManager.getServiceOrThrow(
985 Context.CROSS_PROFILE_APPS_SERVICE);
986 return new CrossProfileApps(ctx.getOuterContext(),
987 ICrossProfileApps.Stub.asInterface(b));
988 }
989 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -0400990
991 registerService(Context.SLICE_SERVICE, SliceManager.class,
992 new CachedServiceFetcher<SliceManager>() {
993 @Override
994 public SliceManager createService(ContextImpl ctx)
995 throws ServiceNotFoundException {
996 return new SliceManager(ctx.getOuterContext(),
997 ctx.mMainThread.getHandler());
998 }
999 });
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001000
1001 registerService(Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class,
1002 new CachedServiceFetcher<DeviceIdleManager>() {
1003 @Override
1004 public DeviceIdleManager createService(ContextImpl ctx)
1005 throws ServiceNotFoundException {
1006 IDeviceIdleController service = IDeviceIdleController.Stub.asInterface(
1007 ServiceManager.getServiceOrThrow(
1008 Context.DEVICE_IDLE_CONTROLLER));
1009 return new DeviceIdleManager(ctx.getOuterContext(), service);
Neil Fuller10e276a2018-06-12 20:10:22 +01001010 }});
Neil Fuller79d42a62018-06-12 14:14:13 +01001011
Neil Fullerb5579072018-05-30 14:35:24 +01001012 registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1013 new CachedServiceFetcher<TimeDetector>() {
1014 @Override
1015 public TimeDetector createService(ContextImpl ctx)
1016 throws ServiceNotFoundException {
1017 return new TimeDetector();
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001018 }});
Neil Fullercccc48d2018-06-19 12:53:47 +01001019 registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class,
1020 new CachedServiceFetcher<TimeZoneDetector>() {
1021 @Override
1022 public TimeZoneDetector createService(ContextImpl ctx)
1023 throws ServiceNotFoundException {
1024 return new TimeZoneDetector();
1025 }});
Jeff Brown6e539312015-02-24 18:53:21 -08001026 }
1027
1028 /**
1029 * Creates an array which is used to cache per-Context service instances.
1030 */
1031 public static Object[] createServiceCache() {
1032 return new Object[sServiceCacheSize];
1033 }
1034
1035 /**
1036 * Gets a system service from a given context.
1037 */
1038 public static Object getSystemService(ContextImpl ctx, String name) {
1039 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1040 return fetcher != null ? fetcher.getService(ctx) : null;
1041 }
1042
1043 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001044 * Gets the name of the system-level service that is represented by the specified class.
Jeff Brown6e539312015-02-24 18:53:21 -08001045 */
1046 public static String getSystemServiceName(Class<?> serviceClass) {
1047 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1048 }
1049
1050 /**
1051 * Statically registers a system service with the context.
1052 * This method must be called during static initialization only.
1053 */
1054 private static <T> void registerService(String serviceName, Class<T> serviceClass,
1055 ServiceFetcher<T> serviceFetcher) {
1056 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1057 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1058 }
1059
1060 /**
1061 * Base interface for classes that fetch services.
1062 * These objects must only be created during static initialization.
1063 */
1064 static abstract interface ServiceFetcher<T> {
1065 T getService(ContextImpl ctx);
1066 }
1067
1068 /**
1069 * Override this class when the system service constructor needs a
1070 * ContextImpl and should be cached and retained by that context.
1071 */
1072 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1073 private final int mCacheIndex;
1074
Makoto Onukid67070e2018-03-30 12:39:14 -07001075 CachedServiceFetcher() {
1076 // Note this class must be instantiated only by the static initializer of the
1077 // outer class (SystemServiceRegistry), which already does the synchronization,
1078 // so bare access to sServiceCacheSize is okay here.
Jeff Brown6e539312015-02-24 18:53:21 -08001079 mCacheIndex = sServiceCacheSize++;
1080 }
1081
1082 @Override
1083 @SuppressWarnings("unchecked")
1084 public final T getService(ContextImpl ctx) {
1085 final Object[] cache = ctx.mServiceCache;
Makoto Onukid67070e2018-03-30 12:39:14 -07001086 final int[] gates = ctx.mServiceInitializationStateArray;
Makoto Onukif896f122018-01-25 09:50:24 -08001087
Makoto Onukid67070e2018-03-30 12:39:14 -07001088 for (;;) {
1089 boolean doInitialize = false;
1090 synchronized (cache) {
1091 // Return it if we already have a cached instance.
1092 T service = (T) cache[mCacheIndex];
1093 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1094 return service;
1095 }
Makoto Onukif896f122018-01-25 09:50:24 -08001096
Makoto Onukid67070e2018-03-30 12:39:14 -07001097 // If we get here, there's no cached instance.
Makoto Onukif896f122018-01-25 09:50:24 -08001098
Makoto Onukid67070e2018-03-30 12:39:14 -07001099 // Grr... if gate is STATE_READY, then this means we initialized the service
1100 // once but someone cleared it.
1101 // We start over from STATE_UNINITIALIZED.
1102 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1103 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1104 }
1105
1106 // It's possible for multiple threads to get here at the same time, so
1107 // use the "gate" to make sure only the first thread will call createService().
1108
1109 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1110 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1111 doInitialize = true;
1112 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1113 }
Makoto Onukif896f122018-01-25 09:50:24 -08001114 }
1115
Makoto Onukid67070e2018-03-30 12:39:14 -07001116 if (doInitialize) {
1117 // Only the first thread gets here.
Makoto Onukif896f122018-01-25 09:50:24 -08001118
Makoto Onukid67070e2018-03-30 12:39:14 -07001119 T service = null;
1120 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001121 try {
Makoto Onukid67070e2018-03-30 12:39:14 -07001122 // This thread is the first one to get here. Instantiate the service
1123 // *without* the cache lock held.
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001124 service = createService(ctx);
Makoto Onukid67070e2018-03-30 12:39:14 -07001125 newState = ContextImpl.STATE_READY;
Makoto Onukif896f122018-01-25 09:50:24 -08001126
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001127 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001128 onServiceNotFound(e);
Makoto Onukif896f122018-01-25 09:50:24 -08001129
Makoto Onukid67070e2018-03-30 12:39:14 -07001130 } finally {
1131 synchronized (cache) {
1132 cache[mCacheIndex] = service;
1133 gates[mCacheIndex] = newState;
1134 cache.notifyAll();
1135 }
1136 }
1137 return service;
1138 }
1139 // The other threads will wait for the first thread to call notifyAll(),
1140 // and go back to the top and retry.
1141 synchronized (cache) {
1142 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1143 try {
1144 cache.wait();
1145 } catch (InterruptedException e) {
1146 Log.w(TAG, "getService() interrupted");
1147 Thread.currentThread().interrupt();
1148 return null;
1149 }
Makoto Onukif896f122018-01-25 09:50:24 -08001150 }
1151 }
Makoto Onukif896f122018-01-25 09:50:24 -08001152 }
Jeff Brown6e539312015-02-24 18:53:21 -08001153 }
1154
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001155 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001156 }
1157
1158 /**
1159 * Override this class when the system service does not need a ContextImpl
1160 * and should be cached and retained process-wide.
1161 */
1162 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1163 private T mCachedInstance;
1164
1165 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001166 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001167 synchronized (StaticServiceFetcher.this) {
1168 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001169 try {
1170 mCachedInstance = createService();
1171 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001172 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001173 }
Jeff Brown6e539312015-02-24 18:53:21 -08001174 }
1175 return mCachedInstance;
1176 }
1177 }
1178
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001179 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001180 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001181
1182 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001183 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1184 * creating the service for the first time, passes it the application context of the creating
1185 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001186 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001187 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1188 * case where multiple application components each have their own ConnectivityManager object.
1189 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001190 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001191 private T mCachedInstance;
1192
1193 @Override
1194 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001195 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001196 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001197 Context appContext = ctx.getApplicationContext();
1198 // If the application context is null, we're either in the system process or
1199 // it's the application context very early in app initialization. In both these
1200 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1201 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001202 try {
1203 mCachedInstance = createService(appContext != null ? appContext : ctx);
1204 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001205 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001206 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001207 }
1208 return mCachedInstance;
1209 }
1210 }
1211
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001212 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001213 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001214
1215 public static void onServiceNotFound(ServiceNotFoundException e) {
1216 // We're mostly interested in tracking down long-lived core system
1217 // components that might stumble if they obtain bad references; just
1218 // emit a tidy log message for normal apps
1219 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1220 Log.wtf(TAG, e.getMessage(), e);
1221 } else {
1222 Log.w(TAG, e.getMessage());
1223 }
1224 }
Jeff Brown6e539312015-02-24 18:53:21 -08001225}