1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net; 18 19 import android.annotation.SystemApi; 20 import android.app.SystemServiceRegistry; 21 import android.content.Context; 22 23 /** 24 * Class for performing registration for all core connectivity services. 25 * 26 * @hide 27 */ 28 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 29 public final class ConnectivityFrameworkInitializer { ConnectivityFrameworkInitializer()30 private ConnectivityFrameworkInitializer() {} 31 32 /** 33 * Called by {@link SystemServiceRegistry}'s static initializer and registers all core 34 * connectivity services to {@link Context}, so that {@link Context#getSystemService} can 35 * return them. 36 * 37 * @throws IllegalStateException if this is called anywhere besides 38 * {@link SystemServiceRegistry}. 39 */ registerServiceWrappers()40 public static void registerServiceWrappers() { 41 // registerContextAwareService will throw if this is called outside of SystemServiceRegistry 42 // initialization. 43 SystemServiceRegistry.registerContextAwareService( 44 Context.CONNECTIVITY_SERVICE, 45 ConnectivityManager.class, 46 (context, serviceBinder) -> { 47 IConnectivityManager icm = IConnectivityManager.Stub.asInterface(serviceBinder); 48 return new ConnectivityManager(context, icm); 49 } 50 ); 51 52 SystemServiceRegistry.registerContextAwareService( 53 Context.CONNECTIVITY_DIAGNOSTICS_SERVICE, 54 ConnectivityDiagnosticsManager.class, 55 (context) -> { 56 final ConnectivityManager cm = context.getSystemService( 57 ConnectivityManager.class); 58 return cm.createDiagnosticsManager(); 59 } 60 ); 61 62 SystemServiceRegistry.registerContextAwareService( 63 Context.TEST_NETWORK_SERVICE, 64 TestNetworkManager.class, 65 context -> { 66 final ConnectivityManager cm = context.getSystemService( 67 ConnectivityManager.class); 68 return cm.startOrGetTestNetworkManager(); 69 } 70 ); 71 72 SystemServiceRegistry.registerContextAwareService( 73 DnsResolverServiceManager.DNS_RESOLVER_SERVICE, 74 DnsResolverServiceManager.class, 75 (context, serviceBinder) -> new DnsResolverServiceManager(serviceBinder) 76 ); 77 } 78 } 79