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