diff options
author | 2021-01-15 23:02:47 +0900 | |
---|---|---|
committer | 2021-02-01 17:10:39 +0900 | |
commit | b74c92e2a8cf9c6596e701a1b753b6edb45d3003 (patch) | |
tree | 59b6d091eaa5b4a11a8409c3938bc705337945d2 | |
parent | 3d70ab73eb0c8dba5d36bb413611db66efcf8cf0 (diff) |
Have connectivity self-register manager classes
As connectivity services are planned to move to a separate module, move
the manager classes registration from SystemServiceRegistry to
ConnectivityServicesRegistrar, using the registerContextAwareService
APIs.
This follows patterns and naming in WifiFrameworkInitializer.
Bug: 171540887
Test: device boots, connectivity working
Change-Id: I62ced1275750c73f209bac8ec3a3204b95695b83
4 files changed, 111 insertions, 60 deletions
diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt index 230863da4661..854e8fd8a6f6 100644 --- a/core/api/module-lib-current.txt +++ b/core/api/module-lib-current.txt @@ -10,6 +10,10 @@ package android.app { package android.net { + public final class ConnectivityFrameworkInitializer { + method public static void registerServiceWrappers(); + } + public class ConnectivityManager { method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void requestBackgroundNetwork(@NonNull android.net.NetworkRequest, @Nullable android.os.Handler, @NonNull android.net.ConnectivityManager.NetworkCallback); method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_TEST_NETWORKS, android.Manifest.permission.NETWORK_STACK}) public void simulateDataStall(int, long, @NonNull android.net.Network, @NonNull android.os.PersistableBundle); diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java index 82e48bfb61a7..d151526612f0 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -111,21 +111,16 @@ import android.media.tv.ITvInputManager; import android.media.tv.TvInputManager; import android.media.tv.tunerresourcemanager.ITunerResourceManager; import android.media.tv.tunerresourcemanager.TunerResourceManager; -import android.net.ConnectivityDiagnosticsManager; -import android.net.ConnectivityManager; +import android.net.ConnectivityFrameworkInitializer; import android.net.EthernetManager; -import android.net.IConnectivityManager; import android.net.IEthernetManager; import android.net.IIpSecService; import android.net.INetworkPolicyManager; -import android.net.ITestNetworkManager; import android.net.IpSecManager; import android.net.NetworkPolicyManager; import android.net.NetworkScoreManager; import android.net.NetworkWatchlistManager; -import android.net.TestNetworkManager; import android.net.TetheringManager; -import android.net.VpnManager; import android.net.lowpan.ILowpanManager; import android.net.lowpan.LowpanManager; import android.net.nsd.INsdManager; @@ -154,7 +149,6 @@ import android.os.IUserManager; import android.os.IncidentManager; import android.os.PowerManager; import android.os.RecoverySystem; -import android.os.RemoteException; import android.os.ServiceManager; import android.os.ServiceManager.ServiceNotFoundException; import android.os.StatsFrameworkInitializer; @@ -349,15 +343,6 @@ public final class SystemServiceRegistry { // (which extends it). SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE); - registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class, - new StaticApplicationContextServiceFetcher<ConnectivityManager>() { - @Override - public ConnectivityManager createService(Context context) throws ServiceNotFoundException { - IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); - IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); - return new ConnectivityManager(context, service); - }}); - registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() { @Override public IBinder createService() throws ServiceNotFoundException { @@ -391,50 +376,6 @@ public final class SystemServiceRegistry { return new IpSecManager(ctx, service); }}); - registerService(Context.VPN_MANAGEMENT_SERVICE, VpnManager.class, - new CachedServiceFetcher<VpnManager>() { - @Override - public VpnManager createService(ContextImpl ctx) throws ServiceNotFoundException { - IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); - IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); - return new VpnManager(ctx, service); - }}); - - registerService(Context.CONNECTIVITY_DIAGNOSTICS_SERVICE, - ConnectivityDiagnosticsManager.class, - new CachedServiceFetcher<ConnectivityDiagnosticsManager>() { - @Override - public ConnectivityDiagnosticsManager createService(ContextImpl ctx) - throws ServiceNotFoundException { - // ConnectivityDiagnosticsManager is backed by ConnectivityService - IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); - IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); - return new ConnectivityDiagnosticsManager(ctx, service); - }}); - - registerService( - Context.TEST_NETWORK_SERVICE, - TestNetworkManager.class, - new StaticApplicationContextServiceFetcher<TestNetworkManager>() { - @Override - public TestNetworkManager createService(Context context) - throws ServiceNotFoundException { - IBinder csBinder = - ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); - IConnectivityManager csMgr = - IConnectivityManager.Stub.asInterface(csBinder); - - final IBinder tnBinder; - try { - tnBinder = csMgr.startOrGetTestNetworkService(); - } catch (RemoteException e) { - throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE); - } - ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder); - return new TestNetworkManager(tnMgr); - } - }); - registerService(Context.COUNTRY_DETECTOR, CountryDetector.class, new StaticServiceFetcher<CountryDetector>() { @Override @@ -1355,6 +1296,7 @@ public final class SystemServiceRegistry { try { // Note: the following functions need to be @SystemApis, once they become mainline // modules. + ConnectivityFrameworkInitializer.registerServiceWrappers(); JobSchedulerFrameworkInitializer.registerServiceWrappers(); BlobStoreManagerFrameworkInitializer.initialize(); TelephonyFrameworkInitializer.registerServiceWrappers(); diff --git a/packages/Connectivity/framework/src/android/net/ConnectivityFrameworkInitializer.java b/packages/Connectivity/framework/src/android/net/ConnectivityFrameworkInitializer.java new file mode 100644 index 000000000000..9afa5d1311c5 --- /dev/null +++ b/packages/Connectivity/framework/src/android/net/ConnectivityFrameworkInitializer.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2021 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.net; + +import android.annotation.SystemApi; +import android.app.SystemServiceRegistry; +import android.content.Context; + +/** + * Class for performing registration for all core connectivity services. + * + * @hide + */ +@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) +public final class ConnectivityFrameworkInitializer { + private ConnectivityFrameworkInitializer() {} + + /** + * Called by {@link SystemServiceRegistry}'s static initializer and registers all core + * connectivity services to {@link Context}, so that {@link Context#getSystemService} can + * return them. + * + * @throws IllegalStateException if this is called anywhere besides + * {@link SystemServiceRegistry}. + */ + public static void registerServiceWrappers() { + // registerContextAwareService will throw if this is called outside of SystemServiceRegistry + // initialization. + SystemServiceRegistry.registerContextAwareService( + Context.CONNECTIVITY_SERVICE, + ConnectivityManager.class, + (context, serviceBinder) -> { + IConnectivityManager icm = IConnectivityManager.Stub.asInterface(serviceBinder); + return new ConnectivityManager(context, icm); + } + ); + + // TODO: move outside of the connectivity JAR + SystemServiceRegistry.registerContextAwareService( + Context.VPN_MANAGEMENT_SERVICE, + VpnManager.class, + (context) -> { + final ConnectivityManager cm = context.getSystemService( + ConnectivityManager.class); + return cm.createVpnManager(); + } + ); + + SystemServiceRegistry.registerContextAwareService( + Context.CONNECTIVITY_DIAGNOSTICS_SERVICE, + ConnectivityDiagnosticsManager.class, + (context) -> { + final ConnectivityManager cm = context.getSystemService( + ConnectivityManager.class); + return cm.createDiagnosticsManager(); + } + ); + + SystemServiceRegistry.registerContextAwareService( + Context.TEST_NETWORK_SERVICE, + TestNetworkManager.class, + context -> { + final ConnectivityManager cm = context.getSystemService( + ConnectivityManager.class); + return cm.startOrGetTestNetworkManager(); + } + ); + } +} diff --git a/packages/Connectivity/framework/src/android/net/ConnectivityManager.java b/packages/Connectivity/framework/src/android/net/ConnectivityManager.java index 7f07bba668a3..987dcc4898b5 100644 --- a/packages/Connectivity/framework/src/android/net/ConnectivityManager.java +++ b/packages/Connectivity/framework/src/android/net/ConnectivityManager.java @@ -4823,6 +4823,28 @@ public class ConnectivityManager { } } + /** @hide */ + public TestNetworkManager startOrGetTestNetworkManager() { + final IBinder tnBinder; + try { + tnBinder = mService.startOrGetTestNetworkService(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + + return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder)); + } + + /** @hide */ + public VpnManager createVpnManager() { + return new VpnManager(mContext, mService); + } + + /** @hide */ + public ConnectivityDiagnosticsManager createDiagnosticsManager() { + return new ConnectivityDiagnosticsManager(mContext, mService); + } + /** * Simulates a Data Stall for the specified Network. * |