diff options
author | Jeff Brown <jeffbrown@google.com> | 2015-02-24 18:53:21 -0800 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2015-02-25 17:04:13 -0800 |
commit | 6e53931f49f49245deef8622eb8e7dc6ccf04536 (patch) | |
tree | 0b7101049fca594901d09a089ba1f9588053cd1e | |
parent | ddd30928a7d8f410d7d13d5706da44ba20bac440 (diff) | |
download | frameworks_base-6e53931f49f49245deef8622eb8e7dc6ccf04536.zip frameworks_base-6e53931f49f49245deef8622eb8e7dc6ccf04536.tar.gz frameworks_base-6e53931f49f49245deef8622eb8e7dc6ccf04536.tar.bz2 |
Add Context.getSystemService(Class<?>).
Added an overload of getSystemService() that takes a class instead of a
service name to eliminate the extra cast and provide more type safety.
Cleaned up docs.
Removed the default constructor of BatteryManager which should not have
existed.
Change-Id: I9da46b20641fc83ecd3342560e5b94cb721f289c
-rw-r--r-- | api/current.txt | 30 | ||||
-rw-r--r-- | api/removed.txt | 4 | ||||
-rw-r--r-- | api/system-current.txt | 30 | ||||
-rw-r--r-- | api/system-removed.txt | 4 | ||||
-rw-r--r-- | core/java/android/app/ContextImpl.java | 675 | ||||
-rw-r--r-- | core/java/android/app/SystemServiceRegistry.java | 776 | ||||
-rw-r--r-- | core/java/android/content/Context.java | 81 | ||||
-rw-r--r-- | core/java/android/content/ContextWrapper.java | 19 | ||||
-rw-r--r-- | core/java/android/os/BatteryManager.java | 17 | ||||
-rw-r--r-- | test-runner/src/android/test/mock/MockContext.java | 5 | ||||
-rw-r--r-- | tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java | 30 |
11 files changed, 990 insertions, 681 deletions
diff --git a/api/current.txt b/api/current.txt index 6749453..de5b1e6 100644 --- a/api/current.txt +++ b/api/current.txt @@ -7319,6 +7319,7 @@ package android.content { method public final java.lang.String getString(int); method public final java.lang.String getString(int, java.lang.Object...); method public abstract java.lang.Object getSystemService(java.lang.String); + method public final T getSystemService(java.lang.Class<T>); method public final java.lang.CharSequence getText(int); method public abstract android.content.res.Resources.Theme getTheme(); method public abstract deprecated android.graphics.drawable.Drawable getWallpaper(); @@ -7448,7 +7449,7 @@ package android.content { method public int checkPermission(java.lang.String, int, int); method public int checkUriPermission(android.net.Uri, int, int, int); method public int checkUriPermission(android.net.Uri, java.lang.String, java.lang.String, int, int, int); - method public void clearWallpaper() throws java.io.IOException; + method public deprecated void clearWallpaper() throws java.io.IOException; method public android.content.Context createConfigurationContext(android.content.res.Configuration); method public android.content.Context createDisplayContext(android.view.Display); method public android.content.Context createPackageContext(java.lang.String, int) throws android.content.pm.PackageManager.NameNotFoundException; @@ -7491,20 +7492,21 @@ package android.content { method public android.content.res.Resources getResources(); method public android.content.SharedPreferences getSharedPreferences(java.lang.String, int); method public java.lang.Object getSystemService(java.lang.String); + method public java.lang.String getSystemServiceName(java.lang.Class<?>); method public android.content.res.Resources.Theme getTheme(); - method public android.graphics.drawable.Drawable getWallpaper(); - method public int getWallpaperDesiredMinimumHeight(); - method public int getWallpaperDesiredMinimumWidth(); + method public deprecated android.graphics.drawable.Drawable getWallpaper(); + method public deprecated int getWallpaperDesiredMinimumHeight(); + method public deprecated int getWallpaperDesiredMinimumWidth(); method public void grantUriPermission(java.lang.String, android.net.Uri, int); method public java.io.FileInputStream openFileInput(java.lang.String) throws java.io.FileNotFoundException; method public java.io.FileOutputStream openFileOutput(java.lang.String, int) throws java.io.FileNotFoundException; method public android.database.sqlite.SQLiteDatabase openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase.CursorFactory); method public android.database.sqlite.SQLiteDatabase openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase.CursorFactory, android.database.DatabaseErrorHandler); - method public android.graphics.drawable.Drawable peekWallpaper(); + method public deprecated android.graphics.drawable.Drawable peekWallpaper(); method public android.content.Intent registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter); method public android.content.Intent registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, java.lang.String, android.os.Handler); - method public void removeStickyBroadcast(android.content.Intent); - method public void removeStickyBroadcastAsUser(android.content.Intent, android.os.UserHandle); + method public deprecated void removeStickyBroadcast(android.content.Intent); + method public deprecated void removeStickyBroadcastAsUser(android.content.Intent, android.os.UserHandle); method public void revokeUriPermission(android.net.Uri, int); method public void sendBroadcast(android.content.Intent); method public void sendBroadcast(android.content.Intent, java.lang.String); @@ -7513,13 +7515,13 @@ package android.content { method public void sendOrderedBroadcast(android.content.Intent, java.lang.String); method public void sendOrderedBroadcast(android.content.Intent, java.lang.String, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); method public void sendOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, java.lang.String, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); - method public void sendStickyBroadcast(android.content.Intent); - method public void sendStickyBroadcastAsUser(android.content.Intent, android.os.UserHandle); - method public void sendStickyOrderedBroadcast(android.content.Intent, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); - method public void sendStickyOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); + method public deprecated void sendStickyBroadcast(android.content.Intent); + method public deprecated void sendStickyBroadcastAsUser(android.content.Intent, android.os.UserHandle); + method public deprecated void sendStickyOrderedBroadcast(android.content.Intent, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); + method public deprecated void sendStickyOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); method public void setTheme(int); - method public void setWallpaper(android.graphics.Bitmap) throws java.io.IOException; - method public void setWallpaper(java.io.InputStream) throws java.io.IOException; + method public deprecated void setWallpaper(android.graphics.Bitmap) throws java.io.IOException; + method public deprecated void setWallpaper(java.io.InputStream) throws java.io.IOException; method public void startActivities(android.content.Intent[]); method public void startActivities(android.content.Intent[], android.os.Bundle); method public void startActivity(android.content.Intent); @@ -21779,7 +21781,6 @@ package android.os { } public class BatteryManager { - ctor public BatteryManager(); method public int getIntProperty(int); method public long getLongProperty(int); field public static final int BATTERY_HEALTH_COLD = 7; // 0x7 @@ -29842,6 +29843,7 @@ package android.test.mock { method public android.content.res.Resources getResources(); method public android.content.SharedPreferences getSharedPreferences(java.lang.String, int); method public java.lang.Object getSystemService(java.lang.String); + method public java.lang.String getSystemServiceName(java.lang.Class<?>); method public android.content.res.Resources.Theme getTheme(); method public android.graphics.drawable.Drawable getWallpaper(); method public int getWallpaperDesiredMinimumHeight(); diff --git a/api/removed.txt b/api/removed.txt index 9322973..8668a77 100644 --- a/api/removed.txt +++ b/api/removed.txt @@ -16,6 +16,10 @@ package android.net { package android.os { + public class BatteryManager { + ctor public BatteryManager(); + } + public final class PowerManager { method public void goToSleep(long); method public deprecated void userActivity(long, boolean); diff --git a/api/system-current.txt b/api/system-current.txt index b2e44a3..92f0f4d 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -7524,6 +7524,7 @@ package android.content { method public final java.lang.String getString(int); method public final java.lang.String getString(int, java.lang.Object...); method public abstract java.lang.Object getSystemService(java.lang.String); + method public final T getSystemService(java.lang.Class<T>); method public final java.lang.CharSequence getText(int); method public abstract android.content.res.Resources.Theme getTheme(); method public abstract deprecated android.graphics.drawable.Drawable getWallpaper(); @@ -7659,7 +7660,7 @@ package android.content { method public int checkPermission(java.lang.String, int, int); method public int checkUriPermission(android.net.Uri, int, int, int); method public int checkUriPermission(android.net.Uri, java.lang.String, java.lang.String, int, int, int); - method public void clearWallpaper() throws java.io.IOException; + method public deprecated void clearWallpaper() throws java.io.IOException; method public android.content.Context createConfigurationContext(android.content.res.Configuration); method public android.content.Context createDisplayContext(android.view.Display); method public android.content.Context createPackageContext(java.lang.String, int) throws android.content.pm.PackageManager.NameNotFoundException; @@ -7702,20 +7703,21 @@ package android.content { method public android.content.res.Resources getResources(); method public android.content.SharedPreferences getSharedPreferences(java.lang.String, int); method public java.lang.Object getSystemService(java.lang.String); + method public java.lang.String getSystemServiceName(java.lang.Class<?>); method public android.content.res.Resources.Theme getTheme(); - method public android.graphics.drawable.Drawable getWallpaper(); - method public int getWallpaperDesiredMinimumHeight(); - method public int getWallpaperDesiredMinimumWidth(); + method public deprecated android.graphics.drawable.Drawable getWallpaper(); + method public deprecated int getWallpaperDesiredMinimumHeight(); + method public deprecated int getWallpaperDesiredMinimumWidth(); method public void grantUriPermission(java.lang.String, android.net.Uri, int); method public java.io.FileInputStream openFileInput(java.lang.String) throws java.io.FileNotFoundException; method public java.io.FileOutputStream openFileOutput(java.lang.String, int) throws java.io.FileNotFoundException; method public android.database.sqlite.SQLiteDatabase openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase.CursorFactory); method public android.database.sqlite.SQLiteDatabase openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase.CursorFactory, android.database.DatabaseErrorHandler); - method public android.graphics.drawable.Drawable peekWallpaper(); + method public deprecated android.graphics.drawable.Drawable peekWallpaper(); method public android.content.Intent registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter); method public android.content.Intent registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, java.lang.String, android.os.Handler); - method public void removeStickyBroadcast(android.content.Intent); - method public void removeStickyBroadcastAsUser(android.content.Intent, android.os.UserHandle); + method public deprecated void removeStickyBroadcast(android.content.Intent); + method public deprecated void removeStickyBroadcastAsUser(android.content.Intent, android.os.UserHandle); method public void revokeUriPermission(android.net.Uri, int); method public void sendBroadcast(android.content.Intent); method public void sendBroadcast(android.content.Intent, java.lang.String); @@ -7724,13 +7726,13 @@ package android.content { method public void sendOrderedBroadcast(android.content.Intent, java.lang.String); method public void sendOrderedBroadcast(android.content.Intent, java.lang.String, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); method public void sendOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, java.lang.String, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); - method public void sendStickyBroadcast(android.content.Intent); - method public void sendStickyBroadcastAsUser(android.content.Intent, android.os.UserHandle); - method public void sendStickyOrderedBroadcast(android.content.Intent, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); - method public void sendStickyOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); + method public deprecated void sendStickyBroadcast(android.content.Intent); + method public deprecated void sendStickyBroadcastAsUser(android.content.Intent, android.os.UserHandle); + method public deprecated void sendStickyOrderedBroadcast(android.content.Intent, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); + method public deprecated void sendStickyOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle); method public void setTheme(int); - method public void setWallpaper(android.graphics.Bitmap) throws java.io.IOException; - method public void setWallpaper(java.io.InputStream) throws java.io.IOException; + method public deprecated void setWallpaper(android.graphics.Bitmap) throws java.io.IOException; + method public deprecated void setWallpaper(java.io.InputStream) throws java.io.IOException; method public void startActivities(android.content.Intent[]); method public void startActivities(android.content.Intent[], android.os.Bundle); method public void startActivity(android.content.Intent); @@ -23368,7 +23370,6 @@ package android.os { } public class BatteryManager { - ctor public BatteryManager(); method public int getIntProperty(int); method public long getLongProperty(int); field public static final int BATTERY_HEALTH_COLD = 7; // 0x7 @@ -32003,6 +32004,7 @@ package android.test.mock { method public android.content.res.Resources getResources(); method public android.content.SharedPreferences getSharedPreferences(java.lang.String, int); method public java.lang.Object getSystemService(java.lang.String); + method public java.lang.String getSystemServiceName(java.lang.Class<?>); method public android.content.res.Resources.Theme getTheme(); method public android.graphics.drawable.Drawable getWallpaper(); method public int getWallpaperDesiredMinimumHeight(); diff --git a/api/system-removed.txt b/api/system-removed.txt index 9322973..8668a77 100644 --- a/api/system-removed.txt +++ b/api/system-removed.txt @@ -16,6 +16,10 @@ package android.net { package android.os { + public class BatteryManager { + ctor public BatteryManager(); + } + public final class PowerManager { method public void goToSleep(long); method public deprecated void userActivity(long, boolean); diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 503657b..eb27830 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -16,17 +16,9 @@ package android.app; -import android.app.usage.IUsageStatsManager; -import android.app.usage.UsageStatsManager; -import android.appwidget.AppWidgetManager; -import android.os.Build; -import android.service.persistentdata.IPersistentDataBlockService; -import android.service.persistentdata.PersistentDataBlockManager; - -import com.android.internal.appwidget.IAppWidgetService; +import com.android.internal.annotations.GuardedBy; import com.android.internal.util.Preconditions; -import android.bluetooth.BluetoothManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentProvider; @@ -34,19 +26,15 @@ import android.content.ContentResolver; import android.content.Context; import android.content.ContextWrapper; import android.content.IContentProvider; +import android.content.IIntentReceiver; import android.content.Intent; import android.content.IntentFilter; -import android.content.IIntentReceiver; import android.content.IntentSender; -import android.content.IRestrictionsManager; import android.content.ReceiverCallNotAllowedException; -import android.content.RestrictionsManager; import android.content.ServiceConnection; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; -import android.content.pm.ILauncherApps; import android.content.pm.IPackageManager; -import android.content.pm.LauncherApps; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.AssetManager; @@ -58,101 +46,28 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; -import android.hardware.ConsumerIrManager; -import android.hardware.ISerialManager; -import android.hardware.SerialManager; -import android.hardware.SystemSensorManager; -import android.hardware.hdmi.HdmiControlManager; -import android.hardware.hdmi.IHdmiControlService; -import android.hardware.camera2.CameraManager; import android.hardware.display.DisplayManager; -import android.hardware.input.InputManager; -import android.hardware.usb.IUsbManager; -import android.hardware.usb.UsbManager; -import android.location.CountryDetector; -import android.location.ICountryDetector; -import android.location.ILocationManager; -import android.location.LocationManager; -import android.media.AudioManager; -import android.media.MediaRouter; -import android.media.midi.IMidiManager; -import android.media.midi.MidiManager; -import android.media.projection.MediaProjectionManager; -import android.media.session.MediaSessionManager; -import android.media.tv.ITvInputManager; -import android.media.tv.TvInputManager; -import android.net.ConnectivityManager; -import android.net.IConnectivityManager; -import android.net.EthernetManager; -import android.net.IEthernetManager; -import android.net.INetworkPolicyManager; -import android.net.NetworkPolicyManager; -import android.net.NetworkScoreManager; import android.net.Uri; -import android.net.nsd.INsdManager; -import android.net.nsd.NsdManager; -import android.net.wifi.IWifiManager; -import android.net.wifi.WifiManager; -import android.net.wifi.passpoint.IWifiPasspointManager; -import android.net.wifi.passpoint.WifiPasspointManager; -import android.net.wifi.p2p.IWifiP2pManager; -import android.net.wifi.p2p.WifiP2pManager; -import android.net.wifi.IWifiScanner; -import android.net.wifi.WifiScanner; -import android.net.wifi.IRttManager; -import android.net.wifi.RttManager; -import android.nfc.NfcManager; -import android.os.BatteryManager; import android.os.Binder; +import android.os.Build; import android.os.Bundle; import android.os.Debug; -import android.os.DropBoxManager; import android.os.Environment; import android.os.FileUtils; import android.os.Handler; import android.os.IBinder; -import android.os.IPowerManager; -import android.os.IUserManager; import android.os.Looper; -import android.os.PowerManager; import android.os.Process; import android.os.RemoteException; import android.os.ServiceManager; import android.os.UserHandle; -import android.os.SystemVibrator; -import android.os.UserManager; import android.os.storage.IMountService; -import android.os.storage.StorageManager; -import android.print.IPrintManager; -import android.print.PrintManager; -import android.service.fingerprint.IFingerprintService; -import android.service.fingerprint.FingerprintManager; -import android.telecom.TelecomManager; -import android.telephony.SubscriptionManager; -import android.telephony.TelephonyManager; -import android.content.ClipboardManager; import android.util.AndroidRuntimeException; import android.util.ArrayMap; import android.util.Log; import android.util.Slog; -import android.view.DisplayAdjustments; -import android.view.ContextThemeWrapper; import android.view.Display; -import android.view.PhoneLayoutInflater; -import android.view.WindowManagerImpl; -import android.view.accessibility.AccessibilityManager; -import android.view.accessibility.CaptioningManager; -import android.view.inputmethod.InputMethodManager; -import android.view.textservice.TextServicesManager; -import android.accounts.AccountManager; -import android.accounts.IAccountManager; -import android.app.admin.DevicePolicyManager; -import android.app.job.IJobScheduler; -import android.app.trust.TrustManager; - -import com.android.internal.annotations.GuardedBy; -import com.android.internal.app.IAppOpsService; -import com.android.internal.os.IDropBoxManagerService; +import android.view.DisplayAdjustments; import java.io.File; import java.io.FileInputStream; @@ -160,8 +75,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; class ReceiverRestrictedContext extends ContextWrapper { ReceiverRestrictedContext(Context base) { @@ -270,521 +183,8 @@ class ContextImpl extends Context { private static final String[] EMPTY_FILE_LIST = {}; - /** - * Override this class when the system service constructor needs a - * ContextImpl. Else, use StaticServiceFetcher below. - */ - /*package*/ static class ServiceFetcher { - int mContextCacheIndex = -1; - - /** - * Main entrypoint; only override if you don't need caching. - */ - public Object getService(ContextImpl ctx) { - ArrayList<Object> cache = ctx.mServiceCache; - Object service; - synchronized (cache) { - if (cache.size() == 0) { - // Initialize the cache vector on first access. - // At this point sNextPerContextServiceCacheIndex - // is the number of potential services that are - // cached per-Context. - for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) { - cache.add(null); - } - } else { - service = cache.get(mContextCacheIndex); - if (service != null) { - return service; - } - } - service = createService(ctx); - cache.set(mContextCacheIndex, service); - return service; - } - } - - /** - * Override this to create a new per-Context instance of the - * service. getService() will handle locking and caching. - */ - public Object createService(ContextImpl ctx) { - throw new RuntimeException("Not implemented"); - } - } - - /** - * Override this class for services to be cached process-wide. - */ - abstract static class StaticServiceFetcher extends ServiceFetcher { - private Object mCachedInstance; - - @Override - public final Object getService(ContextImpl unused) { - synchronized (StaticServiceFetcher.this) { - Object service = mCachedInstance; - if (service != null) { - return service; - } - return mCachedInstance = createStaticService(); - } - } - - public abstract Object createStaticService(); - } - - private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP = - new HashMap<String, ServiceFetcher>(); - - private static int sNextPerContextServiceCacheIndex = 0; - private static void registerService(String serviceName, ServiceFetcher fetcher) { - if (!(fetcher instanceof StaticServiceFetcher)) { - fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++; - } - SYSTEM_SERVICE_MAP.put(serviceName, fetcher); - } - - // This one's defined separately and given a variable name so it - // can be re-used by getWallpaperManager(), avoiding a HashMap - // lookup. - private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new WallpaperManager(ctx.getOuterContext(), - ctx.mMainThread.getHandler()); - }}; - - static { - registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() { - public Object getService(ContextImpl ctx) { - return AccessibilityManager.getInstance(ctx); - }}); - - registerService(CAPTIONING_SERVICE, new ServiceFetcher() { - public Object getService(ContextImpl ctx) { - return new CaptioningManager(ctx); - }}); - - registerService(ACCOUNT_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(ACCOUNT_SERVICE); - IAccountManager service = IAccountManager.Stub.asInterface(b); - return new AccountManager(ctx, service); - }}); - - registerService(ACTIVITY_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); - }}); - - registerService(ALARM_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(ALARM_SERVICE); - IAlarmManager service = IAlarmManager.Stub.asInterface(b); - return new AlarmManager(service, ctx); - }}); - - registerService(AUDIO_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new AudioManager(ctx); - }}); - - registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new MediaRouter(ctx); - }}); - - registerService(BLUETOOTH_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new BluetoothManager(ctx); - }}); - - registerService(HDMI_CONTROL_SERVICE, new StaticServiceFetcher() { - public Object createStaticService() { - IBinder b = ServiceManager.getService(HDMI_CONTROL_SERVICE); - return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b)); - }}); - - registerService(CLIPBOARD_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new ClipboardManager(ctx.getOuterContext(), - ctx.mMainThread.getHandler()); - }}); - - registerService(CONNECTIVITY_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE); - return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b)); - }}); - - registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() { - public Object createStaticService() { - IBinder b = ServiceManager.getService(COUNTRY_DETECTOR); - return new CountryDetector(ICountryDetector.Stub.asInterface(b)); - }}); - - registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler()); - }}); - - registerService(DOWNLOAD_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName()); - }}); - - registerService(BATTERY_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new BatteryManager(); - }}); - - registerService(NFC_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new NfcManager(ctx); - }}); - - registerService(DROPBOX_SERVICE, new StaticServiceFetcher() { - public Object createStaticService() { - return createDropBoxManager(); - }}); - - registerService(INPUT_SERVICE, new StaticServiceFetcher() { - public Object createStaticService() { - return InputManager.getInstance(); - }}); - - registerService(DISPLAY_SERVICE, new ServiceFetcher() { - @Override - public Object createService(ContextImpl ctx) { - return new DisplayManager(ctx.getOuterContext()); - }}); - - registerService(INPUT_METHOD_SERVICE, new StaticServiceFetcher() { - public Object createStaticService() { - return InputMethodManager.getInstance(); - }}); - - registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return TextServicesManager.getInstance(); - }}); - - registerService(KEYGUARD_SERVICE, new ServiceFetcher() { - public Object getService(ContextImpl ctx) { - // TODO: why isn't this caching it? It wasn't - // before, so I'm preserving the old behavior and - // using getService(), instead of createService() - // which would do the caching. - return new KeyguardManager(); - }}); - - registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new PhoneLayoutInflater(ctx.getOuterContext()); - }}); - - registerService(LOCATION_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(LOCATION_SERVICE); - return new LocationManager(ctx, ILocationManager.Stub.asInterface(b)); - }}); - - registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() { - @Override - public Object createService(ContextImpl ctx) { - return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface( - ServiceManager.getService(NETWORK_POLICY_SERVICE))); - } - }); - - registerService(NOTIFICATION_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - final Context outerContext = ctx.getOuterContext(); - return new NotificationManager( - new ContextThemeWrapper(outerContext, - Resources.selectSystemTheme(0, - outerContext.getApplicationInfo().targetSdkVersion, - com.android.internal.R.style.Theme_Dialog, - com.android.internal.R.style.Theme_Holo_Dialog, - com.android.internal.R.style.Theme_DeviceDefault_Dialog, - com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)), - ctx.mMainThread.getHandler()); - }}); - - registerService(NSD_SERVICE, new ServiceFetcher() { - @Override - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(NSD_SERVICE); - INsdManager service = INsdManager.Stub.asInterface(b); - return new NsdManager(ctx.getOuterContext(), service); - }}); - - // Note: this was previously cached in a static variable, but - // constructed using mMainThread.getHandler(), so converting - // it to be a regular Context-cached service... - registerService(POWER_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(POWER_SERVICE); - IPowerManager service = IPowerManager.Stub.asInterface(b); - if (service == null) { - Log.wtf(TAG, "Failed to get power manager service."); - } - return new PowerManager(ctx.getOuterContext(), - service, ctx.mMainThread.getHandler()); - }}); - - registerService(SEARCH_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new SearchManager(ctx.getOuterContext(), - ctx.mMainThread.getHandler()); - }}); - - registerService(SENSOR_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new SystemSensorManager(ctx.getOuterContext(), - ctx.mMainThread.getHandler().getLooper()); - }}); - - registerService(STATUS_BAR_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new StatusBarManager(ctx.getOuterContext()); - }}); - - registerService(STORAGE_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - try { - return new StorageManager( - ctx.getContentResolver(), ctx.mMainThread.getHandler().getLooper()); - } catch (RemoteException rex) { - Log.e(TAG, "Failed to create StorageManager", rex); - return null; - } - }}); - - registerService(TELEPHONY_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new TelephonyManager(ctx.getOuterContext()); - }}); - - registerService(TELEPHONY_SUBSCRIPTION_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new SubscriptionManager(ctx.getOuterContext()); - }}); - - registerService(TELECOM_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new TelecomManager(ctx.getOuterContext()); - }}); - - registerService(UI_MODE_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new UiModeManager(); - }}); - - registerService(USB_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(USB_SERVICE); - return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); - }}); - - registerService(SERIAL_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(SERIAL_SERVICE); - return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); - }}); - - registerService(VIBRATOR_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new SystemVibrator(ctx); - }}); - - registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER); - - registerService(WIFI_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(WIFI_SERVICE); - IWifiManager service = IWifiManager.Stub.asInterface(b); - return new WifiManager(ctx.getOuterContext(), service); - }}); - - registerService(WIFI_PASSPOINT_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(WIFI_PASSPOINT_SERVICE); - IWifiPasspointManager service = IWifiPasspointManager.Stub.asInterface(b); - return new WifiPasspointManager(ctx.getOuterContext(), service); - }}); - - registerService(WIFI_P2P_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE); - IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b); - return new WifiP2pManager(service); - }}); - - registerService(WIFI_SCANNING_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(WIFI_SCANNING_SERVICE); - IWifiScanner service = IWifiScanner.Stub.asInterface(b); - return new WifiScanner(ctx.getOuterContext(), service); - }}); - - registerService(WIFI_RTT_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(WIFI_RTT_SERVICE); - IRttManager service = IRttManager.Stub.asInterface(b); - return new RttManager(ctx.getOuterContext(), service); - }}); - - registerService(ETHERNET_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(ETHERNET_SERVICE); - IEthernetManager service = IEthernetManager.Stub.asInterface(b); - return new EthernetManager(ctx.getOuterContext(), service); - }}); - - registerService(WINDOW_SERVICE, new ServiceFetcher() { - Display mDefaultDisplay; - public Object getService(ContextImpl ctx) { - Display display = ctx.mDisplay; - if (display == null) { - if (mDefaultDisplay == null) { - DisplayManager dm = (DisplayManager)ctx.getOuterContext(). - getSystemService(Context.DISPLAY_SERVICE); - mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY); - } - display = mDefaultDisplay; - } - return new WindowManagerImpl(display); - }}); - - registerService(USER_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(USER_SERVICE); - IUserManager service = IUserManager.Stub.asInterface(b); - return new UserManager(ctx, service); - }}); - - registerService(APP_OPS_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(APP_OPS_SERVICE); - IAppOpsService service = IAppOpsService.Stub.asInterface(b); - return new AppOpsManager(ctx, service); - }}); - - registerService(CAMERA_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new CameraManager(ctx); - } - }); - - registerService(LAUNCHER_APPS_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(LAUNCHER_APPS_SERVICE); - ILauncherApps service = ILauncherApps.Stub.asInterface(b); - return new LauncherApps(ctx, service); - } - }); - - registerService(RESTRICTIONS_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(RESTRICTIONS_SERVICE); - IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b); - return new RestrictionsManager(ctx, service); - } - }); - registerService(PRINT_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE); - IPrintManager service = IPrintManager.Stub.asInterface(iBinder); - return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(), - UserHandle.getAppId(Process.myUid())); - }}); - - registerService(CONSUMER_IR_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new ConsumerIrManager(ctx); - }}); - - registerService(MEDIA_SESSION_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new MediaSessionManager(ctx); - } - }); - - registerService(TRUST_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(TRUST_SERVICE); - return new TrustManager(b); - } - }); - - registerService(FINGERPRINT_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder binder = ServiceManager.getService(FINGERPRINT_SERVICE); - IFingerprintService service = IFingerprintService.Stub.asInterface(binder); - return new FingerprintManager(ctx.getOuterContext(), service); - } - }); - - registerService(TV_INPUT_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder iBinder = ServiceManager.getService(TV_INPUT_SERVICE); - ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder); - return new TvInputManager(service, UserHandle.myUserId()); - } - }); - - registerService(NETWORK_SCORE_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new NetworkScoreManager(ctx); - } - }); - - registerService(USAGE_STATS_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder iBinder = ServiceManager.getService(USAGE_STATS_SERVICE); - IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder); - return new UsageStatsManager(ctx.getOuterContext(), service); - } - }); - - registerService(JOB_SCHEDULER_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(JOB_SCHEDULER_SERVICE); - return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b)); - }}); - - registerService(PERSISTENT_DATA_BLOCK_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(PERSISTENT_DATA_BLOCK_SERVICE); - IPersistentDataBlockService persistentDataBlockService = - IPersistentDataBlockService.Stub.asInterface(b); - if (persistentDataBlockService != null) { - return new PersistentDataBlockManager(persistentDataBlockService); - } else { - // not supported - return null; - } - } - }); - - registerService(MEDIA_PROJECTION_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - return new MediaProjectionManager(ctx); - } - }); - - registerService(APPWIDGET_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(APPWIDGET_SERVICE); - return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b)); - }}); - - registerService(MIDI_SERVICE, new ServiceFetcher() { - public Object createService(ContextImpl ctx) { - IBinder b = ServiceManager.getService(MIDI_SERVICE); - return new MidiManager(ctx, IMidiManager.Stub.asInterface(b)); - }}); - } + // The system service cache for the system services that are cached per-ContextImpl. + final Object[] mServiceCache = SystemServiceRegistry.createServiceCache(); static ContextImpl getImpl(Context context) { Context nextContext; @@ -795,11 +195,6 @@ class ContextImpl extends Context { return (ContextImpl)context; } - // The system service cache for the system services that are - // cached per-ContextImpl. Package-scoped to avoid accessor - // methods. - final ArrayList<Object> mServiceCache = new ArrayList<Object>(); - @Override public AssetManager getAssets() { return getResources().getAssets(); @@ -914,6 +309,7 @@ class ContextImpl extends Context { throw new RuntimeException("Not supported in system context"); } + @Override public File getSharedPrefsFile(String name) { return makeFilename(getPreferencesDir(), name + ".xml"); } @@ -1201,40 +597,51 @@ class ContextImpl extends Context { } @Override + @Deprecated public Drawable getWallpaper() { return getWallpaperManager().getDrawable(); } @Override + @Deprecated public Drawable peekWallpaper() { return getWallpaperManager().peekDrawable(); } @Override + @Deprecated public int getWallpaperDesiredMinimumWidth() { return getWallpaperManager().getDesiredMinimumWidth(); } @Override + @Deprecated public int getWallpaperDesiredMinimumHeight() { return getWallpaperManager().getDesiredMinimumHeight(); } @Override - public void setWallpaper(Bitmap bitmap) throws IOException { + @Deprecated + public void setWallpaper(Bitmap bitmap) throws IOException { getWallpaperManager().setBitmap(bitmap); } @Override + @Deprecated public void setWallpaper(InputStream data) throws IOException { getWallpaperManager().setStream(data); } @Override + @Deprecated public void clearWallpaper() throws IOException { getWallpaperManager().clear(); } + private WallpaperManager getWallpaperManager() { + return getSystemService(WallpaperManager.class); + } + @Override public void startActivity(Intent intent) { warnIfCallingFromSystemProcess(); @@ -1506,6 +913,7 @@ class ContextImpl extends Context { } @Override + @Deprecated public void sendStickyBroadcast(Intent intent) { warnIfCallingFromSystemProcess(); String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); @@ -1520,6 +928,7 @@ class ContextImpl extends Context { } @Override + @Deprecated public void sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, @@ -1554,6 +963,7 @@ class ContextImpl extends Context { } @Override + @Deprecated public void removeStickyBroadcast(Intent intent) { String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); if (resolvedType != null) { @@ -1569,6 +979,7 @@ class ContextImpl extends Context { } @Override + @Deprecated public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); try { @@ -1581,6 +992,7 @@ class ContextImpl extends Context { } @Override + @Deprecated public void sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, @@ -1614,6 +1026,7 @@ class ContextImpl extends Context { } @Override + @Deprecated public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); if (resolvedType != null) { @@ -1850,25 +1263,12 @@ class ContextImpl extends Context { @Override public Object getSystemService(String name) { - ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name); - return fetcher == null ? null : fetcher.getService(this); - } - - private WallpaperManager getWallpaperManager() { - return (WallpaperManager) WALLPAPER_FETCHER.getService(this); + return SystemServiceRegistry.getSystemService(this, name); } - /* package */ static DropBoxManager createDropBoxManager() { - IBinder b = ServiceManager.getService(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(service); + @Override + public String getSystemServiceName(Class<?> serviceClass) { + return SystemServiceRegistry.getSystemServiceName(serviceClass); } @Override @@ -1937,6 +1337,7 @@ class ContextImpl extends Context { } } + @Override public void enforcePermission( String permission, int pid, int uid, String message) { enforce(permission, @@ -1946,6 +1347,7 @@ class ContextImpl extends Context { message); } + @Override public void enforceCallingPermission(String permission, String message) { enforce(permission, checkCallingPermission(permission), @@ -1954,6 +1356,7 @@ class ContextImpl extends Context { message); } + @Override public void enforceCallingOrSelfPermission( String permission, String message) { enforce(permission, @@ -2091,6 +1494,7 @@ class ContextImpl extends Context { } } + @Override public void enforceUriPermission( Uri uri, int pid, int uid, int modeFlags, String message) { enforceForUri( @@ -2098,6 +1502,7 @@ class ContextImpl extends Context { false, uid, uri, message); } + @Override public void enforceCallingUriPermission( Uri uri, int modeFlags, String message) { enforceForUri( @@ -2106,6 +1511,7 @@ class ContextImpl extends Context { Binder.getCallingUid(), uri, message); } + @Override public void enforceCallingOrSelfUriPermission( Uri uri, int modeFlags, String message) { enforceForUri( @@ -2114,6 +1520,7 @@ class ContextImpl extends Context { Binder.getCallingUid(), uri, message); } + @Override public void enforceUriPermission( Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags, String message) { @@ -2209,6 +1616,14 @@ class ContextImpl extends Context { mUser, mRestricted, display, null); } + Display getDisplay() { + if (mDisplay != null) { + return mDisplay; + } + DisplayManager dm = getSystemService(DisplayManager.class); + return dm.getDisplay(Display.DEFAULT_DISPLAY); + } + private int getDisplayId() { return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY; } @@ -2243,6 +1658,7 @@ class ContextImpl extends Context { } /** {@hide} */ + @Override public int getUserId() { return mUser.getIdentifier(); } @@ -2368,6 +1784,7 @@ class ContextImpl extends Context { return mActivityToken; } + @SuppressWarnings("deprecation") static void setFilePermissionsFromMode(String name, int mode, int extraPermissions) { int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java new file mode 100644 index 0000000..993f416 --- /dev/null +++ b/core/java/android/app/SystemServiceRegistry.java @@ -0,0 +1,776 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.app; + +import com.android.internal.app.IAppOpsService; +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.job.IJobScheduler; +import android.app.job.JobScheduler; +import android.app.trust.TrustManager; +import android.app.usage.IUsageStatsManager; +import android.app.usage.UsageStatsManager; +import android.appwidget.AppWidgetManager; +import android.bluetooth.BluetoothManager; +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.LauncherApps; +import android.content.res.Resources; +import android.hardware.ConsumerIrManager; +import android.hardware.ISerialManager; +import android.hardware.SensorManager; +import android.hardware.SerialManager; +import android.hardware.SystemSensorManager; +import android.hardware.camera2.CameraManager; +import android.hardware.display.DisplayManager; +import android.hardware.hdmi.HdmiControlManager; +import android.hardware.hdmi.IHdmiControlService; +import android.hardware.input.InputManager; +import android.hardware.usb.IUsbManager; +import android.hardware.usb.UsbManager; +import android.location.CountryDetector; +import android.location.ICountryDetector; +import android.location.ILocationManager; +import android.location.LocationManager; +import android.media.AudioManager; +import android.media.MediaRouter; +import android.media.midi.IMidiManager; +import android.media.midi.MidiManager; +import android.media.projection.MediaProjectionManager; +import android.media.session.MediaSessionManager; +import android.media.tv.ITvInputManager; +import android.media.tv.TvInputManager; +import android.net.ConnectivityManager; +import android.net.EthernetManager; +import android.net.IConnectivityManager; +import android.net.IEthernetManager; +import android.net.INetworkPolicyManager; +import android.net.NetworkPolicyManager; +import android.net.NetworkScoreManager; +import android.net.nsd.INsdManager; +import android.net.nsd.NsdManager; +import android.net.wifi.IRttManager; +import android.net.wifi.IWifiManager; +import android.net.wifi.IWifiScanner; +import android.net.wifi.RttManager; +import android.net.wifi.WifiManager; +import android.net.wifi.WifiScanner; +import android.net.wifi.p2p.IWifiP2pManager; +import android.net.wifi.p2p.WifiP2pManager; +import android.net.wifi.passpoint.IWifiPasspointManager; +import android.net.wifi.passpoint.WifiPasspointManager; +import android.nfc.NfcManager; +import android.os.BatteryManager; +import android.os.DropBoxManager; +import android.os.IBinder; +import android.os.IPowerManager; +import android.os.IUserManager; +import android.os.PowerManager; +import android.os.Process; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.os.SystemVibrator; +import android.os.UserHandle; +import android.os.UserManager; +import android.os.Vibrator; +import android.os.storage.StorageManager; +import android.print.IPrintManager; +import android.print.PrintManager; +import android.service.fingerprint.FingerprintManager; +import android.service.fingerprint.IFingerprintService; +import android.service.persistentdata.IPersistentDataBlockService; +import android.service.persistentdata.PersistentDataBlockManager; +import android.telecom.TelecomManager; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; +import android.util.Log; +import android.view.ContextThemeWrapper; +import android.view.LayoutInflater; +import android.view.PhoneLayoutInflater; +import android.view.WindowManager; +import android.view.WindowManagerImpl; +import android.view.accessibility.AccessibilityManager; +import android.view.accessibility.CaptioningManager; +import android.view.inputmethod.InputMethodManager; +import android.view.textservice.TextServicesManager; + +import java.util.HashMap; + +/** + * Manages all of the system services that can be returned by {@link Context#getSystemService}. + * Used by {@link ContextImpl}. + */ +final class SystemServiceRegistry { + private final static String TAG = "SystemServiceRegistry"; + + // Service registry information. + // This information is never changed once static initialization has completed. + private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES = + new HashMap<Class<?>, String>(); + private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS = + new HashMap<String, ServiceFetcher<?>>(); + private static int sServiceCacheSize; + + // Not instantiable. + private SystemServiceRegistry() { } + + static { + registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class, + new CachedServiceFetcher<AccessibilityManager>() { + @Override + public AccessibilityManager createService(ContextImpl ctx) { + return AccessibilityManager.getInstance(ctx); + }}); + + registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class, + new CachedServiceFetcher<CaptioningManager>() { + @Override + public CaptioningManager createService(ContextImpl ctx) { + return new CaptioningManager(ctx); + }}); + + registerService(Context.ACCOUNT_SERVICE, AccountManager.class, + new CachedServiceFetcher<AccountManager>() { + @Override + public AccountManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE); + IAccountManager service = IAccountManager.Stub.asInterface(b); + return new AccountManager(ctx, service); + }}); + + registerService(Context.ACTIVITY_SERVICE, ActivityManager.class, + new CachedServiceFetcher<ActivityManager>() { + @Override + public ActivityManager createService(ContextImpl ctx) { + return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); + }}); + + registerService(Context.ALARM_SERVICE, AlarmManager.class, + new CachedServiceFetcher<AlarmManager>() { + @Override + public AlarmManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.ALARM_SERVICE); + IAlarmManager service = IAlarmManager.Stub.asInterface(b); + return new AlarmManager(service, ctx); + }}); + + registerService(Context.AUDIO_SERVICE, AudioManager.class, + new CachedServiceFetcher<AudioManager>() { + @Override + public AudioManager createService(ContextImpl ctx) { + return new AudioManager(ctx); + }}); + + registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class, + new CachedServiceFetcher<MediaRouter>() { + @Override + public MediaRouter createService(ContextImpl ctx) { + return new MediaRouter(ctx); + }}); + + registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class, + new CachedServiceFetcher<BluetoothManager>() { + @Override + public BluetoothManager createService(ContextImpl ctx) { + return new BluetoothManager(ctx); + }}); + + registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class, + new StaticServiceFetcher<HdmiControlManager>() { + @Override + public HdmiControlManager createService() { + IBinder b = ServiceManager.getService(Context.HDMI_CONTROL_SERVICE); + return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b)); + }}); + + registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class, + new CachedServiceFetcher<ClipboardManager>() { + @Override + public ClipboardManager createService(ContextImpl ctx) { + return new ClipboardManager(ctx.getOuterContext(), + ctx.mMainThread.getHandler()); + }}); + + // The clipboard service moved to a new package. If someone asks for the old + // interface by class then we want to redirect over to the new interface instead + // (which extends it). + SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE); + + registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class, + new StaticServiceFetcher<ConnectivityManager>() { + @Override + public ConnectivityManager createService() { + IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); + return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b)); + }}); + + registerService(Context.COUNTRY_DETECTOR, CountryDetector.class, + new StaticServiceFetcher<CountryDetector>() { + @Override + public CountryDetector createService() { + IBinder b = ServiceManager.getService(Context.COUNTRY_DETECTOR); + return new CountryDetector(ICountryDetector.Stub.asInterface(b)); + }}); + + registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class, + new CachedServiceFetcher<DevicePolicyManager>() { + @Override + public DevicePolicyManager createService(ContextImpl ctx) { + return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler()); + }}); + + registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class, + new CachedServiceFetcher<DownloadManager>() { + @Override + public DownloadManager createService(ContextImpl ctx) { + return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName()); + }}); + + registerService(Context.BATTERY_SERVICE, BatteryManager.class, + new StaticServiceFetcher<BatteryManager>() { + @Override + public BatteryManager createService() { + return new BatteryManager(); + }}); + + registerService(Context.NFC_SERVICE, NfcManager.class, + new CachedServiceFetcher<NfcManager>() { + @Override + public NfcManager createService(ContextImpl ctx) { + return new NfcManager(ctx); + }}); + + registerService(Context.DROPBOX_SERVICE, DropBoxManager.class, + new StaticServiceFetcher<DropBoxManager>() { + @Override + public DropBoxManager createService() { + IBinder b = ServiceManager.getService(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(service); + }}); + + registerService(Context.INPUT_SERVICE, InputManager.class, + new StaticServiceFetcher<InputManager>() { + @Override + public InputManager createService() { + return InputManager.getInstance(); + }}); + + registerService(Context.DISPLAY_SERVICE, DisplayManager.class, + new CachedServiceFetcher<DisplayManager>() { + @Override + public DisplayManager createService(ContextImpl ctx) { + return new DisplayManager(ctx.getOuterContext()); + }}); + + registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class, + new StaticServiceFetcher<InputMethodManager>() { + @Override + public InputMethodManager createService() { + return InputMethodManager.getInstance(); + }}); + + registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class, + new StaticServiceFetcher<TextServicesManager>() { + @Override + public TextServicesManager createService() { + return TextServicesManager.getInstance(); + }}); + + registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class, + new StaticServiceFetcher<KeyguardManager>() { + @Override + public KeyguardManager createService() { + return new KeyguardManager(); + }}); + + registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class, + new CachedServiceFetcher<LayoutInflater>() { + @Override + public LayoutInflater createService(ContextImpl ctx) { + return new PhoneLayoutInflater(ctx.getOuterContext()); + }}); + + registerService(Context.LOCATION_SERVICE, LocationManager.class, + new CachedServiceFetcher<LocationManager>() { + @Override + public LocationManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE); + return new LocationManager(ctx, ILocationManager.Stub.asInterface(b)); + }}); + + registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class, + new StaticServiceFetcher<NetworkPolicyManager>() { + @Override + public NetworkPolicyManager createService() { + return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface( + ServiceManager.getService(Context.NETWORK_POLICY_SERVICE))); + }}); + + registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class, + new CachedServiceFetcher<NotificationManager>() { + @Override + public NotificationManager createService(ContextImpl ctx) { + final Context outerContext = ctx.getOuterContext(); + return new NotificationManager( + new ContextThemeWrapper(outerContext, + Resources.selectSystemTheme(0, + outerContext.getApplicationInfo().targetSdkVersion, + com.android.internal.R.style.Theme_Dialog, + com.android.internal.R.style.Theme_Holo_Dialog, + com.android.internal.R.style.Theme_DeviceDefault_Dialog, + com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)), + ctx.mMainThread.getHandler()); + }}); + + registerService(Context.NSD_SERVICE, NsdManager.class, + new CachedServiceFetcher<NsdManager>() { + @Override + public NsdManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.NSD_SERVICE); + INsdManager service = INsdManager.Stub.asInterface(b); + return new NsdManager(ctx.getOuterContext(), service); + }}); + + registerService(Context.POWER_SERVICE, PowerManager.class, + new CachedServiceFetcher<PowerManager>() { + @Override + public PowerManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.POWER_SERVICE); + IPowerManager service = IPowerManager.Stub.asInterface(b); + if (service == null) { + Log.wtf(TAG, "Failed to get power manager service."); + } + return new PowerManager(ctx.getOuterContext(), + service, ctx.mMainThread.getHandler()); + }}); + + registerService(Context.SEARCH_SERVICE, SearchManager.class, + new CachedServiceFetcher<SearchManager>() { + @Override + public SearchManager createService(ContextImpl ctx) { + return new SearchManager(ctx.getOuterContext(), + ctx.mMainThread.getHandler()); + }}); + + registerService(Context.SENSOR_SERVICE, SensorManager.class, + new CachedServiceFetcher<SensorManager>() { + @Override + public SensorManager createService(ContextImpl ctx) { + return new SystemSensorManager(ctx.getOuterContext(), + ctx.mMainThread.getHandler().getLooper()); + }}); + + registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class, + new CachedServiceFetcher<StatusBarManager>() { + @Override + public StatusBarManager createService(ContextImpl ctx) { + return new StatusBarManager(ctx.getOuterContext()); + }}); + + registerService(Context.STORAGE_SERVICE, StorageManager.class, + new CachedServiceFetcher<StorageManager>() { + @Override + public StorageManager createService(ContextImpl ctx) { + try { + return new StorageManager( + ctx.getContentResolver(), ctx.mMainThread.getHandler().getLooper()); + } catch (RemoteException rex) { + Log.e(TAG, "Failed to create StorageManager", rex); + return null; + } + }}); + + registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class, + new CachedServiceFetcher<TelephonyManager>() { + @Override + public TelephonyManager createService(ContextImpl ctx) { + return new TelephonyManager(ctx.getOuterContext()); + }}); + + registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class, + new CachedServiceFetcher<SubscriptionManager>() { + @Override + public SubscriptionManager createService(ContextImpl ctx) { + return new SubscriptionManager(ctx.getOuterContext()); + }}); + + registerService(Context.TELECOM_SERVICE, TelecomManager.class, + new CachedServiceFetcher<TelecomManager>() { + @Override + public TelecomManager createService(ContextImpl ctx) { + return new TelecomManager(ctx.getOuterContext()); + }}); + + registerService(Context.UI_MODE_SERVICE, UiModeManager.class, + new CachedServiceFetcher<UiModeManager>() { + @Override + public UiModeManager createService(ContextImpl ctx) { + return new UiModeManager(); + }}); + + registerService(Context.USB_SERVICE, UsbManager.class, + new CachedServiceFetcher<UsbManager>() { + @Override + public UsbManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.USB_SERVICE); + return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); + }}); + + registerService(Context.SERIAL_SERVICE, SerialManager.class, + new CachedServiceFetcher<SerialManager>() { + @Override + public SerialManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.SERIAL_SERVICE); + return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); + }}); + + registerService(Context.VIBRATOR_SERVICE, Vibrator.class, + new CachedServiceFetcher<Vibrator>() { + @Override + public Vibrator createService(ContextImpl ctx) { + return new SystemVibrator(ctx); + }}); + + registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class, + new CachedServiceFetcher<WallpaperManager>() { + @Override + public WallpaperManager createService(ContextImpl ctx) { + return new WallpaperManager(ctx.getOuterContext(), + ctx.mMainThread.getHandler()); + }}); + + registerService(Context.WIFI_SERVICE, WifiManager.class, + new CachedServiceFetcher<WifiManager>() { + @Override + public WifiManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.WIFI_SERVICE); + IWifiManager service = IWifiManager.Stub.asInterface(b); + return new WifiManager(ctx.getOuterContext(), service); + }}); + + registerService(Context.WIFI_PASSPOINT_SERVICE, WifiPasspointManager.class, + new CachedServiceFetcher<WifiPasspointManager>() { + @Override + public WifiPasspointManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.WIFI_PASSPOINT_SERVICE); + IWifiPasspointManager service = IWifiPasspointManager.Stub.asInterface(b); + return new WifiPasspointManager(ctx.getOuterContext(), service); + }}); + + registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class, + new StaticServiceFetcher<WifiP2pManager>() { + @Override + public WifiP2pManager createService() { + IBinder b = ServiceManager.getService(Context.WIFI_P2P_SERVICE); + IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b); + return new WifiP2pManager(service); + }}); + + registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class, + new CachedServiceFetcher<WifiScanner>() { + @Override + public WifiScanner createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.WIFI_SCANNING_SERVICE); + IWifiScanner service = IWifiScanner.Stub.asInterface(b); + return new WifiScanner(ctx.getOuterContext(), service); + }}); + + registerService(Context.WIFI_RTT_SERVICE, RttManager.class, + new CachedServiceFetcher<RttManager>() { + @Override + public RttManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.WIFI_RTT_SERVICE); + IRttManager service = IRttManager.Stub.asInterface(b); + return new RttManager(ctx.getOuterContext(), service); + }}); + + registerService(Context.ETHERNET_SERVICE, EthernetManager.class, + new CachedServiceFetcher<EthernetManager>() { + @Override + public EthernetManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.ETHERNET_SERVICE); + IEthernetManager service = IEthernetManager.Stub.asInterface(b); + return new EthernetManager(ctx.getOuterContext(), service); + }}); + + registerService(Context.WINDOW_SERVICE, WindowManager.class, + new CachedServiceFetcher<WindowManager>() { + @Override + public WindowManager createService(ContextImpl ctx) { + return new WindowManagerImpl(ctx.getDisplay()); + }}); + + registerService(Context.USER_SERVICE, UserManager.class, + new CachedServiceFetcher<UserManager>() { + @Override + public UserManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.USER_SERVICE); + IUserManager service = IUserManager.Stub.asInterface(b); + return new UserManager(ctx, service); + }}); + + registerService(Context.APP_OPS_SERVICE, AppOpsManager.class, + new CachedServiceFetcher<AppOpsManager>() { + @Override + public AppOpsManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE); + IAppOpsService service = IAppOpsService.Stub.asInterface(b); + return new AppOpsManager(ctx, service); + }}); + + registerService(Context.CAMERA_SERVICE, CameraManager.class, + new CachedServiceFetcher<CameraManager>() { + @Override + public CameraManager createService(ContextImpl ctx) { + return new CameraManager(ctx); + }}); + + registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class, + new CachedServiceFetcher<LauncherApps>() { + @Override + public LauncherApps createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.LAUNCHER_APPS_SERVICE); + ILauncherApps service = ILauncherApps.Stub.asInterface(b); + return new LauncherApps(ctx, service); + }}); + + registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class, + new CachedServiceFetcher<RestrictionsManager>() { + @Override + public RestrictionsManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.RESTRICTIONS_SERVICE); + IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b); + return new RestrictionsManager(ctx, service); + }}); + + registerService(Context.PRINT_SERVICE, PrintManager.class, + new CachedServiceFetcher<PrintManager>() { + @Override + public PrintManager createService(ContextImpl ctx) { + IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE); + IPrintManager service = IPrintManager.Stub.asInterface(iBinder); + return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(), + UserHandle.getAppId(Process.myUid())); + }}); + + registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class, + new CachedServiceFetcher<ConsumerIrManager>() { + @Override + public ConsumerIrManager createService(ContextImpl ctx) { + return new ConsumerIrManager(ctx); + }}); + + registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class, + new CachedServiceFetcher<MediaSessionManager>() { + @Override + public MediaSessionManager createService(ContextImpl ctx) { + return new MediaSessionManager(ctx); + }}); + + registerService(Context.TRUST_SERVICE, TrustManager.class, + new StaticServiceFetcher<TrustManager>() { + @Override + public TrustManager createService() { + IBinder b = ServiceManager.getService(Context.TRUST_SERVICE); + return new TrustManager(b); + }}); + + registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class, + new CachedServiceFetcher<FingerprintManager>() { + @Override + public FingerprintManager createService(ContextImpl ctx) { + IBinder binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE); + IFingerprintService service = IFingerprintService.Stub.asInterface(binder); + return new FingerprintManager(ctx.getOuterContext(), service); + }}); + + registerService(Context.TV_INPUT_SERVICE, TvInputManager.class, + new StaticServiceFetcher<TvInputManager>() { + @Override + public TvInputManager createService() { + IBinder iBinder = ServiceManager.getService(Context.TV_INPUT_SERVICE); + ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder); + return new TvInputManager(service, UserHandle.myUserId()); + }}); + + registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class, + new CachedServiceFetcher<NetworkScoreManager>() { + @Override + public NetworkScoreManager createService(ContextImpl ctx) { + return new NetworkScoreManager(ctx); + }}); + + registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class, + new CachedServiceFetcher<UsageStatsManager>() { + @Override + public UsageStatsManager createService(ContextImpl ctx) { + IBinder iBinder = ServiceManager.getService(Context.USAGE_STATS_SERVICE); + IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder); + return new UsageStatsManager(ctx.getOuterContext(), service); + }}); + + registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class, + new StaticServiceFetcher<JobScheduler>() { + @Override + public JobScheduler createService() { + IBinder b = ServiceManager.getService(Context.JOB_SCHEDULER_SERVICE); + return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b)); + }}); + + registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class, + new StaticServiceFetcher<PersistentDataBlockManager>() { + @Override + public PersistentDataBlockManager createService() { + IBinder b = ServiceManager.getService(Context.PERSISTENT_DATA_BLOCK_SERVICE); + IPersistentDataBlockService persistentDataBlockService = + IPersistentDataBlockService.Stub.asInterface(b); + if (persistentDataBlockService != null) { + return new PersistentDataBlockManager(persistentDataBlockService); + } else { + // not supported + return null; + } + }}); + + registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class, + new CachedServiceFetcher<MediaProjectionManager>() { + @Override + public MediaProjectionManager createService(ContextImpl ctx) { + return new MediaProjectionManager(ctx); + }}); + + registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class, + new CachedServiceFetcher<AppWidgetManager>() { + @Override + public AppWidgetManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE); + return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b)); + }}); + + registerService(Context.MIDI_SERVICE, MidiManager.class, + new CachedServiceFetcher<MidiManager>() { + @Override + public MidiManager createService(ContextImpl ctx) { + IBinder b = ServiceManager.getService(Context.MIDI_SERVICE); + return new MidiManager(ctx, IMidiManager.Stub.asInterface(b)); + }}); + } + + /** + * Creates an array which is used to cache per-Context service instances. + */ + public static Object[] createServiceCache() { + return new Object[sServiceCacheSize]; + } + + /** + * Gets a system service from a given context. + */ + public static Object getSystemService(ContextImpl ctx, String name) { + ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name); + return fetcher != null ? fetcher.getService(ctx) : null; + } + + /** + * Gets the name of the system-level service that is represented by the specified class. + */ + public static String getSystemServiceName(Class<?> serviceClass) { + return SYSTEM_SERVICE_NAMES.get(serviceClass); + } + + /** + * Statically registers a system service with the context. + * This method must be called during static initialization only. + */ + private static <T> void registerService(String serviceName, Class<T> serviceClass, + ServiceFetcher<T> serviceFetcher) { + SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName); + SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher); + } + + /** + * Base interface for classes that fetch services. + * These objects must only be created during static initialization. + */ + static abstract interface ServiceFetcher<T> { + T getService(ContextImpl ctx); + } + + /** + * Override this class when the system service constructor needs a + * ContextImpl and should be cached and retained by that context. + */ + static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> { + private final int mCacheIndex; + + public CachedServiceFetcher() { + mCacheIndex = sServiceCacheSize++; + } + + @Override + @SuppressWarnings("unchecked") + public final T getService(ContextImpl ctx) { + final Object[] cache = ctx.mServiceCache; + synchronized (cache) { + // Fetch or create the service. + Object service = cache[mCacheIndex]; + if (service == null) { + service = createService(ctx); + cache[mCacheIndex] = service; + } + return (T)service; + } + } + + public abstract T createService(ContextImpl ctx); + } + + /** + * Override this class when the system service does not need a ContextImpl + * and should be cached and retained process-wide. + */ + static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> { + private T mCachedInstance; + + @Override + public final T getService(ContextImpl unused) { + synchronized (StaticServiceFetcher.this) { + if (mCachedInstance == null) { + mCachedInstance = createService(); + } + return mCachedInstance; + } + } + + public abstract T createService(); + } +} diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 46ccc95..010c860 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -36,7 +36,6 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; -import android.media.MediaScannerConnection.OnScanCompletedListener; import android.net.Uri; import android.os.Bundle; import android.os.Environment; @@ -469,10 +468,10 @@ public abstract class Context { /** * Retrieve styled attribute information in this Context's theme. See - * {@link Resources.Theme#obtainStyledAttributes(int[])} + * {@link android.content.res.Resources.Theme#obtainStyledAttributes(int[])} * for more information. * - * @see Resources.Theme#obtainStyledAttributes(int[]) + * @see android.content.res.Resources.Theme#obtainStyledAttributes(int[]) */ public final TypedArray obtainStyledAttributes( int[] attrs) { @@ -481,10 +480,10 @@ public abstract class Context { /** * Retrieve styled attribute information in this Context's theme. See - * {@link Resources.Theme#obtainStyledAttributes(int, int[])} + * {@link android.content.res.Resources.Theme#obtainStyledAttributes(int, int[])} * for more information. * - * @see Resources.Theme#obtainStyledAttributes(int, int[]) + * @see android.content.res.Resources.Theme#obtainStyledAttributes(int, int[]) */ public final TypedArray obtainStyledAttributes( @StyleableRes int resid, int[] attrs) throws Resources.NotFoundException { @@ -493,10 +492,10 @@ public abstract class Context { /** * Retrieve styled attribute information in this Context's theme. See - * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} + * {@link android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} * for more information. * - * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) + * @see android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) */ public final TypedArray obtainStyledAttributes( AttributeSet set, int[] attrs) { @@ -505,10 +504,10 @@ public abstract class Context { /** * Retrieve styled attribute information in this Context's theme. See - * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} + * {@link android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} * for more information. * - * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) + * @see android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) */ public final TypedArray obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) { @@ -753,7 +752,8 @@ public abstract class Context { * are not automatically scanned by the media scanner, you can explicitly * add them to the media database with * {@link android.media.MediaScannerConnection#scanFile(Context, String[], String[], - * OnScanCompletedListener) MediaScannerConnection.scanFile}. + * android.media.MediaScannerConnection.OnScanCompletedListener) + * MediaScannerConnection.scanFile}. * Note that this is not the same as * {@link android.os.Environment#getExternalStoragePublicDirectory * Environment.getExternalStoragePublicDirectory()}, which provides @@ -1918,7 +1918,7 @@ public abstract class Context { * @return The first sticky intent found that matches <var>filter</var>, * or null if there are none. * - * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler + * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) * @see #sendBroadcast * @see #unregisterReceiver */ @@ -2081,7 +2081,9 @@ public abstract class Context { * @hide */ @SystemApi - public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, UserHandle user) { + @SuppressWarnings("unused") + public boolean bindServiceAsUser(Intent service, ServiceConnection conn, + int flags, UserHandle user) { throw new RuntimeException("Not implemented. Must override in a subclass."); } @@ -2316,6 +2318,51 @@ public abstract class Context { public abstract Object getSystemService(@ServiceName @NonNull String name); /** + * Return the handle to a system-level service by class. + * <p> + * Currently available classes are: + * {@link android.view.WindowManager}, {@link android.view.LayoutInflater}, + * {@link android.app.ActivityManager}, {@link android.os.PowerManager}, + * {@link android.app.AlarmManager}, {@link android.app.NotificationManager}, + * {@link android.app.KeyguardManager}, {@link android.location.LocationManager}, + * {@link android.app.SearchManager}, {@link android.os.Vibrator}, + * {@link android.net.ConnectivityManager}, + * {@link android.net.wifi.WifiManager}, + * {@link android.media.AudioManager}, {@link android.media.MediaRouter}, + * {@link android.telephony.TelephonyManager}, {@link android.telephony.SubscriptionManager}, + * {@link android.view.inputmethod.InputMethodManager}, + * {@link android.app.UiModeManager}, {@link android.app.DownloadManager}, + * {@link android.os.BatteryManager}, {@link android.app.job.JobScheduler}. + * </p><p> + * Note: System services obtained via this API may be closely associated with + * the Context in which they are obtained from. In general, do not share the + * service objects between various different contexts (Activities, Applications, + * Services, Providers, etc.) + * </p> + * + * @param serviceClass The class of the desired service. + * @return The service or null if the class is not a supported system service. + */ + @SuppressWarnings("unchecked") + public final <T> T getSystemService(Class<T> serviceClass) { + // Because subclasses may override getSystemService(String) we cannot + // perform a lookup by class alone. We must first map the class to its + // service name then invoke the string-based method. + String serviceName = getSystemServiceName(serviceClass); + return serviceName != null ? (T)getSystemService(serviceName) : null; + } + + /** + * Gets the name of the system-level service that is represented by the specified class. + * + * @param serviceClass The class of the desired service. + * @return The service name or null if the class is not a supported system service. + * + * @hide + */ + public abstract String getSystemServiceName(Class<?> serviceClass); + + /** * Use with {@link #getSystemService} to retrieve a * {@link android.os.PowerManager} for controlling power management, * including "wake locks," which let you keep the device on while @@ -2610,7 +2657,7 @@ public abstract class Context { * of fingerprints. * * @see #getSystemService - * @see android.app.FingerprintManager + * @see android.service.fingerprint.FingerprintManager * @hide */ public static final String FINGERPRINT_SERVICE = "fingerprint"; @@ -2666,11 +2713,11 @@ public abstract class Context { /** * Use with {@link #getSystemService} to retrieve a - * {@link android.text.ClipboardManager} for accessing and modifying + * {@link android.content.ClipboardManager} for accessing and modifying * the contents of the global clipboard. * * @see #getSystemService - * @see android.text.ClipboardManager + * @see android.content.ClipboardManager */ public static final String CLIPBOARD_SERVICE = "clipboard"; @@ -2965,7 +3012,7 @@ public abstract class Context { * android.media.projection.MediaProjectionManager} instance for managing * media projection sessions. * @see #getSystemService - * @see android.media.projection.ProjectionManager + * @see android.media.projection.MediaProjectionManager */ public static final String MEDIA_PROJECTION_SERVICE = "media_projection"; @@ -3401,7 +3448,7 @@ public abstract class Context { * are not shared, however they share common state (Resources, ClassLoader, * etc) so the Context instance itself is fairly lightweight. * - * <p>Throws {@link PackageManager.NameNotFoundException} if there is no + * <p>Throws {@link android.content.pm.PackageManager.NameNotFoundException} if there is no * application with the given package name. * * <p>Throws {@link java.lang.SecurityException} if the Context requested diff --git a/core/java/android/content/ContextWrapper.java b/core/java/android/content/ContextWrapper.java index cfae1cf..6e8b7c1 100644 --- a/core/java/android/content/ContextWrapper.java +++ b/core/java/android/content/ContextWrapper.java @@ -284,36 +284,43 @@ public class ContextWrapper extends Context { } @Override + @Deprecated public Drawable getWallpaper() { return mBase.getWallpaper(); } @Override + @Deprecated public Drawable peekWallpaper() { return mBase.peekWallpaper(); } @Override + @Deprecated public int getWallpaperDesiredMinimumWidth() { return mBase.getWallpaperDesiredMinimumWidth(); } @Override + @Deprecated public int getWallpaperDesiredMinimumHeight() { return mBase.getWallpaperDesiredMinimumHeight(); } @Override + @Deprecated public void setWallpaper(Bitmap bitmap) throws IOException { mBase.setWallpaper(bitmap); } @Override + @Deprecated public void setWallpaper(InputStream data) throws IOException { mBase.setWallpaper(data); } @Override + @Deprecated public void clearWallpaper() throws IOException { mBase.clearWallpaper(); } @@ -445,11 +452,13 @@ public class ContextWrapper extends Context { } @Override + @Deprecated public void sendStickyBroadcast(Intent intent) { mBase.sendStickyBroadcast(intent); } @Override + @Deprecated public void sendStickyOrderedBroadcast( Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, @@ -460,16 +469,19 @@ public class ContextWrapper extends Context { } @Override + @Deprecated public void removeStickyBroadcast(Intent intent) { mBase.removeStickyBroadcast(intent); } @Override + @Deprecated public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { mBase.sendStickyBroadcastAsUser(intent, user); } @Override + @Deprecated public void sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, @@ -479,6 +491,7 @@ public class ContextWrapper extends Context { } @Override + @Deprecated public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { mBase.removeStickyBroadcastAsUser(intent, user); } @@ -563,6 +576,11 @@ public class ContextWrapper extends Context { } @Override + public String getSystemServiceName(Class<?> serviceClass) { + return mBase.getSystemServiceName(serviceClass); + } + + @Override public int checkPermission(String permission, int pid, int uid) { return mBase.checkPermission(permission, pid, uid); } @@ -679,6 +697,7 @@ public class ContextWrapper extends Context { } /** @hide */ + @Override public Context createApplicationContext(ApplicationInfo application, int flags) throws PackageManager.NameNotFoundException { return mBase.createApplicationContext(application, flags); diff --git a/core/java/android/os/BatteryManager.java b/core/java/android/os/BatteryManager.java index 537e993..bd5a392 100644 --- a/core/java/android/os/BatteryManager.java +++ b/core/java/android/os/BatteryManager.java @@ -162,7 +162,15 @@ public class BatteryManager { */ public static final int BATTERY_PROPERTY_ENERGY_COUNTER = 5; - private IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar; + private final IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar; + + /** + * @removed Was previously made visible by accident. + */ + public BatteryManager() { + mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface( + ServiceManager.getService("batteryproperties")); + } /** * Query a battery property from the batteryproperties service. @@ -174,12 +182,7 @@ public class BatteryManager { long ret; if (mBatteryPropertiesRegistrar == null) { - IBinder b = ServiceManager.getService("batteryproperties"); - mBatteryPropertiesRegistrar = - IBatteryPropertiesRegistrar.Stub.asInterface(b); - - if (mBatteryPropertiesRegistrar == null) - return Long.MIN_VALUE; + return Long.MIN_VALUE; } try { diff --git a/test-runner/src/android/test/mock/MockContext.java b/test-runner/src/android/test/mock/MockContext.java index 3378872..cfbebba 100644 --- a/test-runner/src/android/test/mock/MockContext.java +++ b/test-runner/src/android/test/mock/MockContext.java @@ -480,6 +480,11 @@ public class MockContext extends Context { } @Override + public String getSystemServiceName(Class<?> serviceClass) { + throw new UnsupportedOperationException(); + } + + @Override public int checkPermission(String permission, int pid, int uid) { throw new UnsupportedOperationException(); } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java index 2f62b93..d88a867 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java @@ -17,6 +17,7 @@ package com.android.layoutlib.bridge.android; import android.os.IBinder; + import com.android.annotations.Nullable; import com.android.ide.common.rendering.api.AssetRepository; import com.android.ide.common.rendering.api.ILayoutPullParser; @@ -73,6 +74,7 @@ import android.util.TypedValue; import android.view.BridgeInflater; import android.view.Display; import android.view.DisplayAdjustments; +import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; @@ -495,6 +497,34 @@ public final class BridgeContext extends Context { throw new UnsupportedOperationException("Unsupported Service: " + service); } + @Override + public String getSystemServiceName(Class<?> serviceClass) { + if (serviceClass.equals(LayoutInflater.class)) { + return LAYOUT_INFLATER_SERVICE; + } + + if (serviceClass.equals(TextServicesManager.class)) { + return TEXT_SERVICES_MANAGER_SERVICE; + } + + if (serviceClass.equals(WindowManager.class)) { + return WINDOW_SERVICE; + } + + if (serviceClass.equals(PowerManager.class)) { + return POWER_SERVICE; + } + + if (serviceClass.equals(DisplayManager.class)) { + return DISPLAY_SERVICE; + } + + if (serviceClass.equals(AccessibilityManager.class)) { + return ACCESSIBILITY_SERVICE; + } + + throw new UnsupportedOperationException("Unsupported Service: " + serviceClass); + } @Override public final BridgeTypedArray obtainStyledAttributes(int[] attrs) { |