1 /*
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 
17 package android.app;
18 
19 import com.android.internal.app.IAppOpsService;
20 import com.android.internal.appwidget.IAppWidgetService;
21 import com.android.internal.os.IDropBoxManagerService;
22 
23 import android.accounts.AccountManager;
24 import android.accounts.IAccountManager;
25 import android.app.admin.DevicePolicyManager;
26 import android.app.job.IJobScheduler;
27 import android.app.job.JobScheduler;
28 import android.app.trust.TrustManager;
29 import android.app.usage.IUsageStatsManager;
30 import android.app.usage.NetworkStatsManager;
31 import android.app.usage.UsageStatsManager;
32 import android.appwidget.AppWidgetManager;
33 import android.bluetooth.BluetoothManager;
34 import android.content.ClipboardManager;
35 import android.content.Context;
36 import android.content.IRestrictionsManager;
37 import android.content.RestrictionsManager;
38 import android.content.pm.ILauncherApps;
39 import android.content.pm.LauncherApps;
40 import android.content.res.Resources;
41 import android.hardware.ConsumerIrManager;
42 import android.hardware.ISerialManager;
43 import android.hardware.SensorManager;
44 import android.hardware.SerialManager;
45 import android.hardware.SystemSensorManager;
46 import android.hardware.camera2.CameraManager;
47 import android.hardware.display.DisplayManager;
48 import android.hardware.hdmi.HdmiControlManager;
49 import android.hardware.hdmi.IHdmiControlService;
50 import android.hardware.input.InputManager;
51 import android.hardware.usb.IUsbManager;
52 import android.hardware.usb.UsbManager;
53 import android.hardware.radio.RadioManager;
54 import android.location.CountryDetector;
55 import android.location.ICountryDetector;
56 import android.location.ILocationManager;
57 import android.location.LocationManager;
58 import android.media.AudioManager;
59 import android.media.MediaRouter;
60 import android.media.midi.IMidiManager;
61 import android.media.midi.MidiManager;
62 import android.media.projection.MediaProjectionManager;
63 import android.media.session.MediaSessionManager;
64 import android.media.tv.ITvInputManager;
65 import android.media.tv.TvInputManager;
66 import android.net.ConnectivityManager;
67 import android.net.EthernetManager;
68 import android.net.IConnectivityManager;
69 import android.net.IEthernetManager;
70 import android.net.INetworkPolicyManager;
71 import android.net.NetworkPolicyManager;
72 import android.net.NetworkScoreManager;
73 import android.net.nsd.INsdManager;
74 import android.net.nsd.NsdManager;
75 import android.net.wifi.IRttManager;
76 import android.net.wifi.IWifiManager;
77 import android.net.wifi.IWifiScanner;
78 import android.net.wifi.RttManager;
79 import android.net.wifi.WifiManager;
80 import android.net.wifi.WifiScanner;
81 import android.net.wifi.p2p.IWifiP2pManager;
82 import android.net.wifi.p2p.WifiP2pManager;
83 import android.net.wifi.passpoint.IWifiPasspointManager;
84 import android.net.wifi.passpoint.WifiPasspointManager;
85 import android.nfc.NfcManager;
86 import android.os.BatteryManager;
87 import android.os.DropBoxManager;
88 import android.os.IBinder;
89 import android.os.IPowerManager;
90 import android.os.IUserManager;
91 import android.os.PowerManager;
92 import android.os.Process;
93 import android.os.ServiceManager;
94 import android.os.SystemVibrator;
95 import android.os.UserHandle;
96 import android.os.UserManager;
97 import android.os.Vibrator;
98 import android.os.storage.StorageManager;
99 import android.print.IPrintManager;
100 import android.print.PrintManager;
101 import android.hardware.fingerprint.FingerprintManager;
102 import android.hardware.fingerprint.IFingerprintService;
103 import android.service.persistentdata.IPersistentDataBlockService;
104 import android.service.persistentdata.PersistentDataBlockManager;
105 import android.telecom.TelecomManager;
106 import android.telephony.CarrierConfigManager;
107 import android.telephony.SubscriptionManager;
108 import android.telephony.TelephonyManager;
109 import android.util.Log;
110 import android.view.ContextThemeWrapper;
111 import android.view.LayoutInflater;
112 import com.android.internal.policy.PhoneLayoutInflater;
113 import android.view.WindowManager;
114 import android.view.WindowManagerImpl;
115 import android.view.accessibility.AccessibilityManager;
116 import android.view.accessibility.CaptioningManager;
117 import android.view.inputmethod.InputMethodManager;
118 import android.view.textservice.TextServicesManager;
119 
120 import java.util.HashMap;
121 
122 /**
123  * Manages all of the system services that can be returned by {@link Context#getSystemService}.
124  * Used by {@link ContextImpl}.
125  */
126 final class SystemServiceRegistry {
127     private final static String TAG = "SystemServiceRegistry";
128 
129     // Service registry information.
130     // This information is never changed once static initialization has completed.
131     private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
132             new HashMap<Class<?>, String>();
133     private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
134             new HashMap<String, ServiceFetcher<?>>();
135     private static int sServiceCacheSize;
136 
137     // Not instantiable.
SystemServiceRegistry()138     private SystemServiceRegistry() { }
139 
140     static {
registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class, new CachedServiceFetcher<AccessibilityManager>() { @Override public AccessibilityManager createService(ContextImpl ctx) { return AccessibilityManager.getInstance(ctx); }})141         registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
142                 new CachedServiceFetcher<AccessibilityManager>() {
143             @Override
144             public AccessibilityManager createService(ContextImpl ctx) {
145                 return AccessibilityManager.getInstance(ctx);
146             }});
147 
registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class, new CachedServiceFetcher<CaptioningManager>() { @Override public CaptioningManager createService(ContextImpl ctx) { return new CaptioningManager(ctx); }})148         registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
149                 new CachedServiceFetcher<CaptioningManager>() {
150             @Override
151             public CaptioningManager createService(ContextImpl ctx) {
152                 return new CaptioningManager(ctx);
153             }});
154 
registerService(Context.ACCOUNT_SERVICE, AccountManager.class, new CachedServiceFetcher<AccountManager>() { @Override public AccountManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE); IAccountManager service = IAccountManager.Stub.asInterface(b); return new AccountManager(ctx, service); }})155         registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
156                 new CachedServiceFetcher<AccountManager>() {
157             @Override
158             public AccountManager createService(ContextImpl ctx) {
159                 IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE);
160                 IAccountManager service = IAccountManager.Stub.asInterface(b);
161                 return new AccountManager(ctx, service);
162             }});
163 
registerService(Context.ACTIVITY_SERVICE, ActivityManager.class, new CachedServiceFetcher<ActivityManager>() { @Override public ActivityManager createService(ContextImpl ctx) { return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})164         registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
165                 new CachedServiceFetcher<ActivityManager>() {
166             @Override
167             public ActivityManager createService(ContextImpl ctx) {
168                 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
169             }});
170 
registerService(Context.ALARM_SERVICE, AlarmManager.class, new CachedServiceFetcher<AlarmManager>() { @Override public AlarmManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ALARM_SERVICE); IAlarmManager service = IAlarmManager.Stub.asInterface(b); return new AlarmManager(service, ctx); }})171         registerService(Context.ALARM_SERVICE, AlarmManager.class,
172                 new CachedServiceFetcher<AlarmManager>() {
173             @Override
174             public AlarmManager createService(ContextImpl ctx) {
175                 IBinder b = ServiceManager.getService(Context.ALARM_SERVICE);
176                 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
177                 return new AlarmManager(service, ctx);
178             }});
179 
registerService(Context.AUDIO_SERVICE, AudioManager.class, new CachedServiceFetcher<AudioManager>() { @Override public AudioManager createService(ContextImpl ctx) { return new AudioManager(ctx); }})180         registerService(Context.AUDIO_SERVICE, AudioManager.class,
181                 new CachedServiceFetcher<AudioManager>() {
182             @Override
183             public AudioManager createService(ContextImpl ctx) {
184                 return new AudioManager(ctx);
185             }});
186 
registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class, new CachedServiceFetcher<MediaRouter>() { @Override public MediaRouter createService(ContextImpl ctx) { return new MediaRouter(ctx); }})187         registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
188                 new CachedServiceFetcher<MediaRouter>() {
189             @Override
190             public MediaRouter createService(ContextImpl ctx) {
191                 return new MediaRouter(ctx);
192             }});
193 
registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class, new CachedServiceFetcher<BluetoothManager>() { @Override public BluetoothManager createService(ContextImpl ctx) { return new BluetoothManager(ctx); }})194         registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
195                 new CachedServiceFetcher<BluetoothManager>() {
196             @Override
197             public BluetoothManager createService(ContextImpl ctx) {
198                 return new BluetoothManager(ctx);
199             }});
200 
registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class, new StaticServiceFetcher<HdmiControlManager>() { @Override public HdmiControlManager createService() { IBinder b = ServiceManager.getService(Context.HDMI_CONTROL_SERVICE); return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b)); }})201         registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
202                 new StaticServiceFetcher<HdmiControlManager>() {
203             @Override
204             public HdmiControlManager createService() {
205                 IBinder b = ServiceManager.getService(Context.HDMI_CONTROL_SERVICE);
206                 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
207             }});
208 
registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class, new CachedServiceFetcher<ClipboardManager>() { @Override public ClipboardManager createService(ContextImpl ctx) { return new ClipboardManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})209         registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
210                 new CachedServiceFetcher<ClipboardManager>() {
211             @Override
212             public ClipboardManager createService(ContextImpl ctx) {
213                 return new ClipboardManager(ctx.getOuterContext(),
214                         ctx.mMainThread.getHandler());
215             }});
216 
217         // The clipboard service moved to a new package.  If someone asks for the old
218         // interface by class then we want to redirect over to the new interface instead
219         // (which extends it).
SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE)220         SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
221 
registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class, new StaticOuterContextServiceFetcher<ConnectivityManager>() { @Override public ConnectivityManager createService(Context context) { IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); return new ConnectivityManager(context, service); }})222         registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
223                 new StaticOuterContextServiceFetcher<ConnectivityManager>() {
224             @Override
225             public ConnectivityManager createService(Context context) {
226                 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
227                 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
228                 return new ConnectivityManager(context, service);
229             }});
230 
registerService(Context.COUNTRY_DETECTOR, CountryDetector.class, new StaticServiceFetcher<CountryDetector>() { @Override public CountryDetector createService() { IBinder b = ServiceManager.getService(Context.COUNTRY_DETECTOR); return new CountryDetector(ICountryDetector.Stub.asInterface(b)); }})231         registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
232                 new StaticServiceFetcher<CountryDetector>() {
233             @Override
234             public CountryDetector createService() {
235                 IBinder b = ServiceManager.getService(Context.COUNTRY_DETECTOR);
236                 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
237             }});
238 
registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class, new CachedServiceFetcher<DevicePolicyManager>() { @Override public DevicePolicyManager createService(ContextImpl ctx) { return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler()); }})239         registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
240                 new CachedServiceFetcher<DevicePolicyManager>() {
241             @Override
242             public DevicePolicyManager createService(ContextImpl ctx) {
243                 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
244             }});
245 
registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class, new CachedServiceFetcher<DownloadManager>() { @Override public DownloadManager createService(ContextImpl ctx) { return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName()); }})246         registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
247                 new CachedServiceFetcher<DownloadManager>() {
248             @Override
249             public DownloadManager createService(ContextImpl ctx) {
250                 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
251             }});
252 
registerService(Context.BATTERY_SERVICE, BatteryManager.class, new StaticServiceFetcher<BatteryManager>() { @Override public BatteryManager createService() { return new BatteryManager(); }})253         registerService(Context.BATTERY_SERVICE, BatteryManager.class,
254                 new StaticServiceFetcher<BatteryManager>() {
255             @Override
256             public BatteryManager createService() {
257                 return new BatteryManager();
258             }});
259 
registerService(Context.NFC_SERVICE, NfcManager.class, new CachedServiceFetcher<NfcManager>() { @Override public NfcManager createService(ContextImpl ctx) { return new NfcManager(ctx); }})260         registerService(Context.NFC_SERVICE, NfcManager.class,
261                 new CachedServiceFetcher<NfcManager>() {
262             @Override
263             public NfcManager createService(ContextImpl ctx) {
264                 return new NfcManager(ctx);
265             }});
266 
registerService(Context.DROPBOX_SERVICE, DropBoxManager.class, new StaticServiceFetcher<DropBoxManager>() { @Override public DropBoxManager createService() { IBinder b = ServiceManager.getService(Context.DROPBOX_SERVICE); IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b); if (service == null) { return null; } return new DropBoxManager(service); }})267         registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
268                 new StaticServiceFetcher<DropBoxManager>() {
269             @Override
270             public DropBoxManager createService() {
271                 IBinder b = ServiceManager.getService(Context.DROPBOX_SERVICE);
272                 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
273                 if (service == null) {
274                     // Don't return a DropBoxManager that will NPE upon use.
275                     // This also avoids caching a broken DropBoxManager in
276                     // getDropBoxManager during early boot, before the
277                     // DROPBOX_SERVICE is registered.
278                     return null;
279                 }
280                 return new DropBoxManager(service);
281             }});
282 
registerService(Context.INPUT_SERVICE, InputManager.class, new StaticServiceFetcher<InputManager>() { @Override public InputManager createService() { return InputManager.getInstance(); }})283         registerService(Context.INPUT_SERVICE, InputManager.class,
284                 new StaticServiceFetcher<InputManager>() {
285             @Override
286             public InputManager createService() {
287                 return InputManager.getInstance();
288             }});
289 
registerService(Context.DISPLAY_SERVICE, DisplayManager.class, new CachedServiceFetcher<DisplayManager>() { @Override public DisplayManager createService(ContextImpl ctx) { return new DisplayManager(ctx.getOuterContext()); }})290         registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
291                 new CachedServiceFetcher<DisplayManager>() {
292             @Override
293             public DisplayManager createService(ContextImpl ctx) {
294                 return new DisplayManager(ctx.getOuterContext());
295             }});
296 
registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class, new StaticServiceFetcher<InputMethodManager>() { @Override public InputMethodManager createService() { return InputMethodManager.getInstance(); }})297         registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
298                 new StaticServiceFetcher<InputMethodManager>() {
299             @Override
300             public InputMethodManager createService() {
301                 return InputMethodManager.getInstance();
302             }});
303 
registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class, new StaticServiceFetcher<TextServicesManager>() { @Override public TextServicesManager createService() { return TextServicesManager.getInstance(); }})304         registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
305                 new StaticServiceFetcher<TextServicesManager>() {
306             @Override
307             public TextServicesManager createService() {
308                 return TextServicesManager.getInstance();
309             }});
310 
registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class, new StaticServiceFetcher<KeyguardManager>() { @Override public KeyguardManager createService() { return new KeyguardManager(); }})311         registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
312                 new StaticServiceFetcher<KeyguardManager>() {
313             @Override
314             public KeyguardManager createService() {
315                 return new KeyguardManager();
316             }});
317 
registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class, new CachedServiceFetcher<LayoutInflater>() { @Override public LayoutInflater createService(ContextImpl ctx) { return new PhoneLayoutInflater(ctx.getOuterContext()); }})318         registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
319                 new CachedServiceFetcher<LayoutInflater>() {
320             @Override
321             public LayoutInflater createService(ContextImpl ctx) {
322                 return new PhoneLayoutInflater(ctx.getOuterContext());
323             }});
324 
registerService(Context.LOCATION_SERVICE, LocationManager.class, new CachedServiceFetcher<LocationManager>() { @Override public LocationManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE); return new LocationManager(ctx, ILocationManager.Stub.asInterface(b)); }})325         registerService(Context.LOCATION_SERVICE, LocationManager.class,
326                 new CachedServiceFetcher<LocationManager>() {
327             @Override
328             public LocationManager createService(ContextImpl ctx) {
329                 IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE);
330                 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
331             }});
332 
registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class, new CachedServiceFetcher<NetworkPolicyManager>() { @Override public NetworkPolicyManager createService(ContextImpl ctx) { return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface( ServiceManager.getService(Context.NETWORK_POLICY_SERVICE))); }})333         registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
334                 new CachedServiceFetcher<NetworkPolicyManager>() {
335             @Override
336             public NetworkPolicyManager createService(ContextImpl ctx) {
337                 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
338                         ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)));
339             }});
340 
registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class, new CachedServiceFetcher<NotificationManager>() { @Override public NotificationManager createService(ContextImpl ctx) { final Context outerContext = ctx.getOuterContext(); return new NotificationManager( new ContextThemeWrapper(outerContext, Resources.selectSystemTheme(0, outerContext.getApplicationInfo().targetSdkVersion, com.android.internal.R.style.Theme_Dialog, com.android.internal.R.style.Theme_Holo_Dialog, com.android.internal.R.style.Theme_DeviceDefault_Dialog, com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)), ctx.mMainThread.getHandler()); }})341         registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
342                 new CachedServiceFetcher<NotificationManager>() {
343             @Override
344             public NotificationManager createService(ContextImpl ctx) {
345                 final Context outerContext = ctx.getOuterContext();
346                 return new NotificationManager(
347                     new ContextThemeWrapper(outerContext,
348                             Resources.selectSystemTheme(0,
349                                     outerContext.getApplicationInfo().targetSdkVersion,
350                                     com.android.internal.R.style.Theme_Dialog,
351                                     com.android.internal.R.style.Theme_Holo_Dialog,
352                                     com.android.internal.R.style.Theme_DeviceDefault_Dialog,
353                                     com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
354                     ctx.mMainThread.getHandler());
355             }});
356 
registerService(Context.NSD_SERVICE, NsdManager.class, new CachedServiceFetcher<NsdManager>() { @Override public NsdManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.NSD_SERVICE); INsdManager service = INsdManager.Stub.asInterface(b); return new NsdManager(ctx.getOuterContext(), service); }})357         registerService(Context.NSD_SERVICE, NsdManager.class,
358                 new CachedServiceFetcher<NsdManager>() {
359             @Override
360             public NsdManager createService(ContextImpl ctx) {
361                 IBinder b = ServiceManager.getService(Context.NSD_SERVICE);
362                 INsdManager service = INsdManager.Stub.asInterface(b);
363                 return new NsdManager(ctx.getOuterContext(), service);
364             }});
365 
registerService(Context.POWER_SERVICE, PowerManager.class, new CachedServiceFetcher<PowerManager>() { @Override public PowerManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.POWER_SERVICE); IPowerManager service = IPowerManager.Stub.asInterface(b); if (service == null) { Log.wtf(TAG, "Failed to get power manager service."); } return new PowerManager(ctx.getOuterContext(), service, ctx.mMainThread.getHandler()); }})366         registerService(Context.POWER_SERVICE, PowerManager.class,
367                 new CachedServiceFetcher<PowerManager>() {
368             @Override
369             public PowerManager createService(ContextImpl ctx) {
370                 IBinder b = ServiceManager.getService(Context.POWER_SERVICE);
371                 IPowerManager service = IPowerManager.Stub.asInterface(b);
372                 if (service == null) {
373                     Log.wtf(TAG, "Failed to get power manager service.");
374                 }
375                 return new PowerManager(ctx.getOuterContext(),
376                         service, ctx.mMainThread.getHandler());
377             }});
378 
registerService(Context.SEARCH_SERVICE, SearchManager.class, new CachedServiceFetcher<SearchManager>() { @Override public SearchManager createService(ContextImpl ctx) { return new SearchManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})379         registerService(Context.SEARCH_SERVICE, SearchManager.class,
380                 new CachedServiceFetcher<SearchManager>() {
381             @Override
382             public SearchManager createService(ContextImpl ctx) {
383                 return new SearchManager(ctx.getOuterContext(),
384                         ctx.mMainThread.getHandler());
385             }});
386 
registerService(Context.SENSOR_SERVICE, SensorManager.class, new CachedServiceFetcher<SensorManager>() { @Override public SensorManager createService(ContextImpl ctx) { return new SystemSensorManager(ctx.getOuterContext(), ctx.mMainThread.getHandler().getLooper()); }})387         registerService(Context.SENSOR_SERVICE, SensorManager.class,
388                 new CachedServiceFetcher<SensorManager>() {
389             @Override
390             public SensorManager createService(ContextImpl ctx) {
391                 return new SystemSensorManager(ctx.getOuterContext(),
392                   ctx.mMainThread.getHandler().getLooper());
393             }});
394 
registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class, new CachedServiceFetcher<StatusBarManager>() { @Override public StatusBarManager createService(ContextImpl ctx) { return new StatusBarManager(ctx.getOuterContext()); }})395         registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
396                 new CachedServiceFetcher<StatusBarManager>() {
397             @Override
398             public StatusBarManager createService(ContextImpl ctx) {
399                 return new StatusBarManager(ctx.getOuterContext());
400             }});
401 
registerService(Context.STORAGE_SERVICE, StorageManager.class, new CachedServiceFetcher<StorageManager>() { @Override public StorageManager createService(ContextImpl ctx) { return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper()); }})402         registerService(Context.STORAGE_SERVICE, StorageManager.class,
403                 new CachedServiceFetcher<StorageManager>() {
404             @Override
405             public StorageManager createService(ContextImpl ctx) {
406                 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
407             }});
408 
registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class, new CachedServiceFetcher<TelephonyManager>() { @Override public TelephonyManager createService(ContextImpl ctx) { return new TelephonyManager(ctx.getOuterContext()); }})409         registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
410                 new CachedServiceFetcher<TelephonyManager>() {
411             @Override
412             public TelephonyManager createService(ContextImpl ctx) {
413                 return new TelephonyManager(ctx.getOuterContext());
414             }});
415 
registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class, new CachedServiceFetcher<SubscriptionManager>() { @Override public SubscriptionManager createService(ContextImpl ctx) { return new SubscriptionManager(ctx.getOuterContext()); }})416         registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
417                 new CachedServiceFetcher<SubscriptionManager>() {
418             @Override
419             public SubscriptionManager createService(ContextImpl ctx) {
420                 return new SubscriptionManager(ctx.getOuterContext());
421             }});
422 
registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class, new CachedServiceFetcher<CarrierConfigManager>() { @Override public CarrierConfigManager createService(ContextImpl ctx) { return new CarrierConfigManager(); }})423         registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
424                 new CachedServiceFetcher<CarrierConfigManager>() {
425             @Override
426             public CarrierConfigManager createService(ContextImpl ctx) {
427                 return new CarrierConfigManager();
428             }});
429 
registerService(Context.TELECOM_SERVICE, TelecomManager.class, new CachedServiceFetcher<TelecomManager>() { @Override public TelecomManager createService(ContextImpl ctx) { return new TelecomManager(ctx.getOuterContext()); }})430         registerService(Context.TELECOM_SERVICE, TelecomManager.class,
431                 new CachedServiceFetcher<TelecomManager>() {
432             @Override
433             public TelecomManager createService(ContextImpl ctx) {
434                 return new TelecomManager(ctx.getOuterContext());
435             }});
436 
registerService(Context.UI_MODE_SERVICE, UiModeManager.class, new CachedServiceFetcher<UiModeManager>() { @Override public UiModeManager createService(ContextImpl ctx) { return new UiModeManager(); }})437         registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
438                 new CachedServiceFetcher<UiModeManager>() {
439             @Override
440             public UiModeManager createService(ContextImpl ctx) {
441                 return new UiModeManager();
442             }});
443 
registerService(Context.USB_SERVICE, UsbManager.class, new CachedServiceFetcher<UsbManager>() { @Override public UsbManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.USB_SERVICE); return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); }})444         registerService(Context.USB_SERVICE, UsbManager.class,
445                 new CachedServiceFetcher<UsbManager>() {
446             @Override
447             public UsbManager createService(ContextImpl ctx) {
448                 IBinder b = ServiceManager.getService(Context.USB_SERVICE);
449                 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
450             }});
451 
registerService(Context.SERIAL_SERVICE, SerialManager.class, new CachedServiceFetcher<SerialManager>() { @Override public SerialManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.SERIAL_SERVICE); return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); }})452         registerService(Context.SERIAL_SERVICE, SerialManager.class,
453                 new CachedServiceFetcher<SerialManager>() {
454             @Override
455             public SerialManager createService(ContextImpl ctx) {
456                 IBinder b = ServiceManager.getService(Context.SERIAL_SERVICE);
457                 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
458             }});
459 
registerService(Context.VIBRATOR_SERVICE, Vibrator.class, new CachedServiceFetcher<Vibrator>() { @Override public Vibrator createService(ContextImpl ctx) { return new SystemVibrator(ctx); }})460         registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
461                 new CachedServiceFetcher<Vibrator>() {
462             @Override
463             public Vibrator createService(ContextImpl ctx) {
464                 return new SystemVibrator(ctx);
465             }});
466 
registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class, new CachedServiceFetcher<WallpaperManager>() { @Override public WallpaperManager createService(ContextImpl ctx) { return new WallpaperManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})467         registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
468                 new CachedServiceFetcher<WallpaperManager>() {
469             @Override
470             public WallpaperManager createService(ContextImpl ctx) {
471                 return new WallpaperManager(ctx.getOuterContext(),
472                         ctx.mMainThread.getHandler());
473             }});
474 
registerService(Context.WIFI_SERVICE, WifiManager.class, new CachedServiceFetcher<WifiManager>() { @Override public WifiManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.WIFI_SERVICE); IWifiManager service = IWifiManager.Stub.asInterface(b); return new WifiManager(ctx.getOuterContext(), service); }})475         registerService(Context.WIFI_SERVICE, WifiManager.class,
476                 new CachedServiceFetcher<WifiManager>() {
477             @Override
478             public WifiManager createService(ContextImpl ctx) {
479                 IBinder b = ServiceManager.getService(Context.WIFI_SERVICE);
480                 IWifiManager service = IWifiManager.Stub.asInterface(b);
481                 return new WifiManager(ctx.getOuterContext(), service);
482             }});
483 
registerService(Context.WIFI_PASSPOINT_SERVICE, WifiPasspointManager.class, new CachedServiceFetcher<WifiPasspointManager>() { @Override public WifiPasspointManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.WIFI_PASSPOINT_SERVICE); IWifiPasspointManager service = IWifiPasspointManager.Stub.asInterface(b); return new WifiPasspointManager(ctx.getOuterContext(), service); }})484         registerService(Context.WIFI_PASSPOINT_SERVICE, WifiPasspointManager.class,
485                 new CachedServiceFetcher<WifiPasspointManager>() {
486             @Override
487             public WifiPasspointManager createService(ContextImpl ctx) {
488                 IBinder b = ServiceManager.getService(Context.WIFI_PASSPOINT_SERVICE);
489                 IWifiPasspointManager service = IWifiPasspointManager.Stub.asInterface(b);
490                 return new WifiPasspointManager(ctx.getOuterContext(), service);
491             }});
492 
registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class, new StaticServiceFetcher<WifiP2pManager>() { @Override public WifiP2pManager createService() { IBinder b = ServiceManager.getService(Context.WIFI_P2P_SERVICE); IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b); return new WifiP2pManager(service); }})493         registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
494                 new StaticServiceFetcher<WifiP2pManager>() {
495             @Override
496             public WifiP2pManager createService() {
497                 IBinder b = ServiceManager.getService(Context.WIFI_P2P_SERVICE);
498                 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
499                 return new WifiP2pManager(service);
500             }});
501 
registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class, new CachedServiceFetcher<WifiScanner>() { @Override public WifiScanner createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.WIFI_SCANNING_SERVICE); IWifiScanner service = IWifiScanner.Stub.asInterface(b); return new WifiScanner(ctx.getOuterContext(), service); }})502         registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
503                 new CachedServiceFetcher<WifiScanner>() {
504             @Override
505             public WifiScanner createService(ContextImpl ctx) {
506                 IBinder b = ServiceManager.getService(Context.WIFI_SCANNING_SERVICE);
507                 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
508                 return new WifiScanner(ctx.getOuterContext(), service);
509             }});
510 
registerService(Context.WIFI_RTT_SERVICE, RttManager.class, new CachedServiceFetcher<RttManager>() { @Override public RttManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.WIFI_RTT_SERVICE); IRttManager service = IRttManager.Stub.asInterface(b); return new RttManager(ctx.getOuterContext(), service); }})511         registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
512                 new CachedServiceFetcher<RttManager>() {
513             @Override
514             public RttManager createService(ContextImpl ctx) {
515                 IBinder b = ServiceManager.getService(Context.WIFI_RTT_SERVICE);
516                 IRttManager service = IRttManager.Stub.asInterface(b);
517                 return new RttManager(ctx.getOuterContext(), service);
518             }});
519 
registerService(Context.ETHERNET_SERVICE, EthernetManager.class, new CachedServiceFetcher<EthernetManager>() { @Override public EthernetManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ETHERNET_SERVICE); IEthernetManager service = IEthernetManager.Stub.asInterface(b); return new EthernetManager(ctx.getOuterContext(), service); }})520         registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
521                 new CachedServiceFetcher<EthernetManager>() {
522             @Override
523             public EthernetManager createService(ContextImpl ctx) {
524                 IBinder b = ServiceManager.getService(Context.ETHERNET_SERVICE);
525                 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
526                 return new EthernetManager(ctx.getOuterContext(), service);
527             }});
528 
registerService(Context.WINDOW_SERVICE, WindowManager.class, new CachedServiceFetcher<WindowManager>() { @Override public WindowManager createService(ContextImpl ctx) { return new WindowManagerImpl(ctx.getDisplay()); }})529         registerService(Context.WINDOW_SERVICE, WindowManager.class,
530                 new CachedServiceFetcher<WindowManager>() {
531             @Override
532             public WindowManager createService(ContextImpl ctx) {
533                 return new WindowManagerImpl(ctx.getDisplay());
534             }});
535 
registerService(Context.USER_SERVICE, UserManager.class, new CachedServiceFetcher<UserManager>() { @Override public UserManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.USER_SERVICE); IUserManager service = IUserManager.Stub.asInterface(b); return new UserManager(ctx, service); }})536         registerService(Context.USER_SERVICE, UserManager.class,
537                 new CachedServiceFetcher<UserManager>() {
538             @Override
539             public UserManager createService(ContextImpl ctx) {
540                 IBinder b = ServiceManager.getService(Context.USER_SERVICE);
541                 IUserManager service = IUserManager.Stub.asInterface(b);
542                 return new UserManager(ctx, service);
543             }});
544 
registerService(Context.APP_OPS_SERVICE, AppOpsManager.class, new CachedServiceFetcher<AppOpsManager>() { @Override public AppOpsManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE); IAppOpsService service = IAppOpsService.Stub.asInterface(b); return new AppOpsManager(ctx, service); }})545         registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
546                 new CachedServiceFetcher<AppOpsManager>() {
547             @Override
548             public AppOpsManager createService(ContextImpl ctx) {
549                 IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE);
550                 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
551                 return new AppOpsManager(ctx, service);
552             }});
553 
registerService(Context.CAMERA_SERVICE, CameraManager.class, new CachedServiceFetcher<CameraManager>() { @Override public CameraManager createService(ContextImpl ctx) { return new CameraManager(ctx); }})554         registerService(Context.CAMERA_SERVICE, CameraManager.class,
555                 new CachedServiceFetcher<CameraManager>() {
556             @Override
557             public CameraManager createService(ContextImpl ctx) {
558                 return new CameraManager(ctx);
559             }});
560 
registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class, new CachedServiceFetcher<LauncherApps>() { @Override public LauncherApps createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.LAUNCHER_APPS_SERVICE); ILauncherApps service = ILauncherApps.Stub.asInterface(b); return new LauncherApps(ctx, service); }})561         registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
562                 new CachedServiceFetcher<LauncherApps>() {
563             @Override
564             public LauncherApps createService(ContextImpl ctx) {
565                 IBinder b = ServiceManager.getService(Context.LAUNCHER_APPS_SERVICE);
566                 ILauncherApps service = ILauncherApps.Stub.asInterface(b);
567                 return new LauncherApps(ctx, service);
568             }});
569 
registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class, new CachedServiceFetcher<RestrictionsManager>() { @Override public RestrictionsManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.RESTRICTIONS_SERVICE); IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b); return new RestrictionsManager(ctx, service); }})570         registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
571                 new CachedServiceFetcher<RestrictionsManager>() {
572             @Override
573             public RestrictionsManager createService(ContextImpl ctx) {
574                 IBinder b = ServiceManager.getService(Context.RESTRICTIONS_SERVICE);
575                 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
576                 return new RestrictionsManager(ctx, service);
577             }});
578 
registerService(Context.PRINT_SERVICE, PrintManager.class, new CachedServiceFetcher<PrintManager>() { @Override public PrintManager createService(ContextImpl ctx) { IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE); IPrintManager service = IPrintManager.Stub.asInterface(iBinder); return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(), UserHandle.getAppId(Process.myUid())); }})579         registerService(Context.PRINT_SERVICE, PrintManager.class,
580                 new CachedServiceFetcher<PrintManager>() {
581             @Override
582             public PrintManager createService(ContextImpl ctx) {
583                 IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
584                 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
585                 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
586                         UserHandle.getAppId(Process.myUid()));
587             }});
588 
registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class, new CachedServiceFetcher<ConsumerIrManager>() { @Override public ConsumerIrManager createService(ContextImpl ctx) { return new ConsumerIrManager(ctx); }})589         registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
590                 new CachedServiceFetcher<ConsumerIrManager>() {
591             @Override
592             public ConsumerIrManager createService(ContextImpl ctx) {
593                 return new ConsumerIrManager(ctx);
594             }});
595 
registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class, new CachedServiceFetcher<MediaSessionManager>() { @Override public MediaSessionManager createService(ContextImpl ctx) { return new MediaSessionManager(ctx); }})596         registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
597                 new CachedServiceFetcher<MediaSessionManager>() {
598             @Override
599             public MediaSessionManager createService(ContextImpl ctx) {
600                 return new MediaSessionManager(ctx);
601             }});
602 
registerService(Context.TRUST_SERVICE, TrustManager.class, new StaticServiceFetcher<TrustManager>() { @Override public TrustManager createService() { IBinder b = ServiceManager.getService(Context.TRUST_SERVICE); return new TrustManager(b); }})603         registerService(Context.TRUST_SERVICE, TrustManager.class,
604                 new StaticServiceFetcher<TrustManager>() {
605             @Override
606             public TrustManager createService() {
607                 IBinder b = ServiceManager.getService(Context.TRUST_SERVICE);
608                 return new TrustManager(b);
609             }});
610 
registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class, new CachedServiceFetcher<FingerprintManager>() { @Override public FingerprintManager createService(ContextImpl ctx) { IBinder binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE); IFingerprintService service = IFingerprintService.Stub.asInterface(binder); return new FingerprintManager(ctx.getOuterContext(), service); }})611         registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
612                 new CachedServiceFetcher<FingerprintManager>() {
613             @Override
614             public FingerprintManager createService(ContextImpl ctx) {
615                 IBinder binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
616                 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
617                 return new FingerprintManager(ctx.getOuterContext(), service);
618             }});
619 
registerService(Context.TV_INPUT_SERVICE, TvInputManager.class, new StaticServiceFetcher<TvInputManager>() { @Override public TvInputManager createService() { IBinder iBinder = ServiceManager.getService(Context.TV_INPUT_SERVICE); ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder); return new TvInputManager(service, UserHandle.myUserId()); }})620         registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
621                 new StaticServiceFetcher<TvInputManager>() {
622             @Override
623             public TvInputManager createService() {
624                 IBinder iBinder = ServiceManager.getService(Context.TV_INPUT_SERVICE);
625                 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
626                 return new TvInputManager(service, UserHandle.myUserId());
627             }});
628 
registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class, new CachedServiceFetcher<NetworkScoreManager>() { @Override public NetworkScoreManager createService(ContextImpl ctx) { return new NetworkScoreManager(ctx); }})629         registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
630                 new CachedServiceFetcher<NetworkScoreManager>() {
631             @Override
632             public NetworkScoreManager createService(ContextImpl ctx) {
633                 return new NetworkScoreManager(ctx);
634             }});
635 
registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class, new CachedServiceFetcher<UsageStatsManager>() { @Override public UsageStatsManager createService(ContextImpl ctx) { IBinder iBinder = ServiceManager.getService(Context.USAGE_STATS_SERVICE); IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder); return new UsageStatsManager(ctx.getOuterContext(), service); }})636         registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
637                 new CachedServiceFetcher<UsageStatsManager>() {
638             @Override
639             public UsageStatsManager createService(ContextImpl ctx) {
640                 IBinder iBinder = ServiceManager.getService(Context.USAGE_STATS_SERVICE);
641                 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
642                 return new UsageStatsManager(ctx.getOuterContext(), service);
643             }});
644 
registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class, new CachedServiceFetcher<NetworkStatsManager>() { @Override public NetworkStatsManager createService(ContextImpl ctx) { return new NetworkStatsManager(ctx.getOuterContext()); }})645         registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
646                 new CachedServiceFetcher<NetworkStatsManager>() {
647             @Override
648             public NetworkStatsManager createService(ContextImpl ctx) {
649                 return new NetworkStatsManager(ctx.getOuterContext());
650             }});
651 
registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class, new StaticServiceFetcher<JobScheduler>() { @Override public JobScheduler createService() { IBinder b = ServiceManager.getService(Context.JOB_SCHEDULER_SERVICE); return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b)); }})652         registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
653                 new StaticServiceFetcher<JobScheduler>() {
654             @Override
655             public JobScheduler createService() {
656                 IBinder b = ServiceManager.getService(Context.JOB_SCHEDULER_SERVICE);
657                 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
658             }});
659 
registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class, new StaticServiceFetcher<PersistentDataBlockManager>() { @Override public PersistentDataBlockManager createService() { IBinder b = ServiceManager.getService(Context.PERSISTENT_DATA_BLOCK_SERVICE); IPersistentDataBlockService persistentDataBlockService = IPersistentDataBlockService.Stub.asInterface(b); if (persistentDataBlockService != null) { return new PersistentDataBlockManager(persistentDataBlockService); } else { return null; } }})660         registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
661                 new StaticServiceFetcher<PersistentDataBlockManager>() {
662             @Override
663             public PersistentDataBlockManager createService() {
664                 IBinder b = ServiceManager.getService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
665                 IPersistentDataBlockService persistentDataBlockService =
666                         IPersistentDataBlockService.Stub.asInterface(b);
667                 if (persistentDataBlockService != null) {
668                     return new PersistentDataBlockManager(persistentDataBlockService);
669                 } else {
670                     // not supported
671                     return null;
672                 }
673             }});
674 
registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class, new CachedServiceFetcher<MediaProjectionManager>() { @Override public MediaProjectionManager createService(ContextImpl ctx) { return new MediaProjectionManager(ctx); }})675         registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
676                 new CachedServiceFetcher<MediaProjectionManager>() {
677             @Override
678             public MediaProjectionManager createService(ContextImpl ctx) {
679                 return new MediaProjectionManager(ctx);
680             }});
681 
registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class, new CachedServiceFetcher<AppWidgetManager>() { @Override public AppWidgetManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE); return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b)); }})682         registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
683                 new CachedServiceFetcher<AppWidgetManager>() {
684             @Override
685             public AppWidgetManager createService(ContextImpl ctx) {
686                 IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE);
687                 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
688             }});
689 
registerService(Context.MIDI_SERVICE, MidiManager.class, new CachedServiceFetcher<MidiManager>() { @Override public MidiManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.MIDI_SERVICE); if (b == null) { return null; } return new MidiManager(IMidiManager.Stub.asInterface(b)); }})690         registerService(Context.MIDI_SERVICE, MidiManager.class,
691                 new CachedServiceFetcher<MidiManager>() {
692             @Override
693             public MidiManager createService(ContextImpl ctx) {
694                 IBinder b = ServiceManager.getService(Context.MIDI_SERVICE);
695                 if (b == null) {
696                     return null;
697                 }
698                 return new MidiManager(IMidiManager.Stub.asInterface(b));
699             }});
700 
registerService(Context.RADIO_SERVICE, RadioManager.class, new CachedServiceFetcher<RadioManager>() { @Override public RadioManager createService(ContextImpl ctx) { return new RadioManager(ctx); }})701         registerService(Context.RADIO_SERVICE, RadioManager.class,
702                 new CachedServiceFetcher<RadioManager>() {
703             @Override
704             public RadioManager createService(ContextImpl ctx) {
705                 return new RadioManager(ctx);
706             }});
707     }
708 
709     /**
710      * Creates an array which is used to cache per-Context service instances.
711      */
createServiceCache()712     public static Object[] createServiceCache() {
713         return new Object[sServiceCacheSize];
714     }
715 
716     /**
717      * Gets a system service from a given context.
718      */
getSystemService(ContextImpl ctx, String name)719     public static Object getSystemService(ContextImpl ctx, String name) {
720         ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
721         return fetcher != null ? fetcher.getService(ctx) : null;
722     }
723 
724     /**
725      * Gets the name of the system-level service that is represented by the specified class.
726      */
getSystemServiceName(Class<?> serviceClass)727     public static String getSystemServiceName(Class<?> serviceClass) {
728         return SYSTEM_SERVICE_NAMES.get(serviceClass);
729     }
730 
731     /**
732      * Statically registers a system service with the context.
733      * This method must be called during static initialization only.
734      */
registerService(String serviceName, Class<T> serviceClass, ServiceFetcher<T> serviceFetcher)735     private static <T> void registerService(String serviceName, Class<T> serviceClass,
736             ServiceFetcher<T> serviceFetcher) {
737         SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
738         SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
739     }
740 
741     /**
742      * Base interface for classes that fetch services.
743      * These objects must only be created during static initialization.
744      */
745     static abstract interface ServiceFetcher<T> {
getService(ContextImpl ctx)746         T getService(ContextImpl ctx);
747     }
748 
749     /**
750      * Override this class when the system service constructor needs a
751      * ContextImpl and should be cached and retained by that context.
752      */
753     static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
754         private final int mCacheIndex;
755 
CachedServiceFetcher()756         public CachedServiceFetcher() {
757             mCacheIndex = sServiceCacheSize++;
758         }
759 
760         @Override
761         @SuppressWarnings("unchecked")
getService(ContextImpl ctx)762         public final T getService(ContextImpl ctx) {
763             final Object[] cache = ctx.mServiceCache;
764             synchronized (cache) {
765                 // Fetch or create the service.
766                 Object service = cache[mCacheIndex];
767                 if (service == null) {
768                     service = createService(ctx);
769                     cache[mCacheIndex] = service;
770                 }
771                 return (T)service;
772             }
773         }
774 
createService(ContextImpl ctx)775         public abstract T createService(ContextImpl ctx);
776     }
777 
778     /**
779      * Override this class when the system service does not need a ContextImpl
780      * and should be cached and retained process-wide.
781      */
782     static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
783         private T mCachedInstance;
784 
785         @Override
getService(ContextImpl unused)786         public final T getService(ContextImpl unused) {
787             synchronized (StaticServiceFetcher.this) {
788                 if (mCachedInstance == null) {
789                     mCachedInstance = createService();
790                 }
791                 return mCachedInstance;
792             }
793         }
794 
createService()795         public abstract T createService();
796     }
797 
798     /**
799      * Like StaticServiceFetcher, creates only one instance of the service per process, but when
800      * creating the service for the first time, passes it the outer context of the creating
801      * component.
802      *
803      * TODO: Is this safe in the case where multiple applications share the same process?
804      * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
805      * case where multiple application components each have their own ConnectivityManager object.
806      */
807     static abstract class StaticOuterContextServiceFetcher<T> implements ServiceFetcher<T> {
808         private T mCachedInstance;
809 
810         @Override
getService(ContextImpl ctx)811         public final T getService(ContextImpl ctx) {
812             synchronized (StaticOuterContextServiceFetcher.this) {
813                 if (mCachedInstance == null) {
814                     mCachedInstance = createService(ctx.getOuterContext());
815                 }
816                 return mCachedInstance;
817             }
818         }
819 
createService(Context applicationContext)820         public abstract T createService(Context applicationContext);
821     }
822 
823 }
824