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