Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 19 | import android.accounts.AccountManager; |
| 20 | import android.accounts.IAccountManager; |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 21 | import android.app.ContextImpl.ServiceInitializationState; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 22 | import android.app.admin.DevicePolicyManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 23 | import android.app.admin.IDevicePolicyManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 24 | import android.app.job.IJobScheduler; |
| 25 | import android.app.job.JobScheduler; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 26 | import android.app.slice.SliceManager; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 27 | import android.app.timedetector.TimeDetector; |
Neil Fuller | 328532a | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 28 | import android.app.timezone.RulesManager; |
Neil Fuller | cccc48d | 2018-06-19 12:53:47 +0100 | [diff] [blame] | 29 | import android.app.timezonedetector.TimeZoneDetector; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 30 | import android.app.trust.TrustManager; |
Jeff Sharkey | e8cece9 | 2017-01-04 11:33:33 -0700 | [diff] [blame] | 31 | import android.app.usage.IStorageStatsManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 32 | import android.app.usage.IUsageStatsManager; |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 33 | import android.app.usage.NetworkStatsManager; |
Jeff Sharkey | e8cece9 | 2017-01-04 11:33:33 -0700 | [diff] [blame] | 34 | import android.app.usage.StorageStatsManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 35 | import android.app.usage.UsageStatsManager; |
| 36 | import android.appwidget.AppWidgetManager; |
| 37 | import android.bluetooth.BluetoothManager; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 38 | import android.companion.CompanionDeviceManager; |
| 39 | import android.companion.ICompanionDeviceManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 40 | import android.content.ClipboardManager; |
| 41 | import android.content.Context; |
| 42 | import android.content.IRestrictionsManager; |
| 43 | import android.content.RestrictionsManager; |
Tony Mak | b0d2262 | 2018-01-18 12:49:49 +0000 | [diff] [blame] | 44 | import android.content.pm.CrossProfileApps; |
| 45 | import android.content.pm.ICrossProfileApps; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 46 | import android.content.pm.IShortcutService; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 47 | import android.content.pm.LauncherApps; |
Svet Ganov | ec7daa5 | 2017-06-30 12:02:59 -0700 | [diff] [blame] | 48 | import android.content.pm.PackageManager; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 49 | import android.content.pm.ShortcutManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 50 | import android.content.res.Resources; |
| 51 | import android.hardware.ConsumerIrManager; |
| 52 | import android.hardware.ISerialManager; |
| 53 | import android.hardware.SensorManager; |
| 54 | import android.hardware.SerialManager; |
| 55 | import android.hardware.SystemSensorManager; |
| 56 | import android.hardware.camera2.CameraManager; |
| 57 | import android.hardware.display.DisplayManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 58 | import android.hardware.fingerprint.FingerprintManager; |
| 59 | import android.hardware.fingerprint.IFingerprintService; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 60 | import android.hardware.hdmi.HdmiControlManager; |
| 61 | import android.hardware.hdmi.IHdmiControlService; |
| 62 | import android.hardware.input.InputManager; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 63 | import android.hardware.location.ContextHubManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 64 | import android.hardware.radio.RadioManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 65 | import android.hardware.usb.IUsbManager; |
| 66 | import android.hardware.usb.UsbManager; |
| 67 | import android.location.CountryDetector; |
| 68 | import android.location.ICountryDetector; |
| 69 | import android.location.ILocationManager; |
| 70 | import android.location.LocationManager; |
| 71 | import android.media.AudioManager; |
| 72 | import android.media.MediaRouter; |
| 73 | import android.media.midi.IMidiManager; |
| 74 | import android.media.midi.MidiManager; |
| 75 | import android.media.projection.MediaProjectionManager; |
| 76 | import android.media.session.MediaSessionManager; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 77 | import android.media.soundtrigger.SoundTriggerManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 78 | import android.media.tv.ITvInputManager; |
| 79 | import android.media.tv.TvInputManager; |
| 80 | import android.net.ConnectivityManager; |
Paul Stewart | bf8cbb05 | 2016-03-17 10:11:54 -0700 | [diff] [blame] | 81 | import android.net.ConnectivityThread; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 82 | import android.net.EthernetManager; |
| 83 | import android.net.IConnectivityManager; |
| 84 | import android.net.IEthernetManager; |
Nathan Harold | 28084d8 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 85 | import android.net.IIpSecService; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 86 | import android.net.INetworkPolicyManager; |
Nathan Harold | 28084d8 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 87 | import android.net.IpSecManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 88 | import android.net.NetworkPolicyManager; |
| 89 | import android.net.NetworkScoreManager; |
Ricky Wai | 1a6e667 | 2017-10-27 14:46:01 +0100 | [diff] [blame] | 90 | import android.net.NetworkWatchlistManager; |
Robert Quattlebaum | 87a7104 | 2017-05-15 15:45:20 -0700 | [diff] [blame] | 91 | import android.net.lowpan.ILowpanManager; |
| 92 | import android.net.lowpan.LowpanManager; |
Etan Cohen | 17ba472 | 2017-08-21 10:52:17 -0700 | [diff] [blame] | 93 | import android.net.nsd.INsdManager; |
| 94 | import android.net.nsd.NsdManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 95 | import android.net.wifi.IWifiManager; |
| 96 | import android.net.wifi.IWifiScanner; |
| 97 | import android.net.wifi.RttManager; |
| 98 | import android.net.wifi.WifiManager; |
| 99 | import android.net.wifi.WifiScanner; |
Etan Cohen | 0413327 | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 100 | import android.net.wifi.aware.IWifiAwareManager; |
| 101 | import android.net.wifi.aware.WifiAwareManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 102 | import android.net.wifi.p2p.IWifiP2pManager; |
| 103 | import android.net.wifi.p2p.WifiP2pManager; |
Etan Cohen | 17ba472 | 2017-08-21 10:52:17 -0700 | [diff] [blame] | 104 | import android.net.wifi.rtt.IWifiRttManager; |
| 105 | import android.net.wifi.rtt.WifiRttManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 106 | import android.nfc.NfcManager; |
| 107 | import android.os.BatteryManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 108 | import android.os.BatteryStats; |
Jeff Sharkey | 351c2c2 | 2017-03-18 16:48:02 -0600 | [diff] [blame] | 109 | import android.os.Build; |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 110 | import android.os.DeviceIdleManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 111 | import android.os.DropBoxManager; |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 112 | import android.os.HardwarePropertiesManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 113 | import android.os.IBatteryPropertiesRegistrar; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 114 | import android.os.IBinder; |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 115 | import android.os.IDeviceIdleController; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 116 | import android.os.IHardwarePropertiesManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 117 | import android.os.IPowerManager; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 118 | import android.os.IRecoverySystem; |
Tao Bao | 07342dc | 2017-01-24 15:08:21 -0800 | [diff] [blame] | 119 | import android.os.ISystemUpdateManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 120 | import android.os.IUserManager; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 121 | import android.os.IncidentManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 122 | import android.os.PowerManager; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 123 | import android.os.RecoverySystem; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 124 | import android.os.ServiceManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 125 | import android.os.ServiceManager.ServiceNotFoundException; |
Tao Bao | 07342dc | 2017-01-24 15:08:21 -0800 | [diff] [blame] | 126 | import android.os.SystemUpdateManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 127 | import android.os.SystemVibrator; |
| 128 | import android.os.UserHandle; |
| 129 | import android.os.UserManager; |
| 130 | import android.os.Vibrator; |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 131 | import android.os.health.SystemHealthManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 132 | import android.os.storage.StorageManager; |
| 133 | import android.print.IPrintManager; |
| 134 | import android.print.PrintManager; |
Andrew Scull | 3b8b46f | 2017-02-13 18:12:15 +0000 | [diff] [blame] | 135 | import android.service.oemlock.IOemLockService; |
| 136 | import android.service.oemlock.OemLockManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 137 | import android.service.persistentdata.IPersistentDataBlockService; |
| 138 | import android.service.persistentdata.PersistentDataBlockManager; |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 139 | import android.service.vr.IVrManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 140 | import android.telecom.TelecomManager; |
Jonathan Basseri | 9a1c9b6 | 2015-02-25 13:01:52 -0800 | [diff] [blame] | 141 | import android.telephony.CarrierConfigManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 142 | import android.telephony.SubscriptionManager; |
| 143 | import android.telephony.TelephonyManager; |
Holly Jiuyu Sun | 5c11024 | 2017-12-21 18:44:59 -0800 | [diff] [blame] | 144 | import android.telephony.euicc.EuiccCardManager; |
Neil Fuller | 009e77a | 2017-05-04 16:53:41 +0100 | [diff] [blame] | 145 | import android.telephony.euicc.EuiccManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 146 | import android.util.Log; |
| 147 | import android.view.ContextThemeWrapper; |
| 148 | import android.view.LayoutInflater; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 149 | import android.view.WindowManager; |
| 150 | import android.view.WindowManagerImpl; |
| 151 | import android.view.accessibility.AccessibilityManager; |
| 152 | import android.view.accessibility.CaptioningManager; |
Neil Fuller | 009e77a | 2017-05-04 16:53:41 +0100 | [diff] [blame] | 153 | import android.view.autofill.AutofillManager; |
| 154 | import android.view.autofill.IAutoFillManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 155 | import android.view.inputmethod.InputMethodManager; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 156 | import android.view.textclassifier.TextClassificationManager; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 157 | import android.view.textservice.TextServicesManager; |
| 158 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 159 | import com.android.internal.app.IAppOpsService; |
| 160 | import com.android.internal.app.IBatteryStats; |
| 161 | import com.android.internal.app.ISoundTriggerService; |
| 162 | import com.android.internal.appwidget.IAppWidgetService; |
Ricky Wai | 1a6e667 | 2017-10-27 14:46:01 +0100 | [diff] [blame] | 163 | import com.android.internal.net.INetworkWatchlistManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 164 | import com.android.internal.os.IDropBoxManagerService; |
| 165 | import com.android.internal.policy.PhoneLayoutInflater; |
| 166 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 167 | import java.util.HashMap; |
| 168 | |
| 169 | /** |
| 170 | * Manages all of the system services that can be returned by {@link Context#getSystemService}. |
| 171 | * Used by {@link ContextImpl}. |
| 172 | */ |
| 173 | final class SystemServiceRegistry { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 174 | private static final String TAG = "SystemServiceRegistry"; |
| 175 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 176 | // Service registry information. |
| 177 | // This information is never changed once static initialization has completed. |
| 178 | private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES = |
| 179 | new HashMap<Class<?>, String>(); |
| 180 | private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS = |
| 181 | new HashMap<String, ServiceFetcher<?>>(); |
| 182 | private static int sServiceCacheSize; |
| 183 | |
| 184 | // Not instantiable. |
| 185 | private SystemServiceRegistry() { } |
| 186 | |
| 187 | static { |
| 188 | registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class, |
| 189 | new CachedServiceFetcher<AccessibilityManager>() { |
| 190 | @Override |
| 191 | public AccessibilityManager createService(ContextImpl ctx) { |
| 192 | return AccessibilityManager.getInstance(ctx); |
| 193 | }}); |
| 194 | |
| 195 | registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class, |
| 196 | new CachedServiceFetcher<CaptioningManager>() { |
| 197 | @Override |
| 198 | public CaptioningManager createService(ContextImpl ctx) { |
| 199 | return new CaptioningManager(ctx); |
| 200 | }}); |
| 201 | |
| 202 | registerService(Context.ACCOUNT_SERVICE, AccountManager.class, |
| 203 | new CachedServiceFetcher<AccountManager>() { |
| 204 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 205 | public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 206 | IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 207 | IAccountManager service = IAccountManager.Stub.asInterface(b); |
| 208 | return new AccountManager(ctx, service); |
| 209 | }}); |
| 210 | |
| 211 | registerService(Context.ACTIVITY_SERVICE, ActivityManager.class, |
| 212 | new CachedServiceFetcher<ActivityManager>() { |
| 213 | @Override |
| 214 | public ActivityManager createService(ContextImpl ctx) { |
| 215 | return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); |
| 216 | }}); |
| 217 | |
| 218 | registerService(Context.ALARM_SERVICE, AlarmManager.class, |
| 219 | new CachedServiceFetcher<AlarmManager>() { |
| 220 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 221 | public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 222 | IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 223 | IAlarmManager service = IAlarmManager.Stub.asInterface(b); |
| 224 | return new AlarmManager(service, ctx); |
| 225 | }}); |
| 226 | |
| 227 | registerService(Context.AUDIO_SERVICE, AudioManager.class, |
| 228 | new CachedServiceFetcher<AudioManager>() { |
| 229 | @Override |
| 230 | public AudioManager createService(ContextImpl ctx) { |
| 231 | return new AudioManager(ctx); |
| 232 | }}); |
| 233 | |
| 234 | registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class, |
| 235 | new CachedServiceFetcher<MediaRouter>() { |
| 236 | @Override |
| 237 | public MediaRouter createService(ContextImpl ctx) { |
| 238 | return new MediaRouter(ctx); |
| 239 | }}); |
| 240 | |
| 241 | registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class, |
| 242 | new CachedServiceFetcher<BluetoothManager>() { |
| 243 | @Override |
| 244 | public BluetoothManager createService(ContextImpl ctx) { |
| 245 | return new BluetoothManager(ctx); |
| 246 | }}); |
| 247 | |
| 248 | registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class, |
| 249 | new StaticServiceFetcher<HdmiControlManager>() { |
| 250 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 251 | public HdmiControlManager createService() throws ServiceNotFoundException { |
| 252 | IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 253 | return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b)); |
| 254 | }}); |
| 255 | |
Abodunrinwa Toki | 8158af5 | 2016-11-23 20:41:09 +0000 | [diff] [blame] | 256 | registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class, |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 257 | new CachedServiceFetcher<TextClassificationManager>() { |
Abodunrinwa Toki | 8158af5 | 2016-11-23 20:41:09 +0000 | [diff] [blame] | 258 | @Override |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 259 | public TextClassificationManager createService(ContextImpl ctx) { |
| 260 | return new TextClassificationManager(ctx); |
Abodunrinwa Toki | 8158af5 | 2016-11-23 20:41:09 +0000 | [diff] [blame] | 261 | }}); |
| 262 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 263 | registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class, |
| 264 | new CachedServiceFetcher<ClipboardManager>() { |
| 265 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 266 | public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 267 | return new ClipboardManager(ctx.getOuterContext(), |
| 268 | ctx.mMainThread.getHandler()); |
| 269 | }}); |
| 270 | |
| 271 | // The clipboard service moved to a new package. If someone asks for the old |
| 272 | // interface by class then we want to redirect over to the new interface instead |
| 273 | // (which extends it). |
| 274 | SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE); |
| 275 | |
| 276 | registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class, |
Lorenzo Colitti | cf95977 | 2016-02-29 16:07:37 +0900 | [diff] [blame] | 277 | new StaticApplicationContextServiceFetcher<ConnectivityManager>() { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 278 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 279 | public ConnectivityManager createService(Context context) throws ServiceNotFoundException { |
| 280 | IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 281 | IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); |
| 282 | return new ConnectivityManager(context, service); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 283 | }}); |
| 284 | |
Nathan Harold | 28084d8 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 285 | registerService(Context.IPSEC_SERVICE, IpSecManager.class, |
Nathan Harold | 592dadb | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 286 | new CachedServiceFetcher<IpSecManager>() { |
Nathan Harold | 28084d8 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 287 | @Override |
Nathan Harold | 592dadb | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 288 | public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Nathan Harold | 28084d8 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 289 | IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE); |
| 290 | IIpSecService service = IIpSecService.Stub.asInterface(b); |
Nathan Harold | 592dadb | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 291 | return new IpSecManager(ctx, service); |
Nathan Harold | 28084d8 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 292 | }}); |
| 293 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 294 | registerService(Context.COUNTRY_DETECTOR, CountryDetector.class, |
| 295 | new StaticServiceFetcher<CountryDetector>() { |
| 296 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 297 | public CountryDetector createService() throws ServiceNotFoundException { |
| 298 | IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 299 | return new CountryDetector(ICountryDetector.Stub.asInterface(b)); |
| 300 | }}); |
| 301 | |
| 302 | registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class, |
| 303 | new CachedServiceFetcher<DevicePolicyManager>() { |
| 304 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 305 | public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 306 | IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE); |
| 307 | return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b)); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 308 | }}); |
| 309 | |
| 310 | registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class, |
| 311 | new CachedServiceFetcher<DownloadManager>() { |
| 312 | @Override |
| 313 | public DownloadManager createService(ContextImpl ctx) { |
Jeff Sharkey | 60cfad8 | 2016-01-05 17:30:57 -0700 | [diff] [blame] | 314 | return new DownloadManager(ctx); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 315 | }}); |
| 316 | |
| 317 | registerService(Context.BATTERY_SERVICE, BatteryManager.class, |
Yifan Hong | b0f1352 | 2017-10-31 15:48:50 -0700 | [diff] [blame] | 318 | new CachedServiceFetcher<BatteryManager>() { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 319 | @Override |
Yifan Hong | b0f1352 | 2017-10-31 15:48:50 -0700 | [diff] [blame] | 320 | public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 321 | IBatteryStats stats = IBatteryStats.Stub.asInterface( |
| 322 | ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME)); |
| 323 | IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub |
| 324 | .asInterface(ServiceManager.getServiceOrThrow("batteryproperties")); |
Yifan Hong | b0f1352 | 2017-10-31 15:48:50 -0700 | [diff] [blame] | 325 | return new BatteryManager(ctx, stats, registrar); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 326 | }}); |
| 327 | |
| 328 | registerService(Context.NFC_SERVICE, NfcManager.class, |
| 329 | new CachedServiceFetcher<NfcManager>() { |
| 330 | @Override |
| 331 | public NfcManager createService(ContextImpl ctx) { |
| 332 | return new NfcManager(ctx); |
| 333 | }}); |
| 334 | |
| 335 | registerService(Context.DROPBOX_SERVICE, DropBoxManager.class, |
Jeff Sharkey | b8e8a91 | 2016-03-09 16:27:40 -0700 | [diff] [blame] | 336 | new CachedServiceFetcher<DropBoxManager>() { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 337 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 338 | public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 339 | IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 340 | IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b); |
Jeff Sharkey | b8e8a91 | 2016-03-09 16:27:40 -0700 | [diff] [blame] | 341 | return new DropBoxManager(ctx, service); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 342 | }}); |
| 343 | |
| 344 | registerService(Context.INPUT_SERVICE, InputManager.class, |
| 345 | new StaticServiceFetcher<InputManager>() { |
| 346 | @Override |
| 347 | public InputManager createService() { |
| 348 | return InputManager.getInstance(); |
| 349 | }}); |
| 350 | |
| 351 | registerService(Context.DISPLAY_SERVICE, DisplayManager.class, |
| 352 | new CachedServiceFetcher<DisplayManager>() { |
| 353 | @Override |
| 354 | public DisplayManager createService(ContextImpl ctx) { |
| 355 | return new DisplayManager(ctx.getOuterContext()); |
| 356 | }}); |
| 357 | |
| 358 | registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class, |
| 359 | new StaticServiceFetcher<InputMethodManager>() { |
| 360 | @Override |
| 361 | public InputMethodManager createService() { |
| 362 | return InputMethodManager.getInstance(); |
| 363 | }}); |
| 364 | |
| 365 | registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class, |
| 366 | new StaticServiceFetcher<TextServicesManager>() { |
| 367 | @Override |
| 368 | public TextServicesManager createService() { |
| 369 | return TextServicesManager.getInstance(); |
| 370 | }}); |
| 371 | |
| 372 | registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class, |
Nancy Zheng | 43f166d | 2016-11-18 18:15:09 -0800 | [diff] [blame] | 373 | new CachedServiceFetcher<KeyguardManager>() { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 374 | @Override |
Nancy Zheng | 40cd8e4 | 2016-11-21 12:58:16 -0800 | [diff] [blame] | 375 | public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Nancy Zheng | 43f166d | 2016-11-18 18:15:09 -0800 | [diff] [blame] | 376 | return new KeyguardManager(ctx); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 377 | }}); |
| 378 | |
| 379 | registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class, |
| 380 | new CachedServiceFetcher<LayoutInflater>() { |
| 381 | @Override |
| 382 | public LayoutInflater createService(ContextImpl ctx) { |
| 383 | return new PhoneLayoutInflater(ctx.getOuterContext()); |
| 384 | }}); |
| 385 | |
| 386 | registerService(Context.LOCATION_SERVICE, LocationManager.class, |
| 387 | new CachedServiceFetcher<LocationManager>() { |
| 388 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 389 | public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 390 | IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 391 | return new LocationManager(ctx, ILocationManager.Stub.asInterface(b)); |
| 392 | }}); |
| 393 | |
| 394 | registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class, |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 395 | new CachedServiceFetcher<NetworkPolicyManager>() { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 396 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 397 | public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 398 | return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface( |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 399 | ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE))); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 400 | }}); |
| 401 | |
| 402 | registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class, |
| 403 | new CachedServiceFetcher<NotificationManager>() { |
| 404 | @Override |
| 405 | public NotificationManager createService(ContextImpl ctx) { |
| 406 | final Context outerContext = ctx.getOuterContext(); |
| 407 | return new NotificationManager( |
| 408 | new ContextThemeWrapper(outerContext, |
| 409 | Resources.selectSystemTheme(0, |
| 410 | outerContext.getApplicationInfo().targetSdkVersion, |
| 411 | com.android.internal.R.style.Theme_Dialog, |
| 412 | com.android.internal.R.style.Theme_Holo_Dialog, |
| 413 | com.android.internal.R.style.Theme_DeviceDefault_Dialog, |
| 414 | com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)), |
| 415 | ctx.mMainThread.getHandler()); |
| 416 | }}); |
| 417 | |
| 418 | registerService(Context.NSD_SERVICE, NsdManager.class, |
| 419 | new CachedServiceFetcher<NsdManager>() { |
| 420 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 421 | public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 422 | IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 423 | INsdManager service = INsdManager.Stub.asInterface(b); |
| 424 | return new NsdManager(ctx.getOuterContext(), service); |
| 425 | }}); |
| 426 | |
| 427 | registerService(Context.POWER_SERVICE, PowerManager.class, |
| 428 | new CachedServiceFetcher<PowerManager>() { |
| 429 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 430 | public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 431 | IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 432 | IPowerManager service = IPowerManager.Stub.asInterface(b); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 433 | return new PowerManager(ctx.getOuterContext(), |
| 434 | service, ctx.mMainThread.getHandler()); |
| 435 | }}); |
| 436 | |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 437 | registerService(Context.RECOVERY_SERVICE, RecoverySystem.class, |
| 438 | new CachedServiceFetcher<RecoverySystem>() { |
| 439 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 440 | public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 441 | IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE); |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 442 | IRecoverySystem service = IRecoverySystem.Stub.asInterface(b); |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 443 | return new RecoverySystem(service); |
| 444 | }}); |
| 445 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 446 | registerService(Context.SEARCH_SERVICE, SearchManager.class, |
| 447 | new CachedServiceFetcher<SearchManager>() { |
| 448 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 449 | public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 450 | return new SearchManager(ctx.getOuterContext(), |
| 451 | ctx.mMainThread.getHandler()); |
| 452 | }}); |
| 453 | |
| 454 | registerService(Context.SENSOR_SERVICE, SensorManager.class, |
| 455 | new CachedServiceFetcher<SensorManager>() { |
| 456 | @Override |
| 457 | public SensorManager createService(ContextImpl ctx) { |
| 458 | return new SystemSensorManager(ctx.getOuterContext(), |
| 459 | ctx.mMainThread.getHandler().getLooper()); |
| 460 | }}); |
| 461 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 462 | registerService(Context.STATS_MANAGER, StatsManager.class, |
Jeff Sharkey | 6b64925 | 2018-04-16 09:50:22 -0600 | [diff] [blame] | 463 | new CachedServiceFetcher<StatsManager>() { |
| 464 | @Override |
| 465 | public StatsManager createService(ContextImpl ctx) { |
| 466 | return new StatsManager(ctx.getOuterContext()); |
| 467 | }}); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 468 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 469 | registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class, |
| 470 | new CachedServiceFetcher<StatusBarManager>() { |
| 471 | @Override |
| 472 | public StatusBarManager createService(ContextImpl ctx) { |
| 473 | return new StatusBarManager(ctx.getOuterContext()); |
| 474 | }}); |
| 475 | |
| 476 | registerService(Context.STORAGE_SERVICE, StorageManager.class, |
| 477 | new CachedServiceFetcher<StorageManager>() { |
| 478 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 479 | public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 480 | return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper()); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 481 | }}); |
| 482 | |
Jeff Sharkey | e8cece9 | 2017-01-04 11:33:33 -0700 | [diff] [blame] | 483 | registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class, |
| 484 | new CachedServiceFetcher<StorageStatsManager>() { |
| 485 | @Override |
| 486 | public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 487 | IStorageStatsManager service = IStorageStatsManager.Stub.asInterface( |
| 488 | ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE)); |
| 489 | return new StorageStatsManager(ctx, service); |
| 490 | }}); |
| 491 | |
Tao Bao | 07342dc | 2017-01-24 15:08:21 -0800 | [diff] [blame] | 492 | registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class, |
| 493 | new CachedServiceFetcher<SystemUpdateManager>() { |
| 494 | @Override |
| 495 | public SystemUpdateManager createService(ContextImpl ctx) |
| 496 | throws ServiceNotFoundException { |
| 497 | IBinder b = ServiceManager.getServiceOrThrow( |
| 498 | Context.SYSTEM_UPDATE_SERVICE); |
| 499 | ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b); |
| 500 | return new SystemUpdateManager(service); |
| 501 | }}); |
| 502 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 503 | registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class, |
| 504 | new CachedServiceFetcher<TelephonyManager>() { |
| 505 | @Override |
| 506 | public TelephonyManager createService(ContextImpl ctx) { |
| 507 | return new TelephonyManager(ctx.getOuterContext()); |
| 508 | }}); |
| 509 | |
| 510 | registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class, |
| 511 | new CachedServiceFetcher<SubscriptionManager>() { |
| 512 | @Override |
Jeff Sharkey | 717f52f | 2018-01-04 16:04:11 -0700 | [diff] [blame] | 513 | public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 514 | return new SubscriptionManager(ctx.getOuterContext()); |
| 515 | }}); |
| 516 | |
Jonathan Basseri | 9a1c9b6 | 2015-02-25 13:01:52 -0800 | [diff] [blame] | 517 | registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class, |
| 518 | new CachedServiceFetcher<CarrierConfigManager>() { |
| 519 | @Override |
| 520 | public CarrierConfigManager createService(ContextImpl ctx) { |
Malcolm Chen | 9afe59b | 2018-02-28 15:20:45 -0800 | [diff] [blame] | 521 | return new CarrierConfigManager(ctx.getOuterContext()); |
Jonathan Basseri | 9a1c9b6 | 2015-02-25 13:01:52 -0800 | [diff] [blame] | 522 | }}); |
| 523 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 524 | registerService(Context.TELECOM_SERVICE, TelecomManager.class, |
| 525 | new CachedServiceFetcher<TelecomManager>() { |
| 526 | @Override |
| 527 | public TelecomManager createService(ContextImpl ctx) { |
| 528 | return new TelecomManager(ctx.getOuterContext()); |
| 529 | }}); |
| 530 | |
Jeff Davidson | 35cda39 | 2017-02-27 09:46:00 -0800 | [diff] [blame] | 531 | registerService(Context.EUICC_SERVICE, EuiccManager.class, |
| 532 | new CachedServiceFetcher<EuiccManager>() { |
| 533 | @Override |
| 534 | public EuiccManager createService(ContextImpl ctx) { |
| 535 | return new EuiccManager(ctx.getOuterContext()); |
| 536 | }}); |
| 537 | |
Holly Jiuyu Sun | 5c11024 | 2017-12-21 18:44:59 -0800 | [diff] [blame] | 538 | registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class, |
| 539 | new CachedServiceFetcher<EuiccCardManager>() { |
| 540 | @Override |
| 541 | public EuiccCardManager createService(ContextImpl ctx) { |
| 542 | return new EuiccCardManager(ctx.getOuterContext()); |
| 543 | }}); |
| 544 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 545 | registerService(Context.UI_MODE_SERVICE, UiModeManager.class, |
| 546 | new CachedServiceFetcher<UiModeManager>() { |
| 547 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 548 | public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 549 | return new UiModeManager(); |
| 550 | }}); |
| 551 | |
| 552 | registerService(Context.USB_SERVICE, UsbManager.class, |
| 553 | new CachedServiceFetcher<UsbManager>() { |
| 554 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 555 | public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 556 | IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 557 | return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); |
| 558 | }}); |
| 559 | |
| 560 | registerService(Context.SERIAL_SERVICE, SerialManager.class, |
| 561 | new CachedServiceFetcher<SerialManager>() { |
| 562 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 563 | public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 564 | IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 565 | return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); |
| 566 | }}); |
| 567 | |
| 568 | registerService(Context.VIBRATOR_SERVICE, Vibrator.class, |
| 569 | new CachedServiceFetcher<Vibrator>() { |
| 570 | @Override |
| 571 | public Vibrator createService(ContextImpl ctx) { |
| 572 | return new SystemVibrator(ctx); |
| 573 | }}); |
| 574 | |
| 575 | registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class, |
| 576 | new CachedServiceFetcher<WallpaperManager>() { |
| 577 | @Override |
Svet Ganov | 8f90bcc | 2017-12-22 23:29:24 -0800 | [diff] [blame] | 578 | public WallpaperManager createService(ContextImpl ctx) |
| 579 | throws ServiceNotFoundException { |
| 580 | final IBinder b; |
| 581 | if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) { |
| 582 | b = ServiceManager.getServiceOrThrow(Context.WALLPAPER_SERVICE); |
| 583 | } else { |
| 584 | b = ServiceManager.getService(Context.WALLPAPER_SERVICE); |
| 585 | } |
| 586 | IWallpaperManager service = IWallpaperManager.Stub.asInterface(b); |
| 587 | return new WallpaperManager(service, ctx.getOuterContext(), |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 588 | ctx.mMainThread.getHandler()); |
| 589 | }}); |
| 590 | |
Robert Quattlebaum | 87a7104 | 2017-05-15 15:45:20 -0700 | [diff] [blame] | 591 | registerService(Context.LOWPAN_SERVICE, LowpanManager.class, |
| 592 | new CachedServiceFetcher<LowpanManager>() { |
| 593 | @Override |
| 594 | public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 595 | IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE); |
| 596 | ILowpanManager service = ILowpanManager.Stub.asInterface(b); |
| 597 | return new LowpanManager(ctx.getOuterContext(), service, |
| 598 | ConnectivityThread.getInstanceLooper()); |
| 599 | }}); |
| 600 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 601 | registerService(Context.WIFI_SERVICE, WifiManager.class, |
| 602 | new CachedServiceFetcher<WifiManager>() { |
| 603 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 604 | public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 605 | IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 606 | IWifiManager service = IWifiManager.Stub.asInterface(b); |
Paul Stewart | bf8cbb05 | 2016-03-17 10:11:54 -0700 | [diff] [blame] | 607 | return new WifiManager(ctx.getOuterContext(), service, |
| 608 | ConnectivityThread.getInstanceLooper()); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 609 | }}); |
| 610 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 611 | registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class, |
| 612 | new StaticServiceFetcher<WifiP2pManager>() { |
| 613 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 614 | public WifiP2pManager createService() throws ServiceNotFoundException { |
| 615 | IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 616 | IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b); |
| 617 | return new WifiP2pManager(service); |
| 618 | }}); |
| 619 | |
Etan Cohen | 0413327 | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 620 | registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class, |
| 621 | new CachedServiceFetcher<WifiAwareManager>() { |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 622 | @Override |
Etan Cohen | 0413327 | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 623 | public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Sharkey | 589f309 | 2016-11-29 12:54:39 -0700 | [diff] [blame] | 624 | IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE); |
Etan Cohen | 0413327 | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 625 | IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 626 | if (service == null) { |
| 627 | return null; |
| 628 | } |
Etan Cohen | 0413327 | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 629 | return new WifiAwareManager(ctx.getOuterContext(), service); |
Etan Cohen | 20d329b | 2015-09-29 13:49:02 -0700 | [diff] [blame] | 630 | }}); |
| 631 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 632 | registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class, |
| 633 | new CachedServiceFetcher<WifiScanner>() { |
| 634 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 635 | public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 636 | IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 637 | IWifiScanner service = IWifiScanner.Stub.asInterface(b); |
Mitchell Wills | 91312c7 | 2016-04-06 17:03:35 -0700 | [diff] [blame] | 638 | return new WifiScanner(ctx.getOuterContext(), service, |
| 639 | ConnectivityThread.getInstanceLooper()); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 640 | }}); |
| 641 | |
| 642 | registerService(Context.WIFI_RTT_SERVICE, RttManager.class, |
| 643 | new CachedServiceFetcher<RttManager>() { |
Etan Cohen | 9accbba | 2018-01-30 16:41:00 -0800 | [diff] [blame] | 644 | @Override |
| 645 | public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 646 | IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE); |
| 647 | IWifiRttManager service = IWifiRttManager.Stub.asInterface(b); |
| 648 | return new RttManager(ctx.getOuterContext(), |
| 649 | new WifiRttManager(ctx.getOuterContext(), service)); |
| 650 | }}); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 651 | |
Etan Cohen | 46efb48 | 2017-12-07 13:50:57 -0800 | [diff] [blame] | 652 | registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class, |
Etan Cohen | 17ba472 | 2017-08-21 10:52:17 -0700 | [diff] [blame] | 653 | new CachedServiceFetcher<WifiRttManager>() { |
| 654 | @Override |
| 655 | public WifiRttManager createService(ContextImpl ctx) |
| 656 | throws ServiceNotFoundException { |
Etan Cohen | 46efb48 | 2017-12-07 13:50:57 -0800 | [diff] [blame] | 657 | IBinder b = ServiceManager.getServiceOrThrow( |
| 658 | Context.WIFI_RTT_RANGING_SERVICE); |
Etan Cohen | 17ba472 | 2017-08-21 10:52:17 -0700 | [diff] [blame] | 659 | IWifiRttManager service = IWifiRttManager.Stub.asInterface(b); |
| 660 | return new WifiRttManager(ctx.getOuterContext(), service); |
| 661 | }}); |
| 662 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 663 | registerService(Context.ETHERNET_SERVICE, EthernetManager.class, |
| 664 | new CachedServiceFetcher<EthernetManager>() { |
| 665 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 666 | public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 667 | IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 668 | IEthernetManager service = IEthernetManager.Stub.asInterface(b); |
| 669 | return new EthernetManager(ctx.getOuterContext(), service); |
| 670 | }}); |
| 671 | |
| 672 | registerService(Context.WINDOW_SERVICE, WindowManager.class, |
| 673 | new CachedServiceFetcher<WindowManager>() { |
| 674 | @Override |
| 675 | public WindowManager createService(ContextImpl ctx) { |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 676 | return new WindowManagerImpl(ctx); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 677 | }}); |
| 678 | |
| 679 | registerService(Context.USER_SERVICE, UserManager.class, |
| 680 | new CachedServiceFetcher<UserManager>() { |
| 681 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 682 | public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 683 | IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 684 | IUserManager service = IUserManager.Stub.asInterface(b); |
| 685 | return new UserManager(ctx, service); |
| 686 | }}); |
| 687 | |
| 688 | registerService(Context.APP_OPS_SERVICE, AppOpsManager.class, |
| 689 | new CachedServiceFetcher<AppOpsManager>() { |
| 690 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 691 | public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 692 | IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 693 | IAppOpsService service = IAppOpsService.Stub.asInterface(b); |
| 694 | return new AppOpsManager(ctx, service); |
| 695 | }}); |
| 696 | |
| 697 | registerService(Context.CAMERA_SERVICE, CameraManager.class, |
| 698 | new CachedServiceFetcher<CameraManager>() { |
| 699 | @Override |
| 700 | public CameraManager createService(ContextImpl ctx) { |
| 701 | return new CameraManager(ctx); |
| 702 | }}); |
| 703 | |
| 704 | registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class, |
| 705 | new CachedServiceFetcher<LauncherApps>() { |
| 706 | @Override |
| 707 | public LauncherApps createService(ContextImpl ctx) { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 708 | return new LauncherApps(ctx); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 709 | }}); |
| 710 | |
| 711 | registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class, |
| 712 | new CachedServiceFetcher<RestrictionsManager>() { |
| 713 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 714 | public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 715 | IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 716 | IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b); |
| 717 | return new RestrictionsManager(ctx, service); |
| 718 | }}); |
| 719 | |
| 720 | registerService(Context.PRINT_SERVICE, PrintManager.class, |
| 721 | new CachedServiceFetcher<PrintManager>() { |
| 722 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 723 | public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Svet Ganov | ec7daa5 | 2017-06-30 12:02:59 -0700 | [diff] [blame] | 724 | IPrintManager service = null; |
| 725 | // If the feature not present, don't try to look up every time |
| 726 | if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) { |
| 727 | service = IPrintManager.Stub.asInterface(ServiceManager |
| 728 | .getServiceOrThrow(Context.PRINT_SERVICE)); |
| 729 | } |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 730 | final int userId = ctx.getUserId(); |
| 731 | final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid); |
| 732 | return new PrintManager(ctx.getOuterContext(), service, userId, appId); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 733 | }}); |
| 734 | |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 735 | registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class, |
| 736 | new CachedServiceFetcher<CompanionDeviceManager>() { |
Svet Ganov | ec7daa5 | 2017-06-30 12:02:59 -0700 | [diff] [blame] | 737 | @Override |
| 738 | public CompanionDeviceManager createService(ContextImpl ctx) |
| 739 | throws ServiceNotFoundException { |
| 740 | ICompanionDeviceManager service = null; |
| 741 | // If the feature not present, don't try to look up every time |
| 742 | if (ctx.getPackageManager().hasSystemFeature( |
| 743 | PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) { |
| 744 | service = ICompanionDeviceManager.Stub.asInterface( |
| 745 | ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE)); |
| 746 | } |
| 747 | return new CompanionDeviceManager(service, ctx.getOuterContext()); |
| 748 | }}); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 749 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 750 | registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class, |
| 751 | new CachedServiceFetcher<ConsumerIrManager>() { |
| 752 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 753 | public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 754 | return new ConsumerIrManager(ctx); |
| 755 | }}); |
| 756 | |
| 757 | registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class, |
| 758 | new CachedServiceFetcher<MediaSessionManager>() { |
| 759 | @Override |
| 760 | public MediaSessionManager createService(ContextImpl ctx) { |
| 761 | return new MediaSessionManager(ctx); |
| 762 | }}); |
| 763 | |
| 764 | registerService(Context.TRUST_SERVICE, TrustManager.class, |
| 765 | new StaticServiceFetcher<TrustManager>() { |
| 766 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 767 | public TrustManager createService() throws ServiceNotFoundException { |
| 768 | IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 769 | return new TrustManager(b); |
| 770 | }}); |
| 771 | |
| 772 | registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class, |
| 773 | new CachedServiceFetcher<FingerprintManager>() { |
| 774 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 775 | public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Sharkey | 351c2c2 | 2017-03-18 16:48:02 -0600 | [diff] [blame] | 776 | final IBinder binder; |
| 777 | if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) { |
| 778 | binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE); |
| 779 | } else { |
| 780 | binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE); |
| 781 | } |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 782 | IFingerprintService service = IFingerprintService.Stub.asInterface(binder); |
| 783 | return new FingerprintManager(ctx.getOuterContext(), service); |
| 784 | }}); |
| 785 | |
| 786 | registerService(Context.TV_INPUT_SERVICE, TvInputManager.class, |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 787 | new CachedServiceFetcher<TvInputManager>() { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 788 | @Override |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 789 | public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 790 | IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 791 | ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder); |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 792 | return new TvInputManager(service, ctx.getUserId()); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 793 | }}); |
| 794 | |
| 795 | registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class, |
| 796 | new CachedServiceFetcher<NetworkScoreManager>() { |
| 797 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 798 | public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 799 | return new NetworkScoreManager(ctx); |
| 800 | }}); |
| 801 | |
| 802 | registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class, |
| 803 | new CachedServiceFetcher<UsageStatsManager>() { |
| 804 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 805 | public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 806 | IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 807 | IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder); |
| 808 | return new UsageStatsManager(ctx.getOuterContext(), service); |
| 809 | }}); |
| 810 | |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 811 | registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class, |
| 812 | new CachedServiceFetcher<NetworkStatsManager>() { |
| 813 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 814 | public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 815 | return new NetworkStatsManager(ctx.getOuterContext()); |
| 816 | }}); |
| 817 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 818 | registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class, |
| 819 | new StaticServiceFetcher<JobScheduler>() { |
| 820 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 821 | public JobScheduler createService() throws ServiceNotFoundException { |
| 822 | IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 823 | return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b)); |
| 824 | }}); |
| 825 | |
| 826 | registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class, |
| 827 | new StaticServiceFetcher<PersistentDataBlockManager>() { |
| 828 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 829 | public PersistentDataBlockManager createService() throws ServiceNotFoundException { |
| 830 | IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 831 | IPersistentDataBlockService persistentDataBlockService = |
| 832 | IPersistentDataBlockService.Stub.asInterface(b); |
| 833 | if (persistentDataBlockService != null) { |
| 834 | return new PersistentDataBlockManager(persistentDataBlockService); |
| 835 | } else { |
| 836 | // not supported |
| 837 | return null; |
| 838 | } |
| 839 | }}); |
| 840 | |
Andrew Scull | 3b8b46f | 2017-02-13 18:12:15 +0000 | [diff] [blame] | 841 | registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class, |
| 842 | new StaticServiceFetcher<OemLockManager>() { |
| 843 | @Override |
| 844 | public OemLockManager createService() throws ServiceNotFoundException { |
| 845 | IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE); |
| 846 | IOemLockService oemLockService = IOemLockService.Stub.asInterface(b); |
| 847 | if (oemLockService != null) { |
| 848 | return new OemLockManager(oemLockService); |
| 849 | } else { |
| 850 | // not supported |
| 851 | return null; |
| 852 | } |
| 853 | }}); |
| 854 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 855 | registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class, |
| 856 | new CachedServiceFetcher<MediaProjectionManager>() { |
| 857 | @Override |
| 858 | public MediaProjectionManager createService(ContextImpl ctx) { |
| 859 | return new MediaProjectionManager(ctx); |
| 860 | }}); |
| 861 | |
| 862 | registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class, |
| 863 | new CachedServiceFetcher<AppWidgetManager>() { |
| 864 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 865 | public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 866 | IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 867 | return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b)); |
| 868 | }}); |
| 869 | |
| 870 | registerService(Context.MIDI_SERVICE, MidiManager.class, |
| 871 | new CachedServiceFetcher<MidiManager>() { |
| 872 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 873 | public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 874 | IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE); |
Mike Lockwood | e0a6ca6 | 2015-06-04 13:43:56 -0700 | [diff] [blame] | 875 | return new MidiManager(IMidiManager.Stub.asInterface(b)); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 876 | }}); |
Eric Laurent | 2035ac8 | 2015-03-05 15:18:44 -0800 | [diff] [blame] | 877 | |
| 878 | registerService(Context.RADIO_SERVICE, RadioManager.class, |
| 879 | new CachedServiceFetcher<RadioManager>() { |
| 880 | @Override |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 881 | public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Eric Laurent | 2035ac8 | 2015-03-05 15:18:44 -0800 | [diff] [blame] | 882 | return new RadioManager(ctx); |
| 883 | }}); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 884 | |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 885 | registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class, |
| 886 | new CachedServiceFetcher<HardwarePropertiesManager>() { |
| 887 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 888 | public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 889 | IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE); |
| 890 | IHardwarePropertiesManager service = |
| 891 | IHardwarePropertiesManager.Stub.asInterface(b); |
| 892 | return new HardwarePropertiesManager(ctx, service); |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 893 | }}); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 894 | |
| 895 | registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class, |
| 896 | new CachedServiceFetcher<SoundTriggerManager>() { |
| 897 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 898 | public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 899 | IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 900 | return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b)); |
| 901 | }}); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 902 | |
| 903 | registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class, |
| 904 | new CachedServiceFetcher<ShortcutManager>() { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 905 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 906 | public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 907 | IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE); |
| 908 | return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b)); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 909 | }}); |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 910 | |
Ricky Wai | 1a6e667 | 2017-10-27 14:46:01 +0100 | [diff] [blame] | 911 | registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class, |
| 912 | new CachedServiceFetcher<NetworkWatchlistManager>() { |
| 913 | @Override |
| 914 | public NetworkWatchlistManager createService(ContextImpl ctx) |
| 915 | throws ServiceNotFoundException { |
| 916 | IBinder b = |
| 917 | ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE); |
| 918 | return new NetworkWatchlistManager(ctx, |
| 919 | INetworkWatchlistManager.Stub.asInterface(b)); |
| 920 | }}); |
| 921 | |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 922 | registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class, |
| 923 | new CachedServiceFetcher<SystemHealthManager>() { |
| 924 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 925 | public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 926 | IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME); |
| 927 | return new SystemHealthManager(IBatteryStats.Stub.asInterface(b)); |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 928 | }}); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 929 | |
| 930 | registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class, |
| 931 | new CachedServiceFetcher<ContextHubManager>() { |
| 932 | @Override |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 933 | public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 934 | return new ContextHubManager(ctx.getOuterContext(), |
| 935 | ctx.mMainThread.getHandler().getLooper()); |
| 936 | }}); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 937 | |
| 938 | registerService(Context.INCIDENT_SERVICE, IncidentManager.class, |
| 939 | new CachedServiceFetcher<IncidentManager>() { |
| 940 | @Override |
| 941 | public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 942 | return new IncidentManager(ctx); |
| 943 | }}); |
Clara Bayarri | 04d72ab | 2017-01-10 09:31:51 -0800 | [diff] [blame] | 944 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 945 | registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class, |
| 946 | new CachedServiceFetcher<AutofillManager>() { |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 947 | @Override |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 948 | public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 949 | // Get the services without throwing as this is an optional feature |
| 950 | IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 951 | IAutoFillManager service = IAutoFillManager.Stub.asInterface(b); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 952 | return new AutofillManager(ctx.getOuterContext(), service); |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 953 | }}); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 954 | |
| 955 | registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() { |
| 956 | @Override |
| 957 | public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException { |
| 958 | IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE); |
| 959 | return new VrManager(IVrManager.Stub.asInterface(b)); |
| 960 | } |
| 961 | }); |
Neil Fuller | 328532a | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 962 | |
| 963 | registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class, |
| 964 | new CachedServiceFetcher<RulesManager>() { |
| 965 | @Override |
| 966 | public RulesManager createService(ContextImpl ctx) { |
| 967 | return new RulesManager(ctx.getOuterContext()); |
| 968 | }}); |
Tony Mak | 1b708e6 | 2017-10-12 10:59:11 +0100 | [diff] [blame] | 969 | |
| 970 | registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class, |
| 971 | new CachedServiceFetcher<CrossProfileApps>() { |
| 972 | @Override |
| 973 | public CrossProfileApps createService(ContextImpl ctx) |
| 974 | throws ServiceNotFoundException { |
| 975 | IBinder b = ServiceManager.getServiceOrThrow( |
| 976 | Context.CROSS_PROFILE_APPS_SERVICE); |
| 977 | return new CrossProfileApps(ctx.getOuterContext(), |
| 978 | ICrossProfileApps.Stub.asInterface(b)); |
| 979 | } |
| 980 | }); |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 981 | |
| 982 | registerService(Context.SLICE_SERVICE, SliceManager.class, |
| 983 | new CachedServiceFetcher<SliceManager>() { |
| 984 | @Override |
| 985 | public SliceManager createService(ContextImpl ctx) |
| 986 | throws ServiceNotFoundException { |
| 987 | return new SliceManager(ctx.getOuterContext(), |
| 988 | ctx.mMainThread.getHandler()); |
| 989 | } |
| 990 | }); |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 991 | |
| 992 | registerService(Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class, |
| 993 | new CachedServiceFetcher<DeviceIdleManager>() { |
| 994 | @Override |
| 995 | public DeviceIdleManager createService(ContextImpl ctx) |
| 996 | throws ServiceNotFoundException { |
| 997 | IDeviceIdleController service = IDeviceIdleController.Stub.asInterface( |
| 998 | ServiceManager.getServiceOrThrow( |
| 999 | Context.DEVICE_IDLE_CONTROLLER)); |
| 1000 | return new DeviceIdleManager(ctx.getOuterContext(), service); |
Neil Fuller | 10e276a | 2018-06-12 20:10:22 +0100 | [diff] [blame] | 1001 | }}); |
Neil Fuller | 79d42a6 | 2018-06-12 14:14:13 +0100 | [diff] [blame] | 1002 | |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 1003 | registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class, |
| 1004 | new CachedServiceFetcher<TimeDetector>() { |
| 1005 | @Override |
| 1006 | public TimeDetector createService(ContextImpl ctx) |
| 1007 | throws ServiceNotFoundException { |
| 1008 | return new TimeDetector(); |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 1009 | }}); |
Neil Fuller | cccc48d | 2018-06-19 12:53:47 +0100 | [diff] [blame] | 1010 | registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class, |
| 1011 | new CachedServiceFetcher<TimeZoneDetector>() { |
| 1012 | @Override |
| 1013 | public TimeZoneDetector createService(ContextImpl ctx) |
| 1014 | throws ServiceNotFoundException { |
| 1015 | return new TimeZoneDetector(); |
| 1016 | }}); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * Creates an array which is used to cache per-Context service instances. |
| 1021 | */ |
| 1022 | public static Object[] createServiceCache() { |
| 1023 | return new Object[sServiceCacheSize]; |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Gets a system service from a given context. |
| 1028 | */ |
| 1029 | public static Object getSystemService(ContextImpl ctx, String name) { |
| 1030 | ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name); |
| 1031 | return fetcher != null ? fetcher.getService(ctx) : null; |
| 1032 | } |
| 1033 | |
| 1034 | /** |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 1035 | * Gets the name of the system-level service that is represented by the specified class. |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1036 | */ |
| 1037 | public static String getSystemServiceName(Class<?> serviceClass) { |
| 1038 | return SYSTEM_SERVICE_NAMES.get(serviceClass); |
| 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * Statically registers a system service with the context. |
| 1043 | * This method must be called during static initialization only. |
| 1044 | */ |
| 1045 | private static <T> void registerService(String serviceName, Class<T> serviceClass, |
| 1046 | ServiceFetcher<T> serviceFetcher) { |
| 1047 | SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName); |
| 1048 | SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher); |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Base interface for classes that fetch services. |
| 1053 | * These objects must only be created during static initialization. |
| 1054 | */ |
| 1055 | static abstract interface ServiceFetcher<T> { |
| 1056 | T getService(ContextImpl ctx); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * Override this class when the system service constructor needs a |
| 1061 | * ContextImpl and should be cached and retained by that context. |
| 1062 | */ |
| 1063 | static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> { |
| 1064 | private final int mCacheIndex; |
| 1065 | |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1066 | CachedServiceFetcher() { |
| 1067 | // Note this class must be instantiated only by the static initializer of the |
| 1068 | // outer class (SystemServiceRegistry), which already does the synchronization, |
| 1069 | // so bare access to sServiceCacheSize is okay here. |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1070 | mCacheIndex = sServiceCacheSize++; |
| 1071 | } |
| 1072 | |
| 1073 | @Override |
| 1074 | @SuppressWarnings("unchecked") |
| 1075 | public final T getService(ContextImpl ctx) { |
| 1076 | final Object[] cache = ctx.mServiceCache; |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1077 | final int[] gates = ctx.mServiceInitializationStateArray; |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1078 | |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1079 | for (;;) { |
| 1080 | boolean doInitialize = false; |
| 1081 | synchronized (cache) { |
| 1082 | // Return it if we already have a cached instance. |
| 1083 | T service = (T) cache[mCacheIndex]; |
| 1084 | if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) { |
| 1085 | return service; |
| 1086 | } |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1087 | |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1088 | // If we get here, there's no cached instance. |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1089 | |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1090 | // Grr... if gate is STATE_READY, then this means we initialized the service |
| 1091 | // once but someone cleared it. |
| 1092 | // We start over from STATE_UNINITIALIZED. |
| 1093 | if (gates[mCacheIndex] == ContextImpl.STATE_READY) { |
| 1094 | gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED; |
| 1095 | } |
| 1096 | |
| 1097 | // It's possible for multiple threads to get here at the same time, so |
| 1098 | // use the "gate" to make sure only the first thread will call createService(). |
| 1099 | |
| 1100 | // At this point, the gate must be either UNINITIALIZED or INITIALIZING. |
| 1101 | if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) { |
| 1102 | doInitialize = true; |
| 1103 | gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING; |
| 1104 | } |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1107 | if (doInitialize) { |
| 1108 | // Only the first thread gets here. |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1109 | |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1110 | T service = null; |
| 1111 | @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1112 | try { |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1113 | // This thread is the first one to get here. Instantiate the service |
| 1114 | // *without* the cache lock held. |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1115 | service = createService(ctx); |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1116 | newState = ContextImpl.STATE_READY; |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1117 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1118 | } catch (ServiceNotFoundException e) { |
Jeff Sharkey | 589f309 | 2016-11-29 12:54:39 -0700 | [diff] [blame] | 1119 | onServiceNotFound(e); |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1120 | |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 1121 | } finally { |
| 1122 | synchronized (cache) { |
| 1123 | cache[mCacheIndex] = service; |
| 1124 | gates[mCacheIndex] = newState; |
| 1125 | cache.notifyAll(); |
| 1126 | } |
| 1127 | } |
| 1128 | return service; |
| 1129 | } |
| 1130 | // The other threads will wait for the first thread to call notifyAll(), |
| 1131 | // and go back to the top and retry. |
| 1132 | synchronized (cache) { |
| 1133 | while (gates[mCacheIndex] < ContextImpl.STATE_READY) { |
| 1134 | try { |
| 1135 | cache.wait(); |
| 1136 | } catch (InterruptedException e) { |
| 1137 | Log.w(TAG, "getService() interrupted"); |
| 1138 | Thread.currentThread().interrupt(); |
| 1139 | return null; |
| 1140 | } |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1141 | } |
| 1142 | } |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 1143 | } |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1146 | public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * Override this class when the system service does not need a ContextImpl |
| 1151 | * and should be cached and retained process-wide. |
| 1152 | */ |
| 1153 | static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> { |
| 1154 | private T mCachedInstance; |
| 1155 | |
| 1156 | @Override |
Jeff Sharkey | 589f309 | 2016-11-29 12:54:39 -0700 | [diff] [blame] | 1157 | public final T getService(ContextImpl ctx) { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1158 | synchronized (StaticServiceFetcher.this) { |
| 1159 | if (mCachedInstance == null) { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1160 | try { |
| 1161 | mCachedInstance = createService(); |
| 1162 | } catch (ServiceNotFoundException e) { |
Jeff Sharkey | 589f309 | 2016-11-29 12:54:39 -0700 | [diff] [blame] | 1163 | onServiceNotFound(e); |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1164 | } |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1165 | } |
| 1166 | return mCachedInstance; |
| 1167 | } |
| 1168 | } |
| 1169 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1170 | public abstract T createService() throws ServiceNotFoundException; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1171 | } |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1172 | |
| 1173 | /** |
Lorenzo Colitti | cf95977 | 2016-02-29 16:07:37 +0900 | [diff] [blame] | 1174 | * Like StaticServiceFetcher, creates only one instance of the service per application, but when |
| 1175 | * creating the service for the first time, passes it the application context of the creating |
| 1176 | * application. |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1177 | * |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1178 | * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the |
| 1179 | * case where multiple application components each have their own ConnectivityManager object. |
| 1180 | */ |
Lorenzo Colitti | cf95977 | 2016-02-29 16:07:37 +0900 | [diff] [blame] | 1181 | static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1182 | private T mCachedInstance; |
| 1183 | |
| 1184 | @Override |
| 1185 | public final T getService(ContextImpl ctx) { |
Lorenzo Colitti | cf95977 | 2016-02-29 16:07:37 +0900 | [diff] [blame] | 1186 | synchronized (StaticApplicationContextServiceFetcher.this) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1187 | if (mCachedInstance == null) { |
Lorenzo Colitti | 0ad9ab0 | 2016-03-25 17:40:11 +0900 | [diff] [blame] | 1188 | Context appContext = ctx.getApplicationContext(); |
| 1189 | // If the application context is null, we're either in the system process or |
| 1190 | // it's the application context very early in app initialization. In both these |
| 1191 | // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it |
| 1192 | // to the service. http://b/27532714 . |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1193 | try { |
| 1194 | mCachedInstance = createService(appContext != null ? appContext : ctx); |
| 1195 | } catch (ServiceNotFoundException e) { |
Jeff Sharkey | 589f309 | 2016-11-29 12:54:39 -0700 | [diff] [blame] | 1196 | onServiceNotFound(e); |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1197 | } |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1198 | } |
| 1199 | return mCachedInstance; |
| 1200 | } |
| 1201 | } |
| 1202 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 1203 | public abstract T createService(Context applicationContext) throws ServiceNotFoundException; |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1204 | } |
Jeff Sharkey | 589f309 | 2016-11-29 12:54:39 -0700 | [diff] [blame] | 1205 | |
| 1206 | public static void onServiceNotFound(ServiceNotFoundException e) { |
| 1207 | // We're mostly interested in tracking down long-lived core system |
| 1208 | // components that might stumble if they obtain bad references; just |
| 1209 | // emit a tidy log message for normal apps |
| 1210 | if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) { |
| 1211 | Log.wtf(TAG, e.getMessage(), e); |
| 1212 | } else { |
| 1213 | Log.w(TAG, e.getMessage()); |
| 1214 | } |
| 1215 | } |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1216 | } |