diff options
| author | 2020-01-24 02:04:06 +0000 | |
|---|---|---|
| committer | 2020-01-24 02:04:06 +0000 | |
| commit | 4329c0d2ed70065d3ceee47446e148d468baadf0 (patch) | |
| tree | 6ecc07c406bf5ddcc0d63180b9a1d738391b0fd6 | |
| parent | 75b5b13c65f19a2905acfd9fab254ca1266021e3 (diff) | |
| parent | f58800b66a145b337537e563f1c2bea0a09cbe91 (diff) | |
Merge changes I9ea3677d,I0a59dce8
* changes:
Migrate away from using ServiceManager
Migrate StatsManager to apex
| -rw-r--r-- | apex/statsd/framework/Android.bp | 11 | ||||
| -rw-r--r-- | apex/statsd/framework/java/android/app/StatsManager.java (renamed from core/java/android/app/StatsManager.java) | 7 | ||||
| -rw-r--r-- | apex/statsd/framework/java/android/os/StatsFrameworkInitializer.java | 77 | ||||
| -rw-r--r-- | apex/statsd/service/java/com/android/server/stats/StatsCompanionService.java | 6 | ||||
| -rwxr-xr-x | api/system-current.txt | 20 | ||||
| -rw-r--r-- | core/java/android/app/ActivityThread.java | 3 | ||||
| -rw-r--r-- | core/java/android/app/SystemServiceRegistry.java | 9 | ||||
| -rw-r--r-- | core/java/android/os/StatsServiceManager.java | 124 |
8 files changed, 244 insertions, 13 deletions
diff --git a/apex/statsd/framework/Android.bp b/apex/statsd/framework/Android.bp index 0b46645ad06f..f66f0340edab 100644 --- a/apex/statsd/framework/Android.bp +++ b/apex/statsd/framework/Android.bp @@ -24,7 +24,7 @@ java_library { name: "framework-statsd", installable: true, // TODO(b/146209659): Use system_current instead. - sdk_version: "core_current", + sdk_version: "core_platform", srcs: [ ":framework-statsd-sources", ], @@ -35,7 +35,9 @@ java_library { libs: [ "framework-annotations-lib", // TODO(b/146230220): Use framework-system-stubs instead. - "android_system_stubs_current", + //"android_system_stubs_current", + //"framework_module_lib_stubs_current", + "framework-all", ], hostdex: true, // for hiddenapi check visibility: [ @@ -52,12 +54,14 @@ java_library { droidstubs { name: "framework-statsd-stubs-docs", defaults: [ - "framework-module-stubs-defaults-publicapi" + "framework-module-stubs-defaults-systemapi" ], srcs: [ + ":framework-annotations", ":framework-statsd-sources", ], libs: [ + // TODO(b/148218250): Change to android_system_stubs_current "framework-all", ], sdk_version: "core_platform", @@ -70,6 +74,7 @@ java_library { ":framework-statsd-stubs-docs", ], libs: [ + // TODO(b/148218250): Change to android_system_stubs_current "framework-all", ], sdk_version: "core_platform", diff --git a/core/java/android/app/StatsManager.java b/apex/statsd/framework/java/android/app/StatsManager.java index 0ea05d8f683c..ad1ac95d667c 100644 --- a/core/java/android/app/StatsManager.java +++ b/apex/statsd/framework/java/android/app/StatsManager.java @@ -30,7 +30,7 @@ import android.os.IPullAtomResultReceiver; import android.os.IStatsManagerService; import android.os.IStatsd; import android.os.RemoteException; -import android.os.ServiceManager; +import android.os.StatsFrameworkInitializer; import android.util.AndroidException; import android.util.Slog; import android.util.StatsEvent; @@ -702,7 +702,10 @@ public final class StatsManager { return mStatsManagerService; } mStatsManagerService = IStatsManagerService.Stub.asInterface( - ServiceManager.getService(Context.STATS_MANAGER_SERVICE)); + StatsFrameworkInitializer + .getStatsServiceManager() + .getStatsManagerServiceRegisterer() + .get()); return mStatsManagerService; } diff --git a/apex/statsd/framework/java/android/os/StatsFrameworkInitializer.java b/apex/statsd/framework/java/android/os/StatsFrameworkInitializer.java new file mode 100644 index 000000000000..3d955336b45c --- /dev/null +++ b/apex/statsd/framework/java/android/os/StatsFrameworkInitializer.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2020 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.os; + +import android.annotation.NonNull; +import android.annotation.SystemApi; +import android.app.StatsManager; +import android.app.SystemServiceRegistry; +import android.content.Context; + +/** + * Class for performing registration for all stats services + * + * TODO(b/148225705) Change to @SystemApi(client=MODULE_LIBRARIES) when the build system is ready. + * @hide + */ +@SystemApi +public class StatsFrameworkInitializer { + private StatsFrameworkInitializer() { + } + + private static volatile StatsServiceManager sStatsServiceManager; + + /** + * Sets an instance of {@link StatsServiceManager} that allows + * the statsd mainline module to register/obtain stats binder services. This is called + * by the platform during the system initialization. + * + * @param statsServiceManager instance of {@link StatsServiceManager} that allows + * the statsd mainline module to register/obtain statsd binder services. + */ + public static void setStatsServiceManager( + @NonNull StatsServiceManager statsServiceManager) { + if (sStatsServiceManager != null) { + throw new IllegalStateException("setStatsServiceManager called twice!"); + } + + if (statsServiceManager == null) { + throw new NullPointerException("statsServiceManager is null"); + } + + sStatsServiceManager = statsServiceManager; + } + + /** @hide */ + public static StatsServiceManager getStatsServiceManager() { + return sStatsServiceManager; + } + + /** + * Called by {@link SystemServiceRegistry}'s static initializer and registers all statsd + * services to {@link Context}, so that {@link Context#getSystemService} can return them. + * + * @throws IllegalStateException if this is called from anywhere besides + * {@link SystemServiceRegistry} + */ + public static void registerServiceWrappers() { + SystemServiceRegistry.registerContextAwareService( + Context.STATS_MANAGER, + StatsManager.class, + context -> new StatsManager(context) + ); + } +} diff --git a/apex/statsd/service/java/com/android/server/stats/StatsCompanionService.java b/apex/statsd/service/java/com/android/server/stats/StatsCompanionService.java index bcbb5a1407f6..4c8790f47bb6 100644 --- a/apex/statsd/service/java/com/android/server/stats/StatsCompanionService.java +++ b/apex/statsd/service/java/com/android/server/stats/StatsCompanionService.java @@ -85,6 +85,7 @@ import android.os.ParcelFileDescriptor; import android.os.Parcelable; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.StatsFrameworkInitializer; import android.os.StatFs; import android.os.StatsLogEventWrapper; import android.os.SynchronousResultReceiver; @@ -750,7 +751,10 @@ public class StatsCompanionService extends IStatsCompanionService.Stub { * sStatsd with a null check. */ private static IStatsd fetchStatsdService() { - return IStatsd.Stub.asInterface(ServiceManager.getService("stats")); + return IStatsd.Stub.asInterface(StatsFrameworkInitializer + .getStatsServiceManager() + .getStatsdServiceRegisterer() + .get()); } /** diff --git a/api/system-current.txt b/api/system-current.txt index e7ad9c0fb4f6..a2bd999dd803 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -8770,6 +8770,26 @@ package android.os { field public static final int TUPLE_VALUE_TYPE = 7; // 0x7 } + public class StatsFrameworkInitializer { + method public static void registerServiceWrappers(); + method public static void setStatsServiceManager(@NonNull android.os.StatsServiceManager); + } + + public class StatsServiceManager { + method @NonNull public android.os.StatsServiceManager.ServiceRegisterer getStatsCompanionServiceRegisterer(); + method @NonNull public android.os.StatsServiceManager.ServiceRegisterer getStatsManagerServiceRegisterer(); + method @NonNull public android.os.StatsServiceManager.ServiceRegisterer getStatsdServiceRegisterer(); + } + + public static class StatsServiceManager.ServiceNotFoundException extends java.lang.Exception { + ctor public StatsServiceManager.ServiceNotFoundException(@NonNull String); + } + + public static final class StatsServiceManager.ServiceRegisterer { + method @Nullable public android.os.IBinder get(); + method @Nullable public android.os.IBinder getOrThrow() throws android.os.StatsServiceManager.ServiceNotFoundException; + } + public class SystemConfigManager { method @NonNull @RequiresPermission(android.Manifest.permission.READ_CARRIER_APP_INFO) public java.util.Set<java.lang.String> getDisabledUntilUsedPreinstalledCarrierApps(); method @NonNull @RequiresPermission(android.Manifest.permission.READ_CARRIER_APP_INFO) public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getDisabledUntilUsedPreinstalledCarrierAssociatedApps(); diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 2ca5b1d5c76f..48f0087f6b30 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -111,6 +111,8 @@ import android.os.Process; import android.os.RemoteCallback; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.StatsFrameworkInitializer; +import android.os.StatsServiceManager; import android.os.StrictMode; import android.os.SystemClock; import android.os.SystemProperties; @@ -7523,6 +7525,7 @@ public final class ActivityThread extends ClientTransactionHandler { */ public static void initializeMainlineModules() { TelephonyFrameworkInitializer.setTelephonyServiceManager(new TelephonyServiceManager()); + StatsFrameworkInitializer.setStatsServiceManager(new StatsServiceManager()); } private void purgePendingResources() { diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java index 7f698653bef7..dcd179f8694d 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -150,6 +150,7 @@ import android.os.RecoverySystem; import android.os.RemoteException; import android.os.ServiceManager; import android.os.ServiceManager.ServiceNotFoundException; +import android.os.StatsFrameworkInitializer; import android.os.SystemConfigManager; import android.os.SystemUpdateManager; import android.os.SystemVibrator; @@ -601,13 +602,6 @@ public final class SystemServiceRegistry { return SensorPrivacyManager.getInstance(ctx); }}); - registerService(Context.STATS_MANAGER, StatsManager.class, - new CachedServiceFetcher<StatsManager>() { - @Override - public StatsManager createService(ContextImpl ctx) { - return new StatsManager(ctx.getOuterContext()); - }}); - registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class, new CachedServiceFetcher<StatusBarManager>() { @Override @@ -1327,6 +1321,7 @@ public final class SystemServiceRegistry { TelephonyFrameworkInitializer.registerServiceWrappers(); AppSearchManagerFrameworkInitializer.initialize(); WifiFrameworkInitializer.registerServiceWrappers(); + StatsFrameworkInitializer.registerServiceWrappers(); } finally { // If any of the above code throws, we're in a pretty bad shape and the process // will likely crash, but we'll reset it just in case there's an exception handler... diff --git a/core/java/android/os/StatsServiceManager.java b/core/java/android/os/StatsServiceManager.java new file mode 100644 index 000000000000..d032e98da00c --- /dev/null +++ b/core/java/android/os/StatsServiceManager.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2020 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.os; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.annotation.SystemApi; + +/** + * Provides a way to register and obtain the system service binder objects managed by the stats + * service. + * + * <p> Only the statsd mainline module will be able to access an instance of this class. + * + * TODO(b/148225705) Change to @SystemApi(client=MODULE_LIBRARIES) when the build system is ready. + * @hide + */ +@SystemApi +public class StatsServiceManager { + /** + * @hide + */ + public StatsServiceManager() {} + + /** + * A class that exposes the methods to register and obtain each system service. + */ + public static final class ServiceRegisterer { + private final String mServiceName; + + /** + * @hide + */ + public ServiceRegisterer(String serviceName) { + mServiceName = serviceName; + } + + /** + * Get the system server binding object for StatsManagerService. + * + * <p> This blocks until the service instance is ready. + * or a timeout happens, in which case it returns null. + */ + @Nullable + public IBinder get() { + return ServiceManager.getService(mServiceName); + } + + /** + * Get the system server binding object for a service. + * + * <p>This blocks until the service instance is ready, + * or a timeout happens, in which case it throws {@link ServiceNotFoundException}. + */ + @Nullable + public IBinder getOrThrow() throws ServiceNotFoundException { + try { + return ServiceManager.getServiceOrThrow(mServiceName); + } catch (ServiceManager.ServiceNotFoundException e) { + throw new ServiceNotFoundException(mServiceName); + } + } + + /** + * Get the system server binding object for a service. If the specified service is + * not available, it returns null. + */ + @Nullable + private IBinder tryGet() { + return ServiceManager.checkService(mServiceName); + } + } + + /** + * See {@link ServiceRegisterer#getOrThrow()} + */ + public static class ServiceNotFoundException extends ServiceManager.ServiceNotFoundException { + /** + * Constructor + * + * @param name the name of the binder service that cannot be found. + */ + public ServiceNotFoundException(@NonNull String name) { + super(name); + } + } + + /** + * Returns {@link ServiceRegisterer} for the "statscompanion" service. + */ + @NonNull + public ServiceRegisterer getStatsCompanionServiceRegisterer() { + return new ServiceRegisterer("statscompanion"); + } + + /** + * Returns {@link ServiceRegisterer} for the "statsmanager" service. + */ + @NonNull + public ServiceRegisterer getStatsManagerServiceRegisterer() { + return new ServiceRegisterer("statsmanager"); + } + + /** + * Returns {@link ServiceRegisterer} for the "statsd" service. + */ + @NonNull + public ServiceRegisterer getStatsdServiceRegisterer() { + return new ServiceRegisterer("stats"); + } +} |