From 49ca529a850e60482ddcc8c0762105b4aa10f35f Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Tue, 10 May 2016 12:54:45 -0600 Subject: [PATCH] Avoid caching services with missing binders. When fetching system services early during boot, if the underlying binder interface hasn't been published yet, we end up caching a manager class that is broken for the remainder of the process lifetime, and innocent downstream callers end up using the broken cached manager. Fix this by using an explicit exception to quickly abort manager creation when the underlying binder is missing. The exception is only used to skip the remainder of the manager creation, and it doesn't actually crash the process. Bug: 28634953 Change-Id: I0cb62261e6d6833660704b93a11185aa11a2ac97 --- core/java/android/app/Activity.java | 7 +- core/java/android/app/KeyguardManager.java | 7 +- core/java/android/app/SearchManager.java | 9 +- .../android/app/SystemServiceRegistry.java | 249 +++++++++--------- core/java/android/app/UiModeManager.java | 5 +- .../app/admin/DevicePolicyManager.java | 23 +- .../app/usage/NetworkStatsManager.java | 5 +- .../android/content/ClipboardManager.java | 38 +-- .../android/hardware/ConsumerIrManager.java | 5 +- .../android/hardware/input/InputManager.java | 9 +- .../hardware/location/ContextHubManager.java | 95 ++----- .../hardware/location/ContextHubService.java | 8 + .../java/android/net/NetworkScoreManager.java | 8 +- core/java/android/os/BatteryManager.java | 12 +- core/java/android/os/ServiceManager.java | 32 ++- .../os/health/SystemHealthManager.java | 9 +- .../android/os/storage/StorageManager.java | 10 +- .../view/inputmethod/InputMethodManager.java | 15 +- .../view/textservice/TextServicesManager.java | 73 +++-- 19 files changed, 311 insertions(+), 308 deletions(-) diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index a4c667f908523..5f8350f72a54d 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -66,6 +66,7 @@ import android.os.Looper; import android.os.Parcelable; import android.os.PersistableBundle; import android.os.RemoteException; +import android.os.ServiceManager.ServiceNotFoundException; import android.os.StrictMode; import android.os.SystemProperties; import android.os.UserHandle; @@ -5591,7 +5592,11 @@ public class Activity extends ContextThemeWrapper return; } - mSearchManager = new SearchManager(this, null); + try { + mSearchManager = new SearchManager(this, null); + } catch (ServiceNotFoundException e) { + throw new IllegalStateException(e); + } } @Override diff --git a/core/java/android/app/KeyguardManager.java b/core/java/android/app/KeyguardManager.java index 505e081bddb40..da99e8005f38e 100644 --- a/core/java/android/app/KeyguardManager.java +++ b/core/java/android/app/KeyguardManager.java @@ -27,6 +27,7 @@ import android.os.RemoteException; import android.os.IBinder; import android.os.IUserManager; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.os.UserHandle; import android.os.UserManager; import android.view.IWindowManager; @@ -192,12 +193,12 @@ public class KeyguardManager { } - KeyguardManager() { + KeyguardManager() throws ServiceNotFoundException { mWM = WindowManagerGlobal.getWindowManagerService(); mTrustManager = ITrustManager.Stub.asInterface( - ServiceManager.getService(Context.TRUST_SERVICE)); + ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE)); mUserManager = IUserManager.Stub.asInterface( - ServiceManager.getService(Context.USER_SERVICE)); + ServiceManager.getServiceOrThrow(Context.USER_SERVICE)); } /** diff --git a/core/java/android/app/SearchManager.java b/core/java/android/app/SearchManager.java index ac4abf5c36a45..ee6a3acb6eee2 100644 --- a/core/java/android/app/SearchManager.java +++ b/core/java/android/app/SearchManager.java @@ -31,6 +31,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.os.UserHandle; import android.text.TextUtils; import android.util.Log; @@ -536,7 +537,7 @@ public class SearchManager /** * Reference to the shared system search service. */ - private static ISearchManager mService; + private final ISearchManager mService; private final Context mContext; @@ -547,11 +548,11 @@ public class SearchManager private SearchDialog mSearchDialog; - /*package*/ SearchManager(Context context, Handler handler) { + /*package*/ SearchManager(Context context, Handler handler) throws ServiceNotFoundException { mContext = context; mHandler = handler; mService = ISearchManager.Stub.asInterface( - ServiceManager.getService(Context.SEARCH_SERVICE)); + ServiceManager.getServiceOrThrow(Context.SEARCH_SERVICE)); } /** @@ -620,7 +621,7 @@ public class SearchManager return; } - UiModeManager uiModeManager = new UiModeManager(); + final UiModeManager uiModeManager = mContext.getSystemService(UiModeManager.class); // Don't show search dialog on televisions. if (uiModeManager.getCurrentModeType() != Configuration.UI_MODE_TYPE_TELEVISION) { ensureSearchDialog(); diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java index 55744b935f517..e07198efec27d 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -16,14 +16,10 @@ package android.app; -import com.android.internal.app.IAppOpsService; -import com.android.internal.app.ISoundTriggerService; -import com.android.internal.appwidget.IAppWidgetService; -import com.android.internal.os.IDropBoxManagerService; - import android.accounts.AccountManager; import android.accounts.IAccountManager; import android.app.admin.DevicePolicyManager; +import android.app.admin.IDevicePolicyManager; import android.app.job.IJobScheduler; import android.app.job.JobScheduler; import android.app.trust.TrustManager; @@ -36,7 +32,6 @@ import android.content.ClipboardManager; import android.content.Context; import android.content.IRestrictionsManager; import android.content.RestrictionsManager; -import android.content.pm.ILauncherApps; import android.content.pm.IShortcutService; import android.content.pm.LauncherApps; import android.content.pm.ShortcutManager; @@ -48,13 +43,15 @@ import android.hardware.SerialManager; import android.hardware.SystemSensorManager; import android.hardware.camera2.CameraManager; import android.hardware.display.DisplayManager; +import android.hardware.fingerprint.FingerprintManager; +import android.hardware.fingerprint.IFingerprintService; import android.hardware.hdmi.HdmiControlManager; import android.hardware.hdmi.IHdmiControlService; import android.hardware.input.InputManager; import android.hardware.location.ContextHubManager; +import android.hardware.radio.RadioManager; import android.hardware.usb.IUsbManager; import android.hardware.usb.UsbManager; -import android.hardware.radio.RadioManager; import android.location.CountryDetector; import android.location.ICountryDetector; import android.location.ILocationManager; @@ -90,8 +87,10 @@ import android.net.wifi.p2p.IWifiP2pManager; import android.net.wifi.p2p.WifiP2pManager; import android.nfc.NfcManager; import android.os.BatteryManager; +import android.os.BatteryStats; import android.os.DropBoxManager; import android.os.HardwarePropertiesManager; +import android.os.IBatteryPropertiesRegistrar; import android.os.IBinder; import android.os.IHardwarePropertiesManager; import android.os.IPowerManager; @@ -101,6 +100,7 @@ import android.os.PowerManager; import android.os.Process; import android.os.RecoverySystem; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.os.SystemVibrator; import android.os.UserHandle; import android.os.UserManager; @@ -109,8 +109,6 @@ import android.os.health.SystemHealthManager; import android.os.storage.StorageManager; import android.print.IPrintManager; import android.print.PrintManager; -import android.hardware.fingerprint.FingerprintManager; -import android.hardware.fingerprint.IFingerprintService; import android.service.persistentdata.IPersistentDataBlockService; import android.service.persistentdata.PersistentDataBlockManager; import android.telecom.TelecomManager; @@ -120,7 +118,6 @@ import android.telephony.TelephonyManager; import android.util.Log; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; -import com.android.internal.policy.PhoneLayoutInflater; import android.view.WindowManager; import android.view.WindowManagerImpl; import android.view.accessibility.AccessibilityManager; @@ -128,6 +125,13 @@ import android.view.accessibility.CaptioningManager; import android.view.inputmethod.InputMethodManager; import android.view.textservice.TextServicesManager; +import com.android.internal.app.IAppOpsService; +import com.android.internal.app.IBatteryStats; +import com.android.internal.app.ISoundTriggerService; +import com.android.internal.appwidget.IAppWidgetService; +import com.android.internal.os.IDropBoxManagerService; +import com.android.internal.policy.PhoneLayoutInflater; + import java.util.HashMap; /** @@ -135,7 +139,10 @@ import java.util.HashMap; * Used by {@link ContextImpl}. */ final class SystemServiceRegistry { - private final static String TAG = "SystemServiceRegistry"; + private static final String TAG = "SystemServiceRegistry"; + + // When set, include stack traces when services aren't published + private static final boolean DEBUG_EARLY_CALLERS = true; // Service registry information. // This information is never changed once static initialization has completed. @@ -166,8 +173,8 @@ final class SystemServiceRegistry { registerService(Context.ACCOUNT_SERVICE, AccountManager.class, new CachedServiceFetcher() { @Override - public AccountManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE); + public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE); IAccountManager service = IAccountManager.Stub.asInterface(b); return new AccountManager(ctx, service); }}); @@ -182,8 +189,8 @@ final class SystemServiceRegistry { registerService(Context.ALARM_SERVICE, AlarmManager.class, new CachedServiceFetcher() { @Override - public AlarmManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.ALARM_SERVICE); + public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE); IAlarmManager service = IAlarmManager.Stub.asInterface(b); return new AlarmManager(service, ctx); }}); @@ -212,15 +219,15 @@ final class SystemServiceRegistry { registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class, new StaticServiceFetcher() { @Override - public HdmiControlManager createService() { - IBinder b = ServiceManager.getService(Context.HDMI_CONTROL_SERVICE); + public HdmiControlManager createService() throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE); return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b)); }}); registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class, new CachedServiceFetcher() { @Override - public ClipboardManager createService(ContextImpl ctx) { + public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new ClipboardManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }}); @@ -233,8 +240,8 @@ final class SystemServiceRegistry { registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class, new StaticApplicationContextServiceFetcher() { @Override - public ConnectivityManager createService(Context context) { - IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); + public ConnectivityManager createService(Context context) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); return new ConnectivityManager(context, service); }}); @@ -242,16 +249,17 @@ final class SystemServiceRegistry { registerService(Context.COUNTRY_DETECTOR, CountryDetector.class, new StaticServiceFetcher() { @Override - public CountryDetector createService() { - IBinder b = ServiceManager.getService(Context.COUNTRY_DETECTOR); + public CountryDetector createService() throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR); return new CountryDetector(ICountryDetector.Stub.asInterface(b)); }}); registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class, new CachedServiceFetcher() { @Override - public DevicePolicyManager createService(ContextImpl ctx) { - return DevicePolicyManager.create(ctx); + public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE); + return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b)); }}); registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class, @@ -264,8 +272,12 @@ final class SystemServiceRegistry { registerService(Context.BATTERY_SERVICE, BatteryManager.class, new StaticServiceFetcher() { @Override - public BatteryManager createService() { - return new BatteryManager(); + public BatteryManager createService() throws ServiceNotFoundException { + IBatteryStats stats = IBatteryStats.Stub.asInterface( + ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME)); + IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub + .asInterface(ServiceManager.getServiceOrThrow("batteryproperties")); + return new BatteryManager(stats, registrar); }}); registerService(Context.NFC_SERVICE, NfcManager.class, @@ -278,16 +290,9 @@ final class SystemServiceRegistry { registerService(Context.DROPBOX_SERVICE, DropBoxManager.class, new CachedServiceFetcher() { @Override - public DropBoxManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.DROPBOX_SERVICE); + public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE); IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b); - if (service == null) { - // Don't return a DropBoxManager that will NPE upon use. - // This also avoids caching a broken DropBoxManager in - // getDropBoxManager during early boot, before the - // DROPBOX_SERVICE is registered. - return null; - } return new DropBoxManager(ctx, service); }}); @@ -322,7 +327,7 @@ final class SystemServiceRegistry { registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class, new StaticServiceFetcher() { @Override - public KeyguardManager createService() { + public KeyguardManager createService() throws ServiceNotFoundException { return new KeyguardManager(); }}); @@ -336,17 +341,17 @@ final class SystemServiceRegistry { registerService(Context.LOCATION_SERVICE, LocationManager.class, new CachedServiceFetcher() { @Override - public LocationManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE); + public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE); return new LocationManager(ctx, ILocationManager.Stub.asInterface(b)); }}); registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class, new CachedServiceFetcher() { @Override - public NetworkPolicyManager createService(ContextImpl ctx) { + public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface( - ServiceManager.getService(Context.NETWORK_POLICY_SERVICE))); + ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE))); }}); registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class, @@ -368,8 +373,8 @@ final class SystemServiceRegistry { registerService(Context.NSD_SERVICE, NsdManager.class, new CachedServiceFetcher() { @Override - public NsdManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.NSD_SERVICE); + public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE); INsdManager service = INsdManager.Stub.asInterface(b); return new NsdManager(ctx.getOuterContext(), service); }}); @@ -377,8 +382,8 @@ final class SystemServiceRegistry { registerService(Context.POWER_SERVICE, PowerManager.class, new CachedServiceFetcher() { @Override - public PowerManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.POWER_SERVICE); + public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE); IPowerManager service = IPowerManager.Stub.asInterface(b); if (service == null) { Log.wtf(TAG, "Failed to get power manager service."); @@ -390,8 +395,8 @@ final class SystemServiceRegistry { registerService(Context.RECOVERY_SERVICE, RecoverySystem.class, new CachedServiceFetcher() { @Override - public RecoverySystem createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.RECOVERY_SERVICE); + public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE); IRecoverySystem service = IRecoverySystem.Stub.asInterface(b); if (service == null) { Log.wtf(TAG, "Failed to get recovery service."); @@ -402,7 +407,7 @@ final class SystemServiceRegistry { registerService(Context.SEARCH_SERVICE, SearchManager.class, new CachedServiceFetcher() { @Override - public SearchManager createService(ContextImpl ctx) { + public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new SearchManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }}); @@ -425,7 +430,7 @@ final class SystemServiceRegistry { registerService(Context.STORAGE_SERVICE, StorageManager.class, new CachedServiceFetcher() { @Override - public StorageManager createService(ContextImpl ctx) { + public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper()); }}); @@ -460,23 +465,23 @@ final class SystemServiceRegistry { registerService(Context.UI_MODE_SERVICE, UiModeManager.class, new CachedServiceFetcher() { @Override - public UiModeManager createService(ContextImpl ctx) { + public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new UiModeManager(); }}); registerService(Context.USB_SERVICE, UsbManager.class, new CachedServiceFetcher() { @Override - public UsbManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.USB_SERVICE); + public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE); return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); }}); registerService(Context.SERIAL_SERVICE, SerialManager.class, new CachedServiceFetcher() { @Override - public SerialManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.SERIAL_SERVICE); + public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE); return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); }}); @@ -498,8 +503,8 @@ final class SystemServiceRegistry { registerService(Context.WIFI_SERVICE, WifiManager.class, new CachedServiceFetcher() { @Override - public WifiManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.WIFI_SERVICE); + public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE); IWifiManager service = IWifiManager.Stub.asInterface(b); return new WifiManager(ctx.getOuterContext(), service, ConnectivityThread.getInstanceLooper()); @@ -508,8 +513,8 @@ final class SystemServiceRegistry { registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class, new StaticServiceFetcher() { @Override - public WifiP2pManager createService() { - IBinder b = ServiceManager.getService(Context.WIFI_P2P_SERVICE); + public WifiP2pManager createService() throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE); IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b); return new WifiP2pManager(service); }}); @@ -517,8 +522,8 @@ final class SystemServiceRegistry { registerService(Context.WIFI_NAN_SERVICE, WifiNanManager.class, new StaticServiceFetcher() { @Override - public WifiNanManager createService() { - IBinder b = ServiceManager.getService(Context.WIFI_NAN_SERVICE); + public WifiNanManager createService() throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_NAN_SERVICE); IWifiNanManager service = IWifiNanManager.Stub.asInterface(b); if (service == null) { return null; @@ -529,8 +534,8 @@ final class SystemServiceRegistry { registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class, new CachedServiceFetcher() { @Override - public WifiScanner createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.WIFI_SCANNING_SERVICE); + public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE); IWifiScanner service = IWifiScanner.Stub.asInterface(b); return new WifiScanner(ctx.getOuterContext(), service, ConnectivityThread.getInstanceLooper()); @@ -539,8 +544,8 @@ final class SystemServiceRegistry { registerService(Context.WIFI_RTT_SERVICE, RttManager.class, new CachedServiceFetcher() { @Override - public RttManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.WIFI_RTT_SERVICE); + public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_SERVICE); IRttManager service = IRttManager.Stub.asInterface(b); return new RttManager(ctx.getOuterContext(), service, ConnectivityThread.getInstanceLooper()); @@ -549,8 +554,8 @@ final class SystemServiceRegistry { registerService(Context.ETHERNET_SERVICE, EthernetManager.class, new CachedServiceFetcher() { @Override - public EthernetManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.ETHERNET_SERVICE); + public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE); IEthernetManager service = IEthernetManager.Stub.asInterface(b); return new EthernetManager(ctx.getOuterContext(), service); }}); @@ -565,8 +570,8 @@ final class SystemServiceRegistry { registerService(Context.USER_SERVICE, UserManager.class, new CachedServiceFetcher() { @Override - public UserManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.USER_SERVICE); + public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE); IUserManager service = IUserManager.Stub.asInterface(b); return new UserManager(ctx, service); }}); @@ -574,8 +579,8 @@ final class SystemServiceRegistry { registerService(Context.APP_OPS_SERVICE, AppOpsManager.class, new CachedServiceFetcher() { @Override - public AppOpsManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE); + public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE); IAppOpsService service = IAppOpsService.Stub.asInterface(b); return new AppOpsManager(ctx, service); }}); @@ -597,8 +602,8 @@ final class SystemServiceRegistry { registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class, new CachedServiceFetcher() { @Override - public RestrictionsManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.RESTRICTIONS_SERVICE); + public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE); IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b); return new RestrictionsManager(ctx, service); }}); @@ -606,8 +611,8 @@ final class SystemServiceRegistry { registerService(Context.PRINT_SERVICE, PrintManager.class, new CachedServiceFetcher() { @Override - public PrintManager createService(ContextImpl ctx) { - IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE); + public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder iBinder = ServiceManager.getServiceOrThrow(Context.PRINT_SERVICE); IPrintManager service = IPrintManager.Stub.asInterface(iBinder); return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(), UserHandle.getAppId(Process.myUid())); @@ -616,7 +621,7 @@ final class SystemServiceRegistry { registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class, new CachedServiceFetcher() { @Override - public ConsumerIrManager createService(ContextImpl ctx) { + public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new ConsumerIrManager(ctx); }}); @@ -630,16 +635,16 @@ final class SystemServiceRegistry { registerService(Context.TRUST_SERVICE, TrustManager.class, new StaticServiceFetcher() { @Override - public TrustManager createService() { - IBinder b = ServiceManager.getService(Context.TRUST_SERVICE); + public TrustManager createService() throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE); return new TrustManager(b); }}); registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class, new CachedServiceFetcher() { @Override - public FingerprintManager createService(ContextImpl ctx) { - IBinder binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE); + public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE); IFingerprintService service = IFingerprintService.Stub.asInterface(binder); return new FingerprintManager(ctx.getOuterContext(), service); }}); @@ -647,8 +652,8 @@ final class SystemServiceRegistry { registerService(Context.TV_INPUT_SERVICE, TvInputManager.class, new StaticServiceFetcher() { @Override - public TvInputManager createService() { - IBinder iBinder = ServiceManager.getService(Context.TV_INPUT_SERVICE); + public TvInputManager createService() throws ServiceNotFoundException { + IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE); ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder); return new TvInputManager(service, UserHandle.myUserId()); }}); @@ -656,15 +661,15 @@ final class SystemServiceRegistry { registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class, new CachedServiceFetcher() { @Override - public NetworkScoreManager createService(ContextImpl ctx) { + public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new NetworkScoreManager(ctx); }}); registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class, new CachedServiceFetcher() { @Override - public UsageStatsManager createService(ContextImpl ctx) { - IBinder iBinder = ServiceManager.getService(Context.USAGE_STATS_SERVICE); + public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE); IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder); return new UsageStatsManager(ctx.getOuterContext(), service); }}); @@ -672,23 +677,23 @@ final class SystemServiceRegistry { registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class, new CachedServiceFetcher() { @Override - public NetworkStatsManager createService(ContextImpl ctx) { + public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new NetworkStatsManager(ctx.getOuterContext()); }}); registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class, new StaticServiceFetcher() { @Override - public JobScheduler createService() { - IBinder b = ServiceManager.getService(Context.JOB_SCHEDULER_SERVICE); + public JobScheduler createService() throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE); return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b)); }}); registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class, new StaticServiceFetcher() { @Override - public PersistentDataBlockManager createService() { - IBinder b = ServiceManager.getService(Context.PERSISTENT_DATA_BLOCK_SERVICE); + public PersistentDataBlockManager createService() throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE); IPersistentDataBlockService persistentDataBlockService = IPersistentDataBlockService.Stub.asInterface(b); if (persistentDataBlockService != null) { @@ -709,19 +714,16 @@ final class SystemServiceRegistry { registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class, new CachedServiceFetcher() { @Override - public AppWidgetManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE); + public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE); return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b)); }}); registerService(Context.MIDI_SERVICE, MidiManager.class, new CachedServiceFetcher() { @Override - public MidiManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.MIDI_SERVICE); - if (b == null) { - return null; - } + public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE); return new MidiManager(IMidiManager.Stub.asInterface(b)); }}); @@ -735,43 +737,41 @@ final class SystemServiceRegistry { registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class, new CachedServiceFetcher() { @Override - public HardwarePropertiesManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.HARDWARE_PROPERTIES_SERVICE); - IHardwarePropertiesManager service = - IHardwarePropertiesManager.Stub.asInterface(b); - if (service == null) { - Log.wtf(TAG, "Failed to get hardwareproperties service."); - return null; - } - return new HardwarePropertiesManager(ctx, service); + public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE); + IHardwarePropertiesManager service = + IHardwarePropertiesManager.Stub.asInterface(b); + return new HardwarePropertiesManager(ctx, service); }}); registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class, new CachedServiceFetcher() { @Override - public SoundTriggerManager createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(Context.SOUND_TRIGGER_SERVICE); + public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE); return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b)); }}); registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class, new CachedServiceFetcher() { @Override - public ShortcutManager createService(ContextImpl ctx) { - return new ShortcutManager(ctx); + public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE); + return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b)); }}); registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class, new CachedServiceFetcher() { @Override - public SystemHealthManager createService(ContextImpl ctx) { - return new SystemHealthManager(); + public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException { + IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME); + return new SystemHealthManager(IBatteryStats.Stub.asInterface(b)); }}); registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class, new CachedServiceFetcher() { @Override - public ContextHubManager createService(ContextImpl ctx) { + public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new ContextHubManager(ctx.getOuterContext(), ctx.mMainThread.getHandler().getLooper()); }}); @@ -836,14 +836,18 @@ final class SystemServiceRegistry { // Fetch or create the service. Object service = cache[mCacheIndex]; if (service == null) { - service = createService(ctx); - cache[mCacheIndex] = service; + try { + service = createService(ctx); + cache[mCacheIndex] = service; + } catch (ServiceNotFoundException e) { + Log.w(TAG, e.getMessage(), DEBUG_EARLY_CALLERS ? e : null); + } } return (T)service; } } - public abstract T createService(ContextImpl ctx); + public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException; } /** @@ -857,13 +861,17 @@ final class SystemServiceRegistry { public final T getService(ContextImpl unused) { synchronized (StaticServiceFetcher.this) { if (mCachedInstance == null) { - mCachedInstance = createService(); + try { + mCachedInstance = createService(); + } catch (ServiceNotFoundException e) { + Log.w(TAG, e.getMessage(), DEBUG_EARLY_CALLERS ? e : null); + } } return mCachedInstance; } } - public abstract T createService(); + public abstract T createService() throws ServiceNotFoundException; } /** @@ -886,13 +894,16 @@ final class SystemServiceRegistry { // it's the application context very early in app initialization. In both these // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it // to the service. http://b/27532714 . - mCachedInstance = createService(appContext != null ? appContext : ctx); + try { + mCachedInstance = createService(appContext != null ? appContext : ctx); + } catch (ServiceNotFoundException e) { + Log.w(TAG, e.getMessage(), DEBUG_EARLY_CALLERS ? e : null); + } } return mCachedInstance; } } - public abstract T createService(Context applicationContext); + public abstract T createService(Context applicationContext) throws ServiceNotFoundException; } - } diff --git a/core/java/android/app/UiModeManager.java b/core/java/android/app/UiModeManager.java index 69e8df87384b6..0046b0e4a8bf7 100644 --- a/core/java/android/app/UiModeManager.java +++ b/core/java/android/app/UiModeManager.java @@ -22,6 +22,7 @@ import android.content.Context; import android.content.res.Configuration; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.util.Log; import java.lang.annotation.Retention; @@ -123,9 +124,9 @@ public class UiModeManager { private IUiModeManager mService; - /*package*/ UiModeManager() { + /*package*/ UiModeManager() throws ServiceNotFoundException { mService = IUiModeManager.Stub.asInterface( - ServiceManager.getService(Context.UI_MODE_SERVICE)); + ServiceManager.getServiceOrThrow(Context.UI_MODE_SERVICE)); } /** diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java index 3a2038fbc9de9..6ac7132166f7c 100644 --- a/core/java/android/app/admin/DevicePolicyManager.java +++ b/core/java/android/app/admin/DevicePolicyManager.java @@ -46,6 +46,7 @@ import android.os.RemoteException; import android.os.ServiceManager; import android.os.UserHandle; import android.os.UserManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.provider.ContactsContract.Directory; import android.provider.Settings; import android.security.Credentials; @@ -99,28 +100,20 @@ public class DevicePolicyManager { private final IDevicePolicyManager mService; private final boolean mParentInstance; - private DevicePolicyManager(Context context, boolean parentInstance) { - this(context, - IDevicePolicyManager.Stub.asInterface( - ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)), - parentInstance); + /** @hide */ + public DevicePolicyManager(Context context, IDevicePolicyManager service) { + this(context, service, false); } /** @hide */ @VisibleForTesting - protected DevicePolicyManager( - Context context, IDevicePolicyManager service, boolean parentInstance) { + protected DevicePolicyManager(Context context, IDevicePolicyManager service, + boolean parentInstance) { mContext = context; mService = service; mParentInstance = parentInstance; } - /** @hide */ - public static DevicePolicyManager create(Context context) { - DevicePolicyManager me = new DevicePolicyManager(context, false); - return me.mService != null ? me : null; - } - /** @hide test will override it. */ @VisibleForTesting protected int myUserId() { @@ -6054,7 +6047,7 @@ public class DevicePolicyManager { if (!mService.isManagedProfile(admin)) { throw new SecurityException("The current user does not have a parent profile."); } - return new DevicePolicyManager(mContext, true); + return new DevicePolicyManager(mContext, mService, true); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -6146,7 +6139,7 @@ public class DevicePolicyManager { throw new SecurityException("The user " + uInfo.id + " does not have a parent profile."); } - return new DevicePolicyManager(mContext, true); + return new DevicePolicyManager(mContext, mService, true); } /** diff --git a/core/java/android/app/usage/NetworkStatsManager.java b/core/java/android/app/usage/NetworkStatsManager.java index 7961a72a12e44..e805fabc79672 100644 --- a/core/java/android/app/usage/NetworkStatsManager.java +++ b/core/java/android/app/usage/NetworkStatsManager.java @@ -34,6 +34,7 @@ import android.os.Handler; import android.os.Looper; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.util.Log; /** @@ -95,10 +96,10 @@ public class NetworkStatsManager { /** * {@hide} */ - public NetworkStatsManager(Context context) { + public NetworkStatsManager(Context context) throws ServiceNotFoundException { mContext = context; mService = INetworkStatsService.Stub.asInterface( - ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); + ServiceManager.getServiceOrThrow(Context.NETWORK_STATS_SERVICE)); } /** diff --git a/core/java/android/content/ClipboardManager.java b/core/java/android/content/ClipboardManager.java index 7244227584ee3..c28172c6347ca 100644 --- a/core/java/android/content/ClipboardManager.java +++ b/core/java/android/content/ClipboardManager.java @@ -16,12 +16,11 @@ package android.content; -import android.content.Context; +import android.os.Handler; import android.os.Message; import android.os.RemoteException; -import android.os.Handler; -import android.os.IBinder; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import java.util.ArrayList; @@ -49,10 +48,8 @@ import java.util.ArrayList; * @see android.content.Context#getSystemService */ public class ClipboardManager extends android.text.ClipboardManager { - private final static Object sStaticLock = new Object(); - private static IClipboard sService; - private final Context mContext; + private final IClipboard mService; private final ArrayList mPrimaryClipChangedListeners = new ArrayList(); @@ -93,20 +90,11 @@ public class ClipboardManager extends android.text.ClipboardManager { void onPrimaryClipChanged(); } - static private IClipboard getService() { - synchronized (sStaticLock) { - if (sService != null) { - return sService; - } - IBinder b = ServiceManager.getService("clipboard"); - sService = IClipboard.Stub.asInterface(b); - return sService; - } - } - /** {@hide} */ - public ClipboardManager(Context context, Handler handler) { + public ClipboardManager(Context context, Handler handler) throws ServiceNotFoundException { mContext = context; + mService = IClipboard.Stub.asInterface( + ServiceManager.getServiceOrThrow(Context.CLIPBOARD_SERVICE)); } /** @@ -120,7 +108,7 @@ public class ClipboardManager extends android.text.ClipboardManager { if (clip != null) { clip.prepareToLeaveProcess(true); } - getService().setPrimaryClip(clip, mContext.getOpPackageName()); + mService.setPrimaryClip(clip, mContext.getOpPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -131,7 +119,7 @@ public class ClipboardManager extends android.text.ClipboardManager { */ public ClipData getPrimaryClip() { try { - return getService().getPrimaryClip(mContext.getOpPackageName()); + return mService.getPrimaryClip(mContext.getOpPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -143,7 +131,7 @@ public class ClipboardManager extends android.text.ClipboardManager { */ public ClipDescription getPrimaryClipDescription() { try { - return getService().getPrimaryClipDescription(mContext.getOpPackageName()); + return mService.getPrimaryClipDescription(mContext.getOpPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -154,7 +142,7 @@ public class ClipboardManager extends android.text.ClipboardManager { */ public boolean hasPrimaryClip() { try { - return getService().hasPrimaryClip(mContext.getOpPackageName()); + return mService.hasPrimaryClip(mContext.getOpPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -164,7 +152,7 @@ public class ClipboardManager extends android.text.ClipboardManager { synchronized (mPrimaryClipChangedListeners) { if (mPrimaryClipChangedListeners.size() == 0) { try { - getService().addPrimaryClipChangedListener( + mService.addPrimaryClipChangedListener( mPrimaryClipChangedServiceListener, mContext.getOpPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); @@ -179,7 +167,7 @@ public class ClipboardManager extends android.text.ClipboardManager { mPrimaryClipChangedListeners.remove(what); if (mPrimaryClipChangedListeners.size() == 0) { try { - getService().removePrimaryClipChangedListener( + mService.removePrimaryClipChangedListener( mPrimaryClipChangedServiceListener); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); @@ -217,7 +205,7 @@ public class ClipboardManager extends android.text.ClipboardManager { @Deprecated public boolean hasText() { try { - return getService().hasClipboardText(mContext.getOpPackageName()); + return mService.hasClipboardText(mContext.getOpPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } diff --git a/core/java/android/hardware/ConsumerIrManager.java b/core/java/android/hardware/ConsumerIrManager.java index 9fa1c3f738239..b221e1604949a 100644 --- a/core/java/android/hardware/ConsumerIrManager.java +++ b/core/java/android/hardware/ConsumerIrManager.java @@ -19,6 +19,7 @@ package android.hardware; import android.content.Context; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.util.Log; /** @@ -40,10 +41,10 @@ public final class ConsumerIrManager { /** * @hide to prevent subclassing from outside of the framework */ - public ConsumerIrManager(Context context) { + public ConsumerIrManager(Context context) throws ServiceNotFoundException { mPackageName = context.getPackageName(); mService = IConsumerIrService.Stub.asInterface( - ServiceManager.getService(Context.CONSUMER_IR_SERVICE)); + ServiceManager.getServiceOrThrow(Context.CONSUMER_IR_SERVICE)); } /** diff --git a/core/java/android/hardware/input/InputManager.java b/core/java/android/hardware/input/InputManager.java index 803337b016f91..2b0593fd38171 100644 --- a/core/java/android/hardware/input/InputManager.java +++ b/core/java/android/hardware/input/InputManager.java @@ -33,6 +33,7 @@ import android.os.RemoteException; import android.os.ServiceManager; import android.os.SystemClock; import android.os.Vibrator; +import android.os.ServiceManager.ServiceNotFoundException; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; import android.util.Log; @@ -225,8 +226,12 @@ public final class InputManager { public static InputManager getInstance() { synchronized (InputManager.class) { if (sInstance == null) { - IBinder b = ServiceManager.getService(Context.INPUT_SERVICE); - sInstance = new InputManager(IInputManager.Stub.asInterface(b)); + try { + sInstance = new InputManager(IInputManager.Stub + .asInterface(ServiceManager.getServiceOrThrow(Context.INPUT_SERVICE))); + } catch (ServiceNotFoundException e) { + throw new IllegalStateException(e); + } } return sInstance; } diff --git a/core/java/android/hardware/location/ContextHubManager.java b/core/java/android/hardware/location/ContextHubManager.java index 3bc15621be3a6..2aed53c7deb41 100644 --- a/core/java/android/hardware/location/ContextHubManager.java +++ b/core/java/android/hardware/location/ContextHubManager.java @@ -18,10 +18,10 @@ package android.hardware.location; import android.annotation.SystemApi; import android.content.Context; import android.os.Handler; -import android.os.IBinder; import android.os.Looper; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.util.Log; /** @@ -38,7 +38,7 @@ public final class ContextHubManager { private static final String TAG = "ContextHubManager"; private final Looper mMainLooper; - private IContextHubService mContextHubService; + private final IContextHubService mService; private Callback mCallback; private Handler mCallbackHandler; @@ -92,13 +92,11 @@ public final class ContextHubManager { * @return array of context hub handles */ public int[] getContextHubHandles() { - int[] retVal = null; try { - retVal = getBinder().getContextHubHandles(); + return mService.getContextHubHandles(); } catch (RemoteException e) { - Log.w(TAG, "Could not fetch context hub handles : " + e); + throw e.rethrowFromSystemServer(); } - return retVal; } /** @@ -110,14 +108,11 @@ public final class ContextHubManager { * @see ContextHubInfo */ public ContextHubInfo getContextHubInfo(int hubHandle) { - ContextHubInfo retVal = null; try { - retVal = getBinder().getContextHubInfo(hubHandle); + return mService.getContextHubInfo(hubHandle); } catch (RemoteException e) { - Log.w(TAG, "Could not fetch context hub info :" + e); + throw e.rethrowFromSystemServer(); } - - return retVal; } /** @@ -132,19 +127,11 @@ public final class ContextHubManager { * @see NanoApp */ public int loadNanoApp(int hubHandle, NanoApp app) { - int retVal = -1; - - if (app == null) { - return retVal; - } - try { - retVal = getBinder().loadNanoApp(hubHandle, app); + return mService.loadNanoApp(hubHandle, app); } catch (RemoteException e) { - Log.w(TAG, "Could not load nanoApp :" + e); + throw e.rethrowFromSystemServer(); } - - return retVal; } /** @@ -155,15 +142,11 @@ public final class ContextHubManager { * @return int 0 on success, -1 otherwise */ public int unloadNanoApp(int nanoAppHandle) { - int retVal = -1; - try { - retVal = getBinder().unloadNanoApp(nanoAppHandle); + return mService.unloadNanoApp(nanoAppHandle); } catch (RemoteException e) { - Log.w(TAG, "Could not fetch unload nanoApp :" + e); + throw e.rethrowFromSystemServer(); } - - return retVal; } /** @@ -175,15 +158,11 @@ public final class ContextHubManager { * @see NanoAppInstanceInfo */ public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppHandle) { - NanoAppInstanceInfo retVal = null; - try { - retVal = getBinder().getNanoAppInstanceInfo(nanoAppHandle); + return mService.getNanoAppInstanceInfo(nanoAppHandle); } catch (RemoteException e) { - Log.w(TAG, "Could not fetch nanoApp info :" + e); + throw e.rethrowFromSystemServer(); } - - return retVal; } /** @@ -197,13 +176,11 @@ public final class ContextHubManager { * @return int[] Array of handles to any found nano apps */ public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) { - int[] retVal = null; try { - retVal = getBinder().findNanoAppOnHub(hubHandle, filter); + return mService.findNanoAppOnHub(hubHandle, filter); } catch (RemoteException e) { - Log.w(TAG, "Could not query nanoApp instance :" + e); + throw e.rethrowFromSystemServer(); } - return retVal; } /** @@ -218,19 +195,11 @@ public final class ContextHubManager { * @return int 0 on success, -1 otherwise */ public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage message) { - int retVal = -1; - - if (message == null || message.getData() == null) { - Log.w(TAG, "null ptr"); - return retVal; - } try { - retVal = getBinder().sendMessage(hubHandle, nanoAppHandle, message); + return mService.sendMessage(hubHandle, nanoAppHandle, message); } catch (RemoteException e) { - Log.w(TAG, "Could not send message :" + e.toString()); + throw e.rethrowFromSystemServer(); } - - return retVal; } /** @@ -318,7 +287,7 @@ public final class ContextHubManager { return 0; } - private IContextHubCallback.Stub mClientCallback = new IContextHubCallback.Stub() { + private final IContextHubCallback.Stub mClientCallback = new IContextHubCallback.Stub() { @Override public void onMessageReceipt(final int hubId, final int nanoAppId, final ContextHubMessage message) { @@ -346,29 +315,17 @@ public final class ContextHubManager { } }; - /** @hide */ - public ContextHubManager(Context context, Looper mainLooper) { + /** @throws ServiceNotFoundException + * @hide */ + public ContextHubManager(Context context, Looper mainLooper) throws ServiceNotFoundException { mMainLooper = mainLooper; + mService = IContextHubService.Stub.asInterface( + ServiceManager.getServiceOrThrow(ContextHubService.CONTEXTHUB_SERVICE)); - IBinder b = ServiceManager.getService(ContextHubService.CONTEXTHUB_SERVICE); - if (b != null) { - mContextHubService = IContextHubService.Stub.asInterface(b); - - try { - getBinder().registerCallback(mClientCallback); - } catch (RemoteException e) { - Log.w(TAG, "Could not register callback:" + e); - } - - } else { - Log.w(TAG, "failed to getService"); + try { + mService.registerCallback(mClientCallback); + } catch (RemoteException e) { + Log.w(TAG, "Could not register callback:" + e); } } - - private IContextHubService getBinder() throws RemoteException { - if (mContextHubService == null) { - throw new RemoteException("Service not connected."); - } - return mContextHubService; - } } diff --git a/core/java/android/hardware/location/ContextHubService.java b/core/java/android/hardware/location/ContextHubService.java index 062c9580c1e42..893ed3f7c2014 100644 --- a/core/java/android/hardware/location/ContextHubService.java +++ b/core/java/android/hardware/location/ContextHubService.java @@ -154,6 +154,9 @@ public class ContextHubService extends IContextHubService.Stub { Log.e(TAG, "Invalid contextHubhandle " + contextHubHandle); return -1; } + if (app == null) { + return -1; + } int[] msgHeader = new int[MSG_LOAD_APP_HEADER_SIZE]; msgHeader[HEADER_FIELD_HUB_HANDLE] = contextHubHandle; @@ -240,6 +243,11 @@ public class ContextHubService extends IContextHubService.Stub { throws RemoteException { checkPermissions(); + if (msg == null || msg.getData() == null) { + Log.w(TAG, "null ptr"); + return -1; + } + int[] msgHeader = new int[MSG_HEADER_SIZE]; msgHeader[HEADER_FIELD_HUB_HANDLE] = hubHandle; msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppHandle; diff --git a/core/java/android/net/NetworkScoreManager.java b/core/java/android/net/NetworkScoreManager.java index 01c160f1bc454..c2286ad8ae132 100644 --- a/core/java/android/net/NetworkScoreManager.java +++ b/core/java/android/net/NetworkScoreManager.java @@ -23,9 +23,9 @@ import android.annotation.SystemApi; import android.content.Context; import android.content.Intent; import android.net.NetworkScorerAppManager.NetworkScorerAppData; -import android.os.IBinder; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.os.UserHandle; /** @@ -127,10 +127,10 @@ public class NetworkScoreManager { private final INetworkScoreService mService; /** @hide */ - public NetworkScoreManager(Context context) { + public NetworkScoreManager(Context context) throws ServiceNotFoundException { mContext = context; - IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE); - mService = INetworkScoreService.Stub.asInterface(iBinder); + mService = INetworkScoreService.Stub + .asInterface(ServiceManager.getServiceOrThrow(Context.NETWORK_SCORE_SERVICE)); } /** diff --git a/core/java/android/os/BatteryManager.java b/core/java/android/os/BatteryManager.java index d39968a09023f..252385faf0899 100644 --- a/core/java/android/os/BatteryManager.java +++ b/core/java/android/os/BatteryManager.java @@ -16,11 +16,6 @@ package android.os; -import android.content.Context; -import android.os.BatteryProperty; -import android.os.IBatteryPropertiesRegistrar; -import android.os.RemoteException; -import android.os.ServiceManager; import com.android.internal.app.IBatteryStats; /** @@ -218,6 +213,13 @@ public class BatteryManager { ServiceManager.getService("batteryproperties")); } + /** {@hide} */ + public BatteryManager(IBatteryStats batteryStats, + IBatteryPropertiesRegistrar batteryPropertiesRegistrar) { + mBatteryStats = batteryStats; + mBatteryPropertiesRegistrar = batteryPropertiesRegistrar; + } + /** * Return true if the battery is currently considered to be charging. This means that * the device is plugged in and is supplying sufficient power that the battery level is diff --git a/core/java/android/os/ServiceManager.java b/core/java/android/os/ServiceManager.java index 2ba4aa433713e..640105ce8f98a 100644 --- a/core/java/android/os/ServiceManager.java +++ b/core/java/android/os/ServiceManager.java @@ -16,10 +16,10 @@ package android.os; -import com.android.internal.os.BinderInternal; - import android.util.Log; +import com.android.internal.os.BinderInternal; + import java.util.HashMap; import java.util.Map; @@ -60,6 +60,21 @@ public final class ServiceManager { return null; } + /** + * Returns a reference to a service with the given name, or throws + * {@link NullPointerException} if none is found. + * + * @hide + */ + public static IBinder getServiceOrThrow(String name) throws ServiceNotFoundException { + final IBinder binder = getService(name); + if (binder != null) { + return binder; + } else { + throw new ServiceNotFoundException(name); + } + } + /** * Place a new @a service called @a name into the service * manager. @@ -138,4 +153,17 @@ public final class ServiceManager { } sCache.putAll(cache); } + + /** + * Exception thrown when no service published for given name. This might be + * thrown early during boot before certain services have published + * themselves. + * + * @hide + */ + public static class ServiceNotFoundException extends Exception { + public ServiceNotFoundException(String name) { + super("No service published for: " + name); + } + } } diff --git a/core/java/android/os/health/SystemHealthManager.java b/core/java/android/os/health/SystemHealthManager.java index 32e8425cbe07b..7c0af2508a933 100644 --- a/core/java/android/os/health/SystemHealthManager.java +++ b/core/java/android/os/health/SystemHealthManager.java @@ -21,6 +21,7 @@ import android.os.BatteryStats; import android.os.Process; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import com.android.internal.app.IBatteryStats; @@ -47,8 +48,12 @@ public class SystemHealthManager { * @hide */ public SystemHealthManager() { - mBatteryStats = IBatteryStats.Stub.asInterface( - ServiceManager.getService(BatteryStats.SERVICE_NAME)); + this(IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))); + } + + /** {@hide} */ + public SystemHealthManager(IBatteryStats batteryStats) { + mBatteryStats = batteryStats; } /** diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java index c5507b9841bc6..cee7553750f65 100644 --- a/core/java/android/os/storage/StorageManager.java +++ b/core/java/android/os/storage/StorageManager.java @@ -35,6 +35,7 @@ import android.os.Message; import android.os.ParcelFileDescriptor; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.os.SystemProperties; import android.os.UserHandle; import android.provider.Settings; @@ -341,7 +342,7 @@ public class StorageManager { * Constructs a StorageManager object through which an application can * can communicate with the systems mount service. * - * @param tgtLooper The {@link android.os.Looper} which events will be received on. + * @param looper The {@link android.os.Looper} which events will be received on. * *

Applications can get instance of this class by calling * {@link android.content.Context#getSystemService(java.lang.String)} with an argument @@ -349,14 +350,11 @@ public class StorageManager { * * @hide */ - public StorageManager(Context context, Looper looper) { + public StorageManager(Context context, Looper looper) throws ServiceNotFoundException { mContext = context; mResolver = context.getContentResolver(); mLooper = looper; - mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount")); - if (mMountService == null) { - throw new IllegalStateException("Failed to find running mount service"); - } + mMountService = IMountService.Stub.asInterface(ServiceManager.getServiceOrThrow("mount")); } /** diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index c0c8e64aacc8b..77e8c0e92b6ec 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -41,6 +41,7 @@ import android.os.RemoteException; import android.os.ResultReceiver; import android.os.ServiceManager; import android.os.Trace; +import android.os.ServiceManager.ServiceNotFoundException; import android.text.TextUtils; import android.text.style.SuggestionSpan; import android.util.Log; @@ -55,6 +56,7 @@ import android.view.InputEventSender; import android.view.KeyEvent; import android.view.View; import android.view.ViewRootImpl; +import android.view.textservice.TextServicesManager; import java.io.FileDescriptor; import java.io.PrintWriter; @@ -622,8 +624,9 @@ public final class InputMethodManager { final InputConnection mDummyInputConnection = new BaseInputConnection(this, false); - InputMethodManager(IInputMethodManager service, Looper looper) { - mService = service; + InputMethodManager(Looper looper) throws ServiceNotFoundException { + mService = IInputMethodManager.Stub.asInterface( + ServiceManager.getServiceOrThrow(Context.INPUT_METHOD_SERVICE)); mMainLooper = looper; mH = new H(looper); mIInputContext = new ControlledInputConnectionWrapper(looper, @@ -638,9 +641,11 @@ public final class InputMethodManager { public static InputMethodManager getInstance() { synchronized (InputMethodManager.class) { if (sInstance == null) { - IBinder b = ServiceManager.getService(Context.INPUT_METHOD_SERVICE); - IInputMethodManager service = IInputMethodManager.Stub.asInterface(b); - sInstance = new InputMethodManager(service, Looper.getMainLooper()); + try { + sInstance = new InputMethodManager(Looper.getMainLooper()); + } catch (ServiceNotFoundException e) { + throw new IllegalStateException(e); + } } return sInstance; } diff --git a/core/java/android/view/textservice/TextServicesManager.java b/core/java/android/view/textservice/TextServicesManager.java index 89d879ee6ee21..0f168f392a205 100644 --- a/core/java/android/view/textservice/TextServicesManager.java +++ b/core/java/android/view/textservice/TextServicesManager.java @@ -16,16 +16,16 @@ package android.view.textservice; -import com.android.internal.textservice.ITextServicesManager; - import android.content.Context; import android.os.Bundle; -import android.os.IBinder; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; import android.util.Log; import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener; +import com.android.internal.textservice.ITextServicesManager; + import java.util.Locale; /** @@ -66,13 +66,12 @@ public final class TextServicesManager { private static final boolean DBG = false; private static TextServicesManager sInstance; - private static ITextServicesManager sService; - private TextServicesManager() { - if (sService == null) { - IBinder b = ServiceManager.getService(Context.TEXT_SERVICES_MANAGER_SERVICE); - sService = ITextServicesManager.Stub.asInterface(b); - } + private final ITextServicesManager mService; + + private TextServicesManager() throws ServiceNotFoundException { + mService = ITextServicesManager.Stub.asInterface( + ServiceManager.getServiceOrThrow(Context.TEXT_SERVICES_MANAGER_SERVICE)); } /** @@ -81,12 +80,15 @@ public final class TextServicesManager { */ public static TextServicesManager getInstance() { synchronized (TextServicesManager.class) { - if (sInstance != null) { - return sInstance; + if (sInstance == null) { + try { + sInstance = new TextServicesManager(); + } catch (ServiceNotFoundException e) { + throw new IllegalStateException(e); + } } - sInstance = new TextServicesManager(); + return sInstance; } - return sInstance; } /** @@ -131,7 +133,7 @@ public final class TextServicesManager { final SpellCheckerInfo sci; try { - sci = sService.getCurrentSpellChecker(null); + sci = mService.getCurrentSpellChecker(null); } catch (RemoteException e) { return null; } @@ -170,13 +172,13 @@ public final class TextServicesManager { return null; } final SpellCheckerSession session = new SpellCheckerSession( - sci, sService, listener, subtypeInUse); + sci, mService, listener, subtypeInUse); try { - sService.getSpellCheckerService(sci.getId(), subtypeInUse.getLocale(), + mService.getSpellCheckerService(sci.getId(), subtypeInUse.getLocale(), session.getTextServicesSessionListener(), session.getSpellCheckerSessionListener(), bundle); } catch (RemoteException e) { - return null; + throw e.rethrowFromSystemServer(); } return session; } @@ -186,14 +188,13 @@ public final class TextServicesManager { */ public SpellCheckerInfo[] getEnabledSpellCheckers() { try { - final SpellCheckerInfo[] retval = sService.getEnabledSpellCheckers(); + final SpellCheckerInfo[] retval = mService.getEnabledSpellCheckers(); if (DBG) { Log.d(TAG, "getEnabledSpellCheckers: " + (retval != null ? retval.length : "null")); } return retval; } catch (RemoteException e) { - Log.e(TAG, "Error in getEnabledSpellCheckers: " + e); - return null; + throw e.rethrowFromSystemServer(); } } @@ -203,9 +204,9 @@ public final class TextServicesManager { public SpellCheckerInfo getCurrentSpellChecker() { try { // Passing null as a locale for ICS - return sService.getCurrentSpellChecker(null); + return mService.getCurrentSpellChecker(null); } catch (RemoteException e) { - return null; + throw e.rethrowFromSystemServer(); } } @@ -217,9 +218,9 @@ public final class TextServicesManager { if (sci == null) { throw new NullPointerException("SpellCheckerInfo is null."); } - sService.setCurrentSpellChecker(null, sci.getId()); + mService.setCurrentSpellChecker(null, sci.getId()); } catch (RemoteException e) { - Log.e(TAG, "Error in setCurrentSpellChecker: " + e); + throw e.rethrowFromSystemServer(); } } @@ -229,17 +230,10 @@ public final class TextServicesManager { public SpellCheckerSubtype getCurrentSpellCheckerSubtype( boolean allowImplicitlySelectedSubtype) { try { - if (sService == null) { - // TODO: This is a workaround. Needs to investigate why sService could be null - // here. - Log.e(TAG, "sService is null."); - return null; - } // Passing null as a locale until we support multiple enabled spell checker subtypes. - return sService.getCurrentSpellCheckerSubtype(null, allowImplicitlySelectedSubtype); + return mService.getCurrentSpellCheckerSubtype(null, allowImplicitlySelectedSubtype); } catch (RemoteException e) { - Log.e(TAG, "Error in getCurrentSpellCheckerSubtype: " + e); - return null; + throw e.rethrowFromSystemServer(); } } @@ -254,9 +248,9 @@ public final class TextServicesManager { } else { hashCode = subtype.hashCode(); } - sService.setCurrentSpellCheckerSubtype(null, hashCode); + mService.setCurrentSpellCheckerSubtype(null, hashCode); } catch (RemoteException e) { - Log.e(TAG, "Error in setSpellCheckerSubtype:" + e); + throw e.rethrowFromSystemServer(); } } @@ -265,9 +259,9 @@ public final class TextServicesManager { */ public void setSpellCheckerEnabled(boolean enabled) { try { - sService.setSpellCheckerEnabled(enabled); + mService.setSpellCheckerEnabled(enabled); } catch (RemoteException e) { - Log.e(TAG, "Error in setSpellCheckerEnabled:" + e); + throw e.rethrowFromSystemServer(); } } @@ -276,10 +270,9 @@ public final class TextServicesManager { */ public boolean isSpellCheckerEnabled() { try { - return sService.isSpellCheckerEnabled(); + return mService.isSpellCheckerEnabled(); } catch (RemoteException e) { - Log.e(TAG, "Error in isSpellCheckerEnabled:" + e); - return false; + throw e.rethrowFromSystemServer(); } } }