diff options
| author | 2020-01-23 13:22:10 -0800 | |
|---|---|---|
| committer | 2020-01-23 15:47:08 -0800 | |
| commit | f58800b66a145b337537e563f1c2bea0a09cbe91 (patch) | |
| tree | 32b387fc00ec5bb95fbc62c8fd551098abdb4edb | |
| parent | 38330a0833f40a3acc142c2bd191c7e0bcda0a7f (diff) | |
Migrate away from using ServiceManager
Create a wrapper class to access binder objects
published by statsd mainline module
Bug: 147923515
Test: atest com.google.android.statsd.gts.StatsdHostTestCases
Change-Id: I9ea3677d88c790c856e4e89318ae2ce67ac7df1e
7 files changed, 236 insertions, 10 deletions
diff --git a/apex/statsd/framework/java/android/app/StatsManager.java b/apex/statsd/framework/java/android/app/StatsManager.java index 0ea05d8f683c..ad1ac95d667c 100644 --- a/apex/statsd/framework/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 327f64268f8f..b694402882f2 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -8566,6 +8566,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 b82a67556fc0..57ba1819e9c0 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; @@ -7514,6 +7516,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 c1e535643ddf..e0c39c2e6648 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -148,6 +148,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; @@ -587,13 +588,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 @@ -1306,6 +1300,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"); + } +} |