blob: 2d4649575c96c67c0e15e4c3c7ec3ba6248268c5 [file] [log] [blame]
Jeff Brown6e539312015-02-24 18:53:21 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Jeff Brown6e539312015-02-24 18:53:21 -080019import android.accounts.AccountManager;
20import android.accounts.IAccountManager;
21import android.app.admin.DevicePolicyManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060022import android.app.admin.IDevicePolicyManager;
Jeff Brown6e539312015-02-24 18:53:21 -080023import android.app.job.IJobScheduler;
24import android.app.job.JobScheduler;
25import android.app.trust.TrustManager;
26import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000027import android.app.usage.NetworkStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080028import android.app.usage.UsageStatsManager;
29import android.appwidget.AppWidgetManager;
30import android.bluetooth.BluetoothManager;
31import android.content.ClipboardManager;
32import android.content.Context;
33import android.content.IRestrictionsManager;
34import android.content.RestrictionsManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080035import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080036import android.content.pm.LauncherApps;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080037import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080038import android.content.res.Resources;
39import android.hardware.ConsumerIrManager;
40import android.hardware.ISerialManager;
41import android.hardware.SensorManager;
42import android.hardware.SerialManager;
43import android.hardware.SystemSensorManager;
44import android.hardware.camera2.CameraManager;
45import android.hardware.display.DisplayManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060046import android.hardware.fingerprint.FingerprintManager;
47import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -080048import android.hardware.hdmi.HdmiControlManager;
49import android.hardware.hdmi.IHdmiControlService;
50import android.hardware.input.InputManager;
Peng Xu9ff7d222016-02-11 13:02:05 -080051import android.hardware.location.ContextHubManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060052import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080053import android.hardware.usb.IUsbManager;
54import android.hardware.usb.UsbManager;
55import android.location.CountryDetector;
56import android.location.ICountryDetector;
57import android.location.ILocationManager;
58import android.location.LocationManager;
59import android.media.AudioManager;
60import android.media.MediaRouter;
61import android.media.midi.IMidiManager;
62import android.media.midi.MidiManager;
63import android.media.projection.MediaProjectionManager;
64import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080065import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -080066import android.media.tv.ITvInputManager;
67import android.media.tv.TvInputManager;
68import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -070069import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -080070import android.net.EthernetManager;
71import android.net.IConnectivityManager;
72import android.net.IEthernetManager;
73import android.net.INetworkPolicyManager;
74import android.net.NetworkPolicyManager;
75import android.net.NetworkScoreManager;
76import android.net.nsd.INsdManager;
77import android.net.nsd.NsdManager;
78import android.net.wifi.IRttManager;
79import android.net.wifi.IWifiManager;
80import android.net.wifi.IWifiScanner;
81import android.net.wifi.RttManager;
82import android.net.wifi.WifiManager;
83import android.net.wifi.WifiScanner;
Etan Cohen20d329b2015-09-29 13:49:02 -070084import android.net.wifi.nan.IWifiNanManager;
85import android.net.wifi.nan.WifiNanManager;
Jeff Brown6e539312015-02-24 18:53:21 -080086import android.net.wifi.p2p.IWifiP2pManager;
87import android.net.wifi.p2p.WifiP2pManager;
Jeff Brown6e539312015-02-24 18:53:21 -080088import android.nfc.NfcManager;
89import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060090import android.os.BatteryStats;
Jeff Brown6e539312015-02-24 18:53:21 -080091import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010092import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060093import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -080094import android.os.IBinder;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010095import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -080096import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -080097import android.os.IRecoverySystem;
Jeff Brown6e539312015-02-24 18:53:21 -080098import android.os.IUserManager;
99import android.os.PowerManager;
100import android.os.Process;
Tao Baoe8a403d2015-12-31 07:44:55 -0800101import android.os.RecoverySystem;
Jeff Brown6e539312015-02-24 18:53:21 -0800102import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600103import android.os.ServiceManager.ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -0800104import android.os.SystemVibrator;
105import android.os.UserHandle;
106import android.os.UserManager;
107import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800108import android.os.health.SystemHealthManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800109import android.os.storage.StorageManager;
110import android.print.IPrintManager;
111import android.print.PrintManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800112import android.service.persistentdata.IPersistentDataBlockService;
113import android.service.persistentdata.PersistentDataBlockManager;
114import android.telecom.TelecomManager;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800115import android.telephony.CarrierConfigManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800116import android.telephony.SubscriptionManager;
117import android.telephony.TelephonyManager;
118import android.util.Log;
119import android.view.ContextThemeWrapper;
120import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800121import android.view.WindowManager;
122import android.view.WindowManagerImpl;
123import android.view.accessibility.AccessibilityManager;
124import android.view.accessibility.CaptioningManager;
125import android.view.inputmethod.InputMethodManager;
126import android.view.textservice.TextServicesManager;
127
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600128import com.android.internal.app.IAppOpsService;
129import com.android.internal.app.IBatteryStats;
130import com.android.internal.app.ISoundTriggerService;
131import com.android.internal.appwidget.IAppWidgetService;
132import com.android.internal.os.IDropBoxManagerService;
133import com.android.internal.policy.PhoneLayoutInflater;
134
Jeff Brown6e539312015-02-24 18:53:21 -0800135import java.util.HashMap;
136
137/**
138 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
139 * Used by {@link ContextImpl}.
140 */
141final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600142 private static final String TAG = "SystemServiceRegistry";
143
Jeff Brown6e539312015-02-24 18:53:21 -0800144 // Service registry information.
145 // This information is never changed once static initialization has completed.
146 private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
147 new HashMap<Class<?>, String>();
148 private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
149 new HashMap<String, ServiceFetcher<?>>();
150 private static int sServiceCacheSize;
151
152 // Not instantiable.
153 private SystemServiceRegistry() { }
154
155 static {
156 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
157 new CachedServiceFetcher<AccessibilityManager>() {
158 @Override
159 public AccessibilityManager createService(ContextImpl ctx) {
160 return AccessibilityManager.getInstance(ctx);
161 }});
162
163 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
164 new CachedServiceFetcher<CaptioningManager>() {
165 @Override
166 public CaptioningManager createService(ContextImpl ctx) {
167 return new CaptioningManager(ctx);
168 }});
169
170 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
171 new CachedServiceFetcher<AccountManager>() {
172 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600173 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
174 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800175 IAccountManager service = IAccountManager.Stub.asInterface(b);
176 return new AccountManager(ctx, service);
177 }});
178
179 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
180 new CachedServiceFetcher<ActivityManager>() {
181 @Override
182 public ActivityManager createService(ContextImpl ctx) {
183 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
184 }});
185
186 registerService(Context.ALARM_SERVICE, AlarmManager.class,
187 new CachedServiceFetcher<AlarmManager>() {
188 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600189 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
190 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800191 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
192 return new AlarmManager(service, ctx);
193 }});
194
195 registerService(Context.AUDIO_SERVICE, AudioManager.class,
196 new CachedServiceFetcher<AudioManager>() {
197 @Override
198 public AudioManager createService(ContextImpl ctx) {
199 return new AudioManager(ctx);
200 }});
201
202 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
203 new CachedServiceFetcher<MediaRouter>() {
204 @Override
205 public MediaRouter createService(ContextImpl ctx) {
206 return new MediaRouter(ctx);
207 }});
208
209 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
210 new CachedServiceFetcher<BluetoothManager>() {
211 @Override
212 public BluetoothManager createService(ContextImpl ctx) {
213 return new BluetoothManager(ctx);
214 }});
215
216 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
217 new StaticServiceFetcher<HdmiControlManager>() {
218 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600219 public HdmiControlManager createService() throws ServiceNotFoundException {
220 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800221 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
222 }});
223
224 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
225 new CachedServiceFetcher<ClipboardManager>() {
226 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600227 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800228 return new ClipboardManager(ctx.getOuterContext(),
229 ctx.mMainThread.getHandler());
230 }});
231
232 // The clipboard service moved to a new package. If someone asks for the old
233 // interface by class then we want to redirect over to the new interface instead
234 // (which extends it).
235 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
236
237 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900238 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800239 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600240 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
241 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900242 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
243 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800244 }});
245
246 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
247 new StaticServiceFetcher<CountryDetector>() {
248 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600249 public CountryDetector createService() throws ServiceNotFoundException {
250 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800251 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
252 }});
253
254 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
255 new CachedServiceFetcher<DevicePolicyManager>() {
256 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600257 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
258 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
259 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800260 }});
261
262 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
263 new CachedServiceFetcher<DownloadManager>() {
264 @Override
265 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700266 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800267 }});
268
269 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
270 new StaticServiceFetcher<BatteryManager>() {
271 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600272 public BatteryManager createService() throws ServiceNotFoundException {
273 IBatteryStats stats = IBatteryStats.Stub.asInterface(
274 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
275 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
276 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
277 return new BatteryManager(stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800278 }});
279
280 registerService(Context.NFC_SERVICE, NfcManager.class,
281 new CachedServiceFetcher<NfcManager>() {
282 @Override
283 public NfcManager createService(ContextImpl ctx) {
284 return new NfcManager(ctx);
285 }});
286
287 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700288 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800289 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600290 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
291 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800292 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700293 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800294 }});
295
296 registerService(Context.INPUT_SERVICE, InputManager.class,
297 new StaticServiceFetcher<InputManager>() {
298 @Override
299 public InputManager createService() {
300 return InputManager.getInstance();
301 }});
302
303 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
304 new CachedServiceFetcher<DisplayManager>() {
305 @Override
306 public DisplayManager createService(ContextImpl ctx) {
307 return new DisplayManager(ctx.getOuterContext());
308 }});
309
310 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
311 new StaticServiceFetcher<InputMethodManager>() {
312 @Override
313 public InputMethodManager createService() {
314 return InputMethodManager.getInstance();
315 }});
316
317 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
318 new StaticServiceFetcher<TextServicesManager>() {
319 @Override
320 public TextServicesManager createService() {
321 return TextServicesManager.getInstance();
322 }});
323
324 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
325 new StaticServiceFetcher<KeyguardManager>() {
326 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600327 public KeyguardManager createService() throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800328 return new KeyguardManager();
329 }});
330
331 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
332 new CachedServiceFetcher<LayoutInflater>() {
333 @Override
334 public LayoutInflater createService(ContextImpl ctx) {
335 return new PhoneLayoutInflater(ctx.getOuterContext());
336 }});
337
338 registerService(Context.LOCATION_SERVICE, LocationManager.class,
339 new CachedServiceFetcher<LocationManager>() {
340 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600341 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
342 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800343 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
344 }});
345
346 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700347 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800348 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600349 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700350 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600351 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800352 }});
353
354 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
355 new CachedServiceFetcher<NotificationManager>() {
356 @Override
357 public NotificationManager createService(ContextImpl ctx) {
358 final Context outerContext = ctx.getOuterContext();
359 return new NotificationManager(
360 new ContextThemeWrapper(outerContext,
361 Resources.selectSystemTheme(0,
362 outerContext.getApplicationInfo().targetSdkVersion,
363 com.android.internal.R.style.Theme_Dialog,
364 com.android.internal.R.style.Theme_Holo_Dialog,
365 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
366 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
367 ctx.mMainThread.getHandler());
368 }});
369
370 registerService(Context.NSD_SERVICE, NsdManager.class,
371 new CachedServiceFetcher<NsdManager>() {
372 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600373 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
374 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800375 INsdManager service = INsdManager.Stub.asInterface(b);
376 return new NsdManager(ctx.getOuterContext(), service);
377 }});
378
379 registerService(Context.POWER_SERVICE, PowerManager.class,
380 new CachedServiceFetcher<PowerManager>() {
381 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600382 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
383 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800384 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800385 return new PowerManager(ctx.getOuterContext(),
386 service, ctx.mMainThread.getHandler());
387 }});
388
Tao Baoe8a403d2015-12-31 07:44:55 -0800389 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
390 new CachedServiceFetcher<RecoverySystem>() {
391 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600392 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
393 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800394 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800395 return new RecoverySystem(service);
396 }});
397
Jeff Brown6e539312015-02-24 18:53:21 -0800398 registerService(Context.SEARCH_SERVICE, SearchManager.class,
399 new CachedServiceFetcher<SearchManager>() {
400 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600401 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800402 return new SearchManager(ctx.getOuterContext(),
403 ctx.mMainThread.getHandler());
404 }});
405
406 registerService(Context.SENSOR_SERVICE, SensorManager.class,
407 new CachedServiceFetcher<SensorManager>() {
408 @Override
409 public SensorManager createService(ContextImpl ctx) {
410 return new SystemSensorManager(ctx.getOuterContext(),
411 ctx.mMainThread.getHandler().getLooper());
412 }});
413
414 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
415 new CachedServiceFetcher<StatusBarManager>() {
416 @Override
417 public StatusBarManager createService(ContextImpl ctx) {
418 return new StatusBarManager(ctx.getOuterContext());
419 }});
420
421 registerService(Context.STORAGE_SERVICE, StorageManager.class,
422 new CachedServiceFetcher<StorageManager>() {
423 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600424 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700425 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800426 }});
427
428 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
429 new CachedServiceFetcher<TelephonyManager>() {
430 @Override
431 public TelephonyManager createService(ContextImpl ctx) {
432 return new TelephonyManager(ctx.getOuterContext());
433 }});
434
435 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
436 new CachedServiceFetcher<SubscriptionManager>() {
437 @Override
438 public SubscriptionManager createService(ContextImpl ctx) {
439 return new SubscriptionManager(ctx.getOuterContext());
440 }});
441
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800442 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
443 new CachedServiceFetcher<CarrierConfigManager>() {
444 @Override
445 public CarrierConfigManager createService(ContextImpl ctx) {
446 return new CarrierConfigManager();
447 }});
448
Jeff Brown6e539312015-02-24 18:53:21 -0800449 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
450 new CachedServiceFetcher<TelecomManager>() {
451 @Override
452 public TelecomManager createService(ContextImpl ctx) {
453 return new TelecomManager(ctx.getOuterContext());
454 }});
455
456 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
457 new CachedServiceFetcher<UiModeManager>() {
458 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600459 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800460 return new UiModeManager();
461 }});
462
463 registerService(Context.USB_SERVICE, UsbManager.class,
464 new CachedServiceFetcher<UsbManager>() {
465 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600466 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
467 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800468 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
469 }});
470
471 registerService(Context.SERIAL_SERVICE, SerialManager.class,
472 new CachedServiceFetcher<SerialManager>() {
473 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600474 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
475 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800476 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
477 }});
478
479 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
480 new CachedServiceFetcher<Vibrator>() {
481 @Override
482 public Vibrator createService(ContextImpl ctx) {
483 return new SystemVibrator(ctx);
484 }});
485
486 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
487 new CachedServiceFetcher<WallpaperManager>() {
488 @Override
489 public WallpaperManager createService(ContextImpl ctx) {
490 return new WallpaperManager(ctx.getOuterContext(),
491 ctx.mMainThread.getHandler());
492 }});
493
494 registerService(Context.WIFI_SERVICE, WifiManager.class,
495 new CachedServiceFetcher<WifiManager>() {
496 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600497 public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
498 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800499 IWifiManager service = IWifiManager.Stub.asInterface(b);
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700500 return new WifiManager(ctx.getOuterContext(), service,
501 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800502 }});
503
Jeff Brown6e539312015-02-24 18:53:21 -0800504 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
505 new StaticServiceFetcher<WifiP2pManager>() {
506 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600507 public WifiP2pManager createService() throws ServiceNotFoundException {
508 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800509 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
510 return new WifiP2pManager(service);
511 }});
512
Etan Cohen20d329b2015-09-29 13:49:02 -0700513 registerService(Context.WIFI_NAN_SERVICE, WifiNanManager.class,
Etan Cohen7531a282016-08-11 09:31:21 -0700514 new CachedServiceFetcher<WifiNanManager>() {
Etan Cohen20d329b2015-09-29 13:49:02 -0700515 @Override
Etan Cohenccb0b652016-08-19 11:22:06 -0700516 public WifiNanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Etan Cohen20d329b2015-09-29 13:49:02 -0700517 IBinder b = ServiceManager.getService(Context.WIFI_NAN_SERVICE);
518 IWifiNanManager service = IWifiNanManager.Stub.asInterface(b);
519 if (service == null) {
520 return null;
521 }
Etan Cohen7531a282016-08-11 09:31:21 -0700522 return new WifiNanManager(ctx.getOuterContext(), service);
Etan Cohen20d329b2015-09-29 13:49:02 -0700523 }});
524
Jeff Brown6e539312015-02-24 18:53:21 -0800525 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
526 new CachedServiceFetcher<WifiScanner>() {
527 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600528 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
529 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800530 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
Mitchell Wills91312c72016-04-06 17:03:35 -0700531 return new WifiScanner(ctx.getOuterContext(), service,
532 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800533 }});
534
535 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
536 new CachedServiceFetcher<RttManager>() {
537 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600538 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
539 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800540 IRttManager service = IRttManager.Stub.asInterface(b);
Wei Wangbd3353a2016-04-08 14:24:09 -0700541 return new RttManager(ctx.getOuterContext(), service,
542 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800543 }});
544
545 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
546 new CachedServiceFetcher<EthernetManager>() {
547 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600548 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
549 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800550 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
551 return new EthernetManager(ctx.getOuterContext(), service);
552 }});
553
554 registerService(Context.WINDOW_SERVICE, WindowManager.class,
555 new CachedServiceFetcher<WindowManager>() {
556 @Override
557 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700558 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800559 }});
560
561 registerService(Context.USER_SERVICE, UserManager.class,
562 new CachedServiceFetcher<UserManager>() {
563 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600564 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
565 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800566 IUserManager service = IUserManager.Stub.asInterface(b);
567 return new UserManager(ctx, service);
568 }});
569
570 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
571 new CachedServiceFetcher<AppOpsManager>() {
572 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600573 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
574 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800575 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
576 return new AppOpsManager(ctx, service);
577 }});
578
579 registerService(Context.CAMERA_SERVICE, CameraManager.class,
580 new CachedServiceFetcher<CameraManager>() {
581 @Override
582 public CameraManager createService(ContextImpl ctx) {
583 return new CameraManager(ctx);
584 }});
585
586 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
587 new CachedServiceFetcher<LauncherApps>() {
588 @Override
589 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700590 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800591 }});
592
593 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
594 new CachedServiceFetcher<RestrictionsManager>() {
595 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600596 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
597 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800598 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
599 return new RestrictionsManager(ctx, service);
600 }});
601
602 registerService(Context.PRINT_SERVICE, PrintManager.class,
603 new CachedServiceFetcher<PrintManager>() {
604 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600605 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
606 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.PRINT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800607 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
608 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
609 UserHandle.getAppId(Process.myUid()));
610 }});
611
612 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
613 new CachedServiceFetcher<ConsumerIrManager>() {
614 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600615 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800616 return new ConsumerIrManager(ctx);
617 }});
618
619 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
620 new CachedServiceFetcher<MediaSessionManager>() {
621 @Override
622 public MediaSessionManager createService(ContextImpl ctx) {
623 return new MediaSessionManager(ctx);
624 }});
625
626 registerService(Context.TRUST_SERVICE, TrustManager.class,
627 new StaticServiceFetcher<TrustManager>() {
628 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600629 public TrustManager createService() throws ServiceNotFoundException {
630 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800631 return new TrustManager(b);
632 }});
633
634 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
635 new CachedServiceFetcher<FingerprintManager>() {
636 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600637 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
638 IBinder binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800639 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
640 return new FingerprintManager(ctx.getOuterContext(), service);
641 }});
642
643 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
644 new StaticServiceFetcher<TvInputManager>() {
645 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600646 public TvInputManager createService() throws ServiceNotFoundException {
647 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800648 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
649 return new TvInputManager(service, UserHandle.myUserId());
650 }});
651
652 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
653 new CachedServiceFetcher<NetworkScoreManager>() {
654 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600655 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800656 return new NetworkScoreManager(ctx);
657 }});
658
659 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
660 new CachedServiceFetcher<UsageStatsManager>() {
661 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600662 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
663 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800664 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
665 return new UsageStatsManager(ctx.getOuterContext(), service);
666 }});
667
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000668 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
669 new CachedServiceFetcher<NetworkStatsManager>() {
670 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600671 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000672 return new NetworkStatsManager(ctx.getOuterContext());
673 }});
674
Jeff Brown6e539312015-02-24 18:53:21 -0800675 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
676 new StaticServiceFetcher<JobScheduler>() {
677 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600678 public JobScheduler createService() throws ServiceNotFoundException {
679 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800680 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
681 }});
682
683 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
684 new StaticServiceFetcher<PersistentDataBlockManager>() {
685 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600686 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
687 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800688 IPersistentDataBlockService persistentDataBlockService =
689 IPersistentDataBlockService.Stub.asInterface(b);
690 if (persistentDataBlockService != null) {
691 return new PersistentDataBlockManager(persistentDataBlockService);
692 } else {
693 // not supported
694 return null;
695 }
696 }});
697
698 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
699 new CachedServiceFetcher<MediaProjectionManager>() {
700 @Override
701 public MediaProjectionManager createService(ContextImpl ctx) {
702 return new MediaProjectionManager(ctx);
703 }});
704
705 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
706 new CachedServiceFetcher<AppWidgetManager>() {
707 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600708 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
709 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800710 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
711 }});
712
713 registerService(Context.MIDI_SERVICE, MidiManager.class,
714 new CachedServiceFetcher<MidiManager>() {
715 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600716 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
717 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -0700718 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800719 }});
Eric Laurent2035ac82015-03-05 15:18:44 -0800720
721 registerService(Context.RADIO_SERVICE, RadioManager.class,
722 new CachedServiceFetcher<RadioManager>() {
723 @Override
724 public RadioManager createService(ContextImpl ctx) {
725 return new RadioManager(ctx);
726 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800727
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100728 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
729 new CachedServiceFetcher<HardwarePropertiesManager>() {
730 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600731 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
732 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
733 IHardwarePropertiesManager service =
734 IHardwarePropertiesManager.Stub.asInterface(b);
735 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100736 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800737
738 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
739 new CachedServiceFetcher<SoundTriggerManager>() {
740 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600741 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
742 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800743 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
744 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800745
746 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
747 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -0800748 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600749 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
750 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
751 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -0800752 }});
Joe Onorato713fec82016-03-04 10:34:02 -0800753
754 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
755 new CachedServiceFetcher<SystemHealthManager>() {
756 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600757 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
758 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
759 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -0800760 }});
Peng Xu9ff7d222016-02-11 13:02:05 -0800761
762 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
763 new CachedServiceFetcher<ContextHubManager>() {
764 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600765 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -0800766 return new ContextHubManager(ctx.getOuterContext(),
767 ctx.mMainThread.getHandler().getLooper());
768 }});
Jeff Brown6e539312015-02-24 18:53:21 -0800769 }
770
771 /**
772 * Creates an array which is used to cache per-Context service instances.
773 */
774 public static Object[] createServiceCache() {
775 return new Object[sServiceCacheSize];
776 }
777
778 /**
779 * Gets a system service from a given context.
780 */
781 public static Object getSystemService(ContextImpl ctx, String name) {
782 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
783 return fetcher != null ? fetcher.getService(ctx) : null;
784 }
785
786 /**
Paul McLeane3383cc2015-05-08 11:41:20 -0700787 * Gets the name of the system-level service that is represented by the specified class.
Jeff Brown6e539312015-02-24 18:53:21 -0800788 */
789 public static String getSystemServiceName(Class<?> serviceClass) {
790 return SYSTEM_SERVICE_NAMES.get(serviceClass);
791 }
792
793 /**
794 * Statically registers a system service with the context.
795 * This method must be called during static initialization only.
796 */
797 private static <T> void registerService(String serviceName, Class<T> serviceClass,
798 ServiceFetcher<T> serviceFetcher) {
799 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
800 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
801 }
802
803 /**
804 * Base interface for classes that fetch services.
805 * These objects must only be created during static initialization.
806 */
807 static abstract interface ServiceFetcher<T> {
808 T getService(ContextImpl ctx);
809 }
810
811 /**
812 * Override this class when the system service constructor needs a
813 * ContextImpl and should be cached and retained by that context.
814 */
815 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
816 private final int mCacheIndex;
817
818 public CachedServiceFetcher() {
819 mCacheIndex = sServiceCacheSize++;
820 }
821
822 @Override
823 @SuppressWarnings("unchecked")
824 public final T getService(ContextImpl ctx) {
825 final Object[] cache = ctx.mServiceCache;
826 synchronized (cache) {
827 // Fetch or create the service.
828 Object service = cache[mCacheIndex];
829 if (service == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600830 try {
831 service = createService(ctx);
832 cache[mCacheIndex] = service;
833 } catch (ServiceNotFoundException e) {
Jeff Sharkeyd79d2032016-08-23 13:39:07 -0600834 Log.wtf(TAG, e.getMessage(), e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600835 }
Jeff Brown6e539312015-02-24 18:53:21 -0800836 }
837 return (T)service;
838 }
839 }
840
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600841 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -0800842 }
843
844 /**
845 * Override this class when the system service does not need a ContextImpl
846 * and should be cached and retained process-wide.
847 */
848 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
849 private T mCachedInstance;
850
851 @Override
852 public final T getService(ContextImpl unused) {
853 synchronized (StaticServiceFetcher.this) {
854 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600855 try {
856 mCachedInstance = createService();
857 } catch (ServiceNotFoundException e) {
Jeff Sharkeyd79d2032016-08-23 13:39:07 -0600858 Log.wtf(TAG, e.getMessage(), e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600859 }
Jeff Brown6e539312015-02-24 18:53:21 -0800860 }
861 return mCachedInstance;
862 }
863 }
864
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600865 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -0800866 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900867
868 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +0900869 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
870 * creating the service for the first time, passes it the application context of the creating
871 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900872 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900873 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
874 * case where multiple application components each have their own ConnectivityManager object.
875 */
Lorenzo Colitticf959772016-02-29 16:07:37 +0900876 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900877 private T mCachedInstance;
878
879 @Override
880 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +0900881 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900882 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +0900883 Context appContext = ctx.getApplicationContext();
884 // If the application context is null, we're either in the system process or
885 // it's the application context very early in app initialization. In both these
886 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
887 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600888 try {
889 mCachedInstance = createService(appContext != null ? appContext : ctx);
890 } catch (ServiceNotFoundException e) {
Jeff Sharkeyd79d2032016-08-23 13:39:07 -0600891 Log.wtf(TAG, e.getMessage(), e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600892 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900893 }
894 return mCachedInstance;
895 }
896 }
897
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600898 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900899 }
Jeff Brown6e539312015-02-24 18:53:21 -0800900}