Home | History | Annotate | Download | only in app
      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 android.accounts.AccountManager;
     20 import android.accounts.IAccountManager;
     21 import android.app.admin.DevicePolicyManager;
     22 import android.app.admin.IDevicePolicyManager;
     23 import android.app.job.IJobScheduler;
     24 import android.app.job.JobScheduler;
     25 import android.app.trust.TrustManager;
     26 import android.app.usage.IStorageStatsManager;
     27 import android.app.usage.IUsageStatsManager;
     28 import android.app.usage.NetworkStatsManager;
     29 import android.app.usage.StorageStatsManager;
     30 import android.app.usage.UsageStatsManager;
     31 import android.appwidget.AppWidgetManager;
     32 import android.bluetooth.BluetoothManager;
     33 import android.companion.CompanionDeviceManager;
     34 import android.companion.ICompanionDeviceManager;
     35 import android.content.ClipboardManager;
     36 import android.content.Context;
     37 import android.content.IRestrictionsManager;
     38 import android.content.RestrictionsManager;
     39 import android.content.pm.IShortcutService;
     40 import android.content.pm.LauncherApps;
     41 import android.content.pm.ShortcutManager;
     42 import android.content.res.Resources;
     43 import android.hardware.ConsumerIrManager;
     44 import android.hardware.ISerialManager;
     45 import android.hardware.SensorManager;
     46 import android.hardware.SerialManager;
     47 import android.hardware.SystemSensorManager;
     48 import android.hardware.camera2.CameraManager;
     49 import android.hardware.display.DisplayManager;
     50 import android.hardware.fingerprint.FingerprintManager;
     51 import android.hardware.fingerprint.IFingerprintService;
     52 import android.hardware.hdmi.HdmiControlManager;
     53 import android.hardware.hdmi.IHdmiControlService;
     54 import android.hardware.input.InputManager;
     55 import android.hardware.location.ContextHubManager;
     56 import android.hardware.radio.RadioManager;
     57 import android.hardware.usb.IUsbManager;
     58 import android.hardware.usb.UsbManager;
     59 import android.location.CountryDetector;
     60 import android.location.ICountryDetector;
     61 import android.location.ILocationManager;
     62 import android.location.LocationManager;
     63 import android.media.AudioManager;
     64 import android.media.MediaRouter;
     65 import android.media.midi.IMidiManager;
     66 import android.media.midi.MidiManager;
     67 import android.media.projection.MediaProjectionManager;
     68 import android.media.session.MediaSessionManager;
     69 import android.media.soundtrigger.SoundTriggerManager;
     70 import android.media.tv.ITvInputManager;
     71 import android.media.tv.TvInputManager;
     72 import android.net.ConnectivityManager;
     73 import android.net.ConnectivityThread;
     74 import android.net.EthernetManager;
     75 import android.net.IConnectivityManager;
     76 import android.net.IEthernetManager;
     77 import android.net.IIpSecService;
     78 import android.net.INetworkPolicyManager;
     79 import android.net.IpSecManager;
     80 import android.net.NetworkPolicyManager;
     81 import android.net.NetworkScoreManager;
     82 import android.net.nsd.INsdManager;
     83 import android.net.nsd.NsdManager;
     84 import android.net.wifi.IRttManager;
     85 import android.net.wifi.IWifiManager;
     86 import android.net.wifi.IWifiScanner;
     87 import android.net.wifi.RttManager;
     88 import android.net.wifi.WifiManager;
     89 import android.net.wifi.WifiScanner;
     90 import android.net.wifi.aware.IWifiAwareManager;
     91 import android.net.wifi.aware.WifiAwareManager;
     92 import android.net.wifi.p2p.IWifiP2pManager;
     93 import android.net.wifi.p2p.WifiP2pManager;
     94 import android.nfc.NfcManager;
     95 import android.os.BatteryManager;
     96 import android.os.BatteryStats;
     97 import android.os.Build;
     98 import android.os.DropBoxManager;
     99 import android.os.HardwarePropertiesManager;
    100 import android.os.IBatteryPropertiesRegistrar;
    101 import android.os.IBinder;
    102 import android.os.IHardwarePropertiesManager;
    103 import android.os.IPowerManager;
    104 import android.os.IRecoverySystem;
    105 import android.os.IUserManager;
    106 import android.os.IncidentManager;
    107 import android.os.PowerManager;
    108 import android.os.Process;
    109 import android.os.RecoverySystem;
    110 import android.os.ServiceManager;
    111 import android.os.ServiceManager.ServiceNotFoundException;
    112 import android.os.SystemVibrator;
    113 import android.os.UserHandle;
    114 import android.os.UserManager;
    115 import android.os.Vibrator;
    116 import android.os.health.SystemHealthManager;
    117 import android.os.storage.StorageManager;
    118 import android.print.IPrintManager;
    119 import android.print.PrintManager;
    120 import android.service.oemlock.IOemLockService;
    121 import android.service.oemlock.OemLockManager;
    122 import android.service.persistentdata.IPersistentDataBlockService;
    123 import android.service.persistentdata.PersistentDataBlockManager;
    124 import android.service.vr.IVrManager;
    125 import android.telecom.TelecomManager;
    126 import android.telephony.CarrierConfigManager;
    127 import android.telephony.SubscriptionManager;
    128 import android.telephony.TelephonyManager;
    129 import android.util.Log;
    130 import android.view.ContextThemeWrapper;
    131 import android.view.LayoutInflater;
    132 import android.view.WindowManager;
    133 import android.view.WindowManagerImpl;
    134 import android.view.accessibility.AccessibilityManager;
    135 import android.view.accessibility.CaptioningManager;
    136 import android.view.autofill.AutofillManager;
    137 import android.view.autofill.IAutoFillManager;
    138 import android.view.inputmethod.InputMethodManager;
    139 import android.view.textclassifier.TextClassificationManager;
    140 import android.view.textservice.TextServicesManager;
    141 
    142 import com.android.internal.app.IAppOpsService;
    143 import com.android.internal.app.IBatteryStats;
    144 import com.android.internal.app.ISoundTriggerService;
    145 import com.android.internal.appwidget.IAppWidgetService;
    146 import com.android.internal.os.IDropBoxManagerService;
    147 import com.android.internal.policy.PhoneLayoutInflater;
    148 
    149 import java.util.HashMap;
    150 
    151 /**
    152  * Manages all of the system services that can be returned by {@link Context#getSystemService}.
    153  * Used by {@link ContextImpl}.
    154  */
    155 final class SystemServiceRegistry {
    156     private static final String TAG = "SystemServiceRegistry";
    157 
    158     // Service registry information.
    159     // This information is never changed once static initialization has completed.
    160     private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
    161             new HashMap<Class<?>, String>();
    162     private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
    163             new HashMap<String, ServiceFetcher<?>>();
    164     private static int sServiceCacheSize;
    165 
    166     // Not instantiable.
    167     private SystemServiceRegistry() { }
    168 
    169     static {
    170         registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
    171                 new CachedServiceFetcher<AccessibilityManager>() {
    172             @Override
    173             public AccessibilityManager createService(ContextImpl ctx) {
    174                 return AccessibilityManager.getInstance(ctx);
    175             }});
    176 
    177         registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
    178                 new CachedServiceFetcher<CaptioningManager>() {
    179             @Override
    180             public CaptioningManager createService(ContextImpl ctx) {
    181                 return new CaptioningManager(ctx);
    182             }});
    183 
    184         registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
    185                 new CachedServiceFetcher<AccountManager>() {
    186             @Override
    187             public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    188                 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
    189                 IAccountManager service = IAccountManager.Stub.asInterface(b);
    190                 return new AccountManager(ctx, service);
    191             }});
    192 
    193         registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
    194                 new CachedServiceFetcher<ActivityManager>() {
    195             @Override
    196             public ActivityManager createService(ContextImpl ctx) {
    197                 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
    198             }});
    199 
    200         registerService(Context.ALARM_SERVICE, AlarmManager.class,
    201                 new CachedServiceFetcher<AlarmManager>() {
    202             @Override
    203             public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    204                 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
    205                 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
    206                 return new AlarmManager(service, ctx);
    207             }});
    208 
    209         registerService(Context.AUDIO_SERVICE, AudioManager.class,
    210                 new CachedServiceFetcher<AudioManager>() {
    211             @Override
    212             public AudioManager createService(ContextImpl ctx) {
    213                 return new AudioManager(ctx);
    214             }});
    215 
    216         registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
    217                 new CachedServiceFetcher<MediaRouter>() {
    218             @Override
    219             public MediaRouter createService(ContextImpl ctx) {
    220                 return new MediaRouter(ctx);
    221             }});
    222 
    223         registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
    224                 new CachedServiceFetcher<BluetoothManager>() {
    225             @Override
    226             public BluetoothManager createService(ContextImpl ctx) {
    227                 return new BluetoothManager(ctx);
    228             }});
    229 
    230         registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
    231                 new StaticServiceFetcher<HdmiControlManager>() {
    232             @Override
    233             public HdmiControlManager createService() throws ServiceNotFoundException {
    234                 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
    235                 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
    236             }});
    237 
    238         registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
    239                 new CachedServiceFetcher<TextClassificationManager>() {
    240             @Override
    241             public TextClassificationManager createService(ContextImpl ctx) {
    242                 return new TextClassificationManager(ctx);
    243             }});
    244 
    245         registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
    246                 new CachedServiceFetcher<ClipboardManager>() {
    247             @Override
    248             public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    249                 return new ClipboardManager(ctx.getOuterContext(),
    250                         ctx.mMainThread.getHandler());
    251             }});
    252 
    253         // The clipboard service moved to a new package.  If someone asks for the old
    254         // interface by class then we want to redirect over to the new interface instead
    255         // (which extends it).
    256         SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
    257 
    258         registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
    259                 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
    260             @Override
    261             public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
    262                 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
    263                 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
    264                 return new ConnectivityManager(context, service);
    265             }});
    266 
    267         registerService(Context.IPSEC_SERVICE, IpSecManager.class,
    268                 new StaticServiceFetcher<IpSecManager>() {
    269             @Override
    270             public IpSecManager createService() {
    271                 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
    272                 IIpSecService service = IIpSecService.Stub.asInterface(b);
    273                 return new IpSecManager(service);
    274             }});
    275 
    276         registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
    277                 new StaticServiceFetcher<CountryDetector>() {
    278             @Override
    279             public CountryDetector createService() throws ServiceNotFoundException {
    280                 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
    281                 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
    282             }});
    283 
    284         registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
    285                 new CachedServiceFetcher<DevicePolicyManager>() {
    286             @Override
    287             public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    288                 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
    289                 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
    290             }});
    291 
    292         registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
    293                 new CachedServiceFetcher<DownloadManager>() {
    294             @Override
    295             public DownloadManager createService(ContextImpl ctx) {
    296                 return new DownloadManager(ctx);
    297             }});
    298 
    299         registerService(Context.BATTERY_SERVICE, BatteryManager.class,
    300                 new StaticServiceFetcher<BatteryManager>() {
    301             @Override
    302             public BatteryManager createService() throws ServiceNotFoundException {
    303                 IBatteryStats stats = IBatteryStats.Stub.asInterface(
    304                         ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
    305                 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
    306                         .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
    307                 return new BatteryManager(stats, registrar);
    308             }});
    309 
    310         registerService(Context.NFC_SERVICE, NfcManager.class,
    311                 new CachedServiceFetcher<NfcManager>() {
    312             @Override
    313             public NfcManager createService(ContextImpl ctx) {
    314                 return new NfcManager(ctx);
    315             }});
    316 
    317         registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
    318                 new CachedServiceFetcher<DropBoxManager>() {
    319             @Override
    320             public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    321                 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
    322                 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
    323                 return new DropBoxManager(ctx, service);
    324             }});
    325 
    326         registerService(Context.INPUT_SERVICE, InputManager.class,
    327                 new StaticServiceFetcher<InputManager>() {
    328             @Override
    329             public InputManager createService() {
    330                 return InputManager.getInstance();
    331             }});
    332 
    333         registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
    334                 new CachedServiceFetcher<DisplayManager>() {
    335             @Override
    336             public DisplayManager createService(ContextImpl ctx) {
    337                 return new DisplayManager(ctx.getOuterContext());
    338             }});
    339 
    340         registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
    341                 new StaticServiceFetcher<InputMethodManager>() {
    342             @Override
    343             public InputMethodManager createService() {
    344                 return InputMethodManager.getInstance();
    345             }});
    346 
    347         registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
    348                 new StaticServiceFetcher<TextServicesManager>() {
    349             @Override
    350             public TextServicesManager createService() {
    351                 return TextServicesManager.getInstance();
    352             }});
    353 
    354         registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
    355                 new CachedServiceFetcher<KeyguardManager>() {
    356             @Override
    357             public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    358                 return new KeyguardManager(ctx);
    359             }});
    360 
    361         registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
    362                 new CachedServiceFetcher<LayoutInflater>() {
    363             @Override
    364             public LayoutInflater createService(ContextImpl ctx) {
    365                 return new PhoneLayoutInflater(ctx.getOuterContext());
    366             }});
    367 
    368         registerService(Context.LOCATION_SERVICE, LocationManager.class,
    369                 new CachedServiceFetcher<LocationManager>() {
    370             @Override
    371             public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    372                 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
    373                 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
    374             }});
    375 
    376         registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
    377                 new CachedServiceFetcher<NetworkPolicyManager>() {
    378             @Override
    379             public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    380                 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
    381                         ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
    382             }});
    383 
    384         registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
    385                 new CachedServiceFetcher<NotificationManager>() {
    386             @Override
    387             public NotificationManager createService(ContextImpl ctx) {
    388                 final Context outerContext = ctx.getOuterContext();
    389                 return new NotificationManager(
    390                     new ContextThemeWrapper(outerContext,
    391                             Resources.selectSystemTheme(0,
    392                                     outerContext.getApplicationInfo().targetSdkVersion,
    393                                     com.android.internal.R.style.Theme_Dialog,
    394                                     com.android.internal.R.style.Theme_Holo_Dialog,
    395                                     com.android.internal.R.style.Theme_DeviceDefault_Dialog,
    396                                     com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
    397                     ctx.mMainThread.getHandler());
    398             }});
    399 
    400         registerService(Context.NSD_SERVICE, NsdManager.class,
    401                 new CachedServiceFetcher<NsdManager>() {
    402             @Override
    403             public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    404                 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
    405                 INsdManager service = INsdManager.Stub.asInterface(b);
    406                 return new NsdManager(ctx.getOuterContext(), service);
    407             }});
    408 
    409         registerService(Context.POWER_SERVICE, PowerManager.class,
    410                 new CachedServiceFetcher<PowerManager>() {
    411             @Override
    412             public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    413                 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
    414                 IPowerManager service = IPowerManager.Stub.asInterface(b);
    415                 return new PowerManager(ctx.getOuterContext(),
    416                         service, ctx.mMainThread.getHandler());
    417             }});
    418 
    419         registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
    420                 new CachedServiceFetcher<RecoverySystem>() {
    421             @Override
    422             public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
    423                 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
    424                 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
    425                 return new RecoverySystem(service);
    426             }});
    427 
    428         registerService(Context.SEARCH_SERVICE, SearchManager.class,
    429                 new CachedServiceFetcher<SearchManager>() {
    430             @Override
    431             public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    432                 return new SearchManager(ctx.getOuterContext(),
    433                         ctx.mMainThread.getHandler());
    434             }});
    435 
    436         registerService(Context.SENSOR_SERVICE, SensorManager.class,
    437                 new CachedServiceFetcher<SensorManager>() {
    438             @Override
    439             public SensorManager createService(ContextImpl ctx) {
    440                 return new SystemSensorManager(ctx.getOuterContext(),
    441                   ctx.mMainThread.getHandler().getLooper());
    442             }});
    443 
    444         registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
    445                 new CachedServiceFetcher<StatusBarManager>() {
    446             @Override
    447             public StatusBarManager createService(ContextImpl ctx) {
    448                 return new StatusBarManager(ctx.getOuterContext());
    449             }});
    450 
    451         registerService(Context.STORAGE_SERVICE, StorageManager.class,
    452                 new CachedServiceFetcher<StorageManager>() {
    453             @Override
    454             public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    455                 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
    456             }});
    457 
    458         registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
    459                 new CachedServiceFetcher<StorageStatsManager>() {
    460             @Override
    461             public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    462                 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
    463                         ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
    464                 return new StorageStatsManager(ctx, service);
    465             }});
    466 
    467         registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
    468                 new CachedServiceFetcher<TelephonyManager>() {
    469             @Override
    470             public TelephonyManager createService(ContextImpl ctx) {
    471                 return new TelephonyManager(ctx.getOuterContext());
    472             }});
    473 
    474         registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
    475                 new CachedServiceFetcher<SubscriptionManager>() {
    476             @Override
    477             public SubscriptionManager createService(ContextImpl ctx) {
    478                 return new SubscriptionManager(ctx.getOuterContext());
    479             }});
    480 
    481         registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
    482                 new CachedServiceFetcher<CarrierConfigManager>() {
    483             @Override
    484             public CarrierConfigManager createService(ContextImpl ctx) {
    485                 return new CarrierConfigManager();
    486             }});
    487 
    488         registerService(Context.TELECOM_SERVICE, TelecomManager.class,
    489                 new CachedServiceFetcher<TelecomManager>() {
    490             @Override
    491             public TelecomManager createService(ContextImpl ctx) {
    492                 return new TelecomManager(ctx.getOuterContext());
    493             }});
    494 
    495         registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
    496                 new CachedServiceFetcher<UiModeManager>() {
    497             @Override
    498             public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    499                 return new UiModeManager();
    500             }});
    501 
    502         registerService(Context.USB_SERVICE, UsbManager.class,
    503                 new CachedServiceFetcher<UsbManager>() {
    504             @Override
    505             public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    506                 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
    507                 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
    508             }});
    509 
    510         registerService(Context.SERIAL_SERVICE, SerialManager.class,
    511                 new CachedServiceFetcher<SerialManager>() {
    512             @Override
    513             public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    514                 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
    515                 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
    516             }});
    517 
    518         registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
    519                 new CachedServiceFetcher<Vibrator>() {
    520             @Override
    521             public Vibrator createService(ContextImpl ctx) {
    522                 return new SystemVibrator(ctx);
    523             }});
    524 
    525         registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
    526                 new CachedServiceFetcher<WallpaperManager>() {
    527             @Override
    528             public WallpaperManager createService(ContextImpl ctx) {
    529                 return new WallpaperManager(ctx.getOuterContext(),
    530                         ctx.mMainThread.getHandler());
    531             }});
    532 
    533         registerService(Context.WIFI_SERVICE, WifiManager.class,
    534                 new CachedServiceFetcher<WifiManager>() {
    535             @Override
    536             public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    537                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
    538                 IWifiManager service = IWifiManager.Stub.asInterface(b);
    539                 return new WifiManager(ctx.getOuterContext(), service,
    540                         ConnectivityThread.getInstanceLooper());
    541             }});
    542 
    543         registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
    544                 new StaticServiceFetcher<WifiP2pManager>() {
    545             @Override
    546             public WifiP2pManager createService() throws ServiceNotFoundException {
    547                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
    548                 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
    549                 return new WifiP2pManager(service);
    550             }});
    551 
    552         registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
    553                 new CachedServiceFetcher<WifiAwareManager>() {
    554             @Override
    555             public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    556                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
    557                 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
    558                 if (service == null) {
    559                     return null;
    560                 }
    561                 return new WifiAwareManager(ctx.getOuterContext(), service);
    562             }});
    563 
    564         registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
    565                 new CachedServiceFetcher<WifiScanner>() {
    566             @Override
    567             public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
    568                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
    569                 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
    570                 return new WifiScanner(ctx.getOuterContext(), service,
    571                         ConnectivityThread.getInstanceLooper());
    572             }});
    573 
    574         registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
    575                 new CachedServiceFetcher<RttManager>() {
    576             @Override
    577             public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    578                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_SERVICE);
    579                 IRttManager service = IRttManager.Stub.asInterface(b);
    580                 return new RttManager(ctx.getOuterContext(), service,
    581                         ConnectivityThread.getInstanceLooper());
    582             }});
    583 
    584         registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
    585                 new CachedServiceFetcher<EthernetManager>() {
    586             @Override
    587             public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    588                 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
    589                 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
    590                 return new EthernetManager(ctx.getOuterContext(), service);
    591             }});
    592 
    593         registerService(Context.WINDOW_SERVICE, WindowManager.class,
    594                 new CachedServiceFetcher<WindowManager>() {
    595             @Override
    596             public WindowManager createService(ContextImpl ctx) {
    597                 return new WindowManagerImpl(ctx);
    598             }});
    599 
    600         registerService(Context.USER_SERVICE, UserManager.class,
    601                 new CachedServiceFetcher<UserManager>() {
    602             @Override
    603             public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    604                 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
    605                 IUserManager service = IUserManager.Stub.asInterface(b);
    606                 return new UserManager(ctx, service);
    607             }});
    608 
    609         registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
    610                 new CachedServiceFetcher<AppOpsManager>() {
    611             @Override
    612             public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    613                 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
    614                 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
    615                 return new AppOpsManager(ctx, service);
    616             }});
    617 
    618         registerService(Context.CAMERA_SERVICE, CameraManager.class,
    619                 new CachedServiceFetcher<CameraManager>() {
    620             @Override
    621             public CameraManager createService(ContextImpl ctx) {
    622                 return new CameraManager(ctx);
    623             }});
    624 
    625         registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
    626                 new CachedServiceFetcher<LauncherApps>() {
    627             @Override
    628             public LauncherApps createService(ContextImpl ctx) {
    629                 return new LauncherApps(ctx);
    630             }});
    631 
    632         registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
    633                 new CachedServiceFetcher<RestrictionsManager>() {
    634             @Override
    635             public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    636                 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
    637                 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
    638                 return new RestrictionsManager(ctx, service);
    639             }});
    640 
    641         registerService(Context.PRINT_SERVICE, PrintManager.class,
    642                 new CachedServiceFetcher<PrintManager>() {
    643             @Override
    644             public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    645                 // Get the services without throwing as this is an optional feature
    646                 IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
    647                 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
    648                 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
    649                         UserHandle.getAppId(Process.myUid()));
    650             }});
    651 
    652         registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
    653                 new CachedServiceFetcher<CompanionDeviceManager>() {
    654                     @Override
    655                     public CompanionDeviceManager createService(ContextImpl ctx)
    656                             throws ServiceNotFoundException {
    657                         // Get the services without throwing as this is an optional feature
    658                         IBinder iBinder =
    659                                 ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE);
    660                         ICompanionDeviceManager service =
    661                                 ICompanionDeviceManager.Stub.asInterface(iBinder);
    662                         return new CompanionDeviceManager(service, ctx.getOuterContext());
    663                     }});
    664 
    665         registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
    666                 new CachedServiceFetcher<ConsumerIrManager>() {
    667             @Override
    668             public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    669                 return new ConsumerIrManager(ctx);
    670             }});
    671 
    672         registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
    673                 new CachedServiceFetcher<MediaSessionManager>() {
    674             @Override
    675             public MediaSessionManager createService(ContextImpl ctx) {
    676                 return new MediaSessionManager(ctx);
    677             }});
    678 
    679         registerService(Context.TRUST_SERVICE, TrustManager.class,
    680                 new StaticServiceFetcher<TrustManager>() {
    681             @Override
    682             public TrustManager createService() throws ServiceNotFoundException {
    683                 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
    684                 return new TrustManager(b);
    685             }});
    686 
    687         registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
    688                 new CachedServiceFetcher<FingerprintManager>() {
    689             @Override
    690             public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    691                 final IBinder binder;
    692                 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
    693                     binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
    694                 } else {
    695                     binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
    696                 }
    697                 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
    698                 return new FingerprintManager(ctx.getOuterContext(), service);
    699             }});
    700 
    701         registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
    702                 new StaticServiceFetcher<TvInputManager>() {
    703             @Override
    704             public TvInputManager createService() throws ServiceNotFoundException {
    705                 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
    706                 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
    707                 return new TvInputManager(service, UserHandle.myUserId());
    708             }});
    709 
    710         registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
    711                 new CachedServiceFetcher<NetworkScoreManager>() {
    712             @Override
    713             public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    714                 return new NetworkScoreManager(ctx);
    715             }});
    716 
    717         registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
    718                 new CachedServiceFetcher<UsageStatsManager>() {
    719             @Override
    720             public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    721                 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
    722                 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
    723                 return new UsageStatsManager(ctx.getOuterContext(), service);
    724             }});
    725 
    726         registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
    727                 new CachedServiceFetcher<NetworkStatsManager>() {
    728             @Override
    729             public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    730                 return new NetworkStatsManager(ctx.getOuterContext());
    731             }});
    732 
    733         registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
    734                 new StaticServiceFetcher<JobScheduler>() {
    735             @Override
    736             public JobScheduler createService() throws ServiceNotFoundException {
    737                 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
    738                 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
    739             }});
    740 
    741         registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
    742                 new StaticServiceFetcher<PersistentDataBlockManager>() {
    743             @Override
    744             public PersistentDataBlockManager createService() throws ServiceNotFoundException {
    745                 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
    746                 IPersistentDataBlockService persistentDataBlockService =
    747                         IPersistentDataBlockService.Stub.asInterface(b);
    748                 if (persistentDataBlockService != null) {
    749                     return new PersistentDataBlockManager(persistentDataBlockService);
    750                 } else {
    751                     // not supported
    752                     return null;
    753                 }
    754             }});
    755 
    756         registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
    757                 new StaticServiceFetcher<OemLockManager>() {
    758             @Override
    759             public OemLockManager createService() throws ServiceNotFoundException {
    760                 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
    761                 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
    762                 if (oemLockService != null) {
    763                     return new OemLockManager(oemLockService);
    764                 } else {
    765                     // not supported
    766                     return null;
    767                 }
    768             }});
    769 
    770         registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
    771                 new CachedServiceFetcher<MediaProjectionManager>() {
    772             @Override
    773             public MediaProjectionManager createService(ContextImpl ctx) {
    774                 return new MediaProjectionManager(ctx);
    775             }});
    776 
    777         registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
    778                 new CachedServiceFetcher<AppWidgetManager>() {
    779             @Override
    780             public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    781                 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
    782                 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
    783             }});
    784 
    785         registerService(Context.MIDI_SERVICE, MidiManager.class,
    786                 new CachedServiceFetcher<MidiManager>() {
    787             @Override
    788             public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    789                 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
    790                 return new MidiManager(IMidiManager.Stub.asInterface(b));
    791             }});
    792 
    793         registerService(Context.RADIO_SERVICE, RadioManager.class,
    794                 new CachedServiceFetcher<RadioManager>() {
    795             @Override
    796             public RadioManager createService(ContextImpl ctx) {
    797                 return new RadioManager(ctx);
    798             }});
    799 
    800         registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
    801                 new CachedServiceFetcher<HardwarePropertiesManager>() {
    802             @Override
    803             public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    804                 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
    805                 IHardwarePropertiesManager service =
    806                         IHardwarePropertiesManager.Stub.asInterface(b);
    807                 return new HardwarePropertiesManager(ctx, service);
    808             }});
    809 
    810         registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
    811                 new CachedServiceFetcher<SoundTriggerManager>() {
    812             @Override
    813             public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    814                 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
    815                 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
    816             }});
    817 
    818         registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
    819                 new CachedServiceFetcher<ShortcutManager>() {
    820             @Override
    821             public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    822                 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
    823                 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
    824             }});
    825 
    826         registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
    827                 new CachedServiceFetcher<SystemHealthManager>() {
    828             @Override
    829             public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    830                 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
    831                 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
    832             }});
    833 
    834         registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
    835                 new CachedServiceFetcher<ContextHubManager>() {
    836             @Override
    837             public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    838                 return new ContextHubManager(ctx.getOuterContext(),
    839                   ctx.mMainThread.getHandler().getLooper());
    840             }});
    841 
    842         registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
    843                 new CachedServiceFetcher<IncidentManager>() {
    844             @Override
    845             public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    846                 return new IncidentManager(ctx);
    847             }});
    848 
    849         registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
    850                 new CachedServiceFetcher<AutofillManager>() {
    851             @Override
    852             public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    853                 // Get the services without throwing as this is an optional feature
    854                 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
    855                 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
    856                 return new AutofillManager(ctx.getOuterContext(), service);
    857             }});
    858 
    859         registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
    860             @Override
    861             public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    862                 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
    863                 return new VrManager(IVrManager.Stub.asInterface(b));
    864             }
    865         });
    866     }
    867 
    868     /**
    869      * Creates an array which is used to cache per-Context service instances.
    870      */
    871     public static Object[] createServiceCache() {
    872         return new Object[sServiceCacheSize];
    873     }
    874 
    875     /**
    876      * Gets a system service from a given context.
    877      */
    878     public static Object getSystemService(ContextImpl ctx, String name) {
    879         ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
    880         return fetcher != null ? fetcher.getService(ctx) : null;
    881     }
    882 
    883     /**
    884      * Gets the name of the system-level service that is represented by the specified class.
    885      */
    886     public static String getSystemServiceName(Class<?> serviceClass) {
    887         return SYSTEM_SERVICE_NAMES.get(serviceClass);
    888     }
    889 
    890     /**
    891      * Statically registers a system service with the context.
    892      * This method must be called during static initialization only.
    893      */
    894     private static <T> void registerService(String serviceName, Class<T> serviceClass,
    895             ServiceFetcher<T> serviceFetcher) {
    896         SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
    897         SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
    898     }
    899 
    900     /**
    901      * Base interface for classes that fetch services.
    902      * These objects must only be created during static initialization.
    903      */
    904     static abstract interface ServiceFetcher<T> {
    905         T getService(ContextImpl ctx);
    906     }
    907 
    908     /**
    909      * Override this class when the system service constructor needs a
    910      * ContextImpl and should be cached and retained by that context.
    911      */
    912     static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
    913         private final int mCacheIndex;
    914 
    915         public CachedServiceFetcher() {
    916             mCacheIndex = sServiceCacheSize++;
    917         }
    918 
    919         @Override
    920         @SuppressWarnings("unchecked")
    921         public final T getService(ContextImpl ctx) {
    922             final Object[] cache = ctx.mServiceCache;
    923             synchronized (cache) {
    924                 // Fetch or create the service.
    925                 Object service = cache[mCacheIndex];
    926                 if (service == null) {
    927                     try {
    928                         service = createService(ctx);
    929                         cache[mCacheIndex] = service;
    930                     } catch (ServiceNotFoundException e) {
    931                         onServiceNotFound(e);
    932                     }
    933                 }
    934                 return (T)service;
    935             }
    936         }
    937 
    938         public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
    939     }
    940 
    941     /**
    942      * Override this class when the system service does not need a ContextImpl
    943      * and should be cached and retained process-wide.
    944      */
    945     static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
    946         private T mCachedInstance;
    947 
    948         @Override
    949         public final T getService(ContextImpl ctx) {
    950             synchronized (StaticServiceFetcher.this) {
    951                 if (mCachedInstance == null) {
    952                     try {
    953                         mCachedInstance = createService();
    954                     } catch (ServiceNotFoundException e) {
    955                         onServiceNotFound(e);
    956                     }
    957                 }
    958                 return mCachedInstance;
    959             }
    960         }
    961 
    962         public abstract T createService() throws ServiceNotFoundException;
    963     }
    964 
    965     /**
    966      * Like StaticServiceFetcher, creates only one instance of the service per application, but when
    967      * creating the service for the first time, passes it the application context of the creating
    968      * application.
    969      *
    970      * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
    971      * case where multiple application components each have their own ConnectivityManager object.
    972      */
    973     static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
    974         private T mCachedInstance;
    975 
    976         @Override
    977         public final T getService(ContextImpl ctx) {
    978             synchronized (StaticApplicationContextServiceFetcher.this) {
    979                 if (mCachedInstance == null) {
    980                     Context appContext = ctx.getApplicationContext();
    981                     // If the application context is null, we're either in the system process or
    982                     // it's the application context very early in app initialization. In both these
    983                     // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
    984                     // to the service. http://b/27532714 .
    985                     try {
    986                         mCachedInstance = createService(appContext != null ? appContext : ctx);
    987                     } catch (ServiceNotFoundException e) {
    988                         onServiceNotFound(e);
    989                     }
    990                 }
    991                 return mCachedInstance;
    992             }
    993         }
    994 
    995         public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
    996     }
    997 
    998     public static void onServiceNotFound(ServiceNotFoundException e) {
    999         // We're mostly interested in tracking down long-lived core system
   1000         // components that might stumble if they obtain bad references; just
   1001         // emit a tidy log message for normal apps
   1002         if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
   1003             Log.wtf(TAG, e.getMessage(), e);
   1004         } else {
   1005             Log.w(TAG, e.getMessage());
   1006         }
   1007     }
   1008 }
   1009