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.app.usage.NetworkStatsManager;
22 import android.content.Context;
23 import android.net.mdns.aidl.IMDns;
24 import android.net.nsd.INsdManager;
25 import android.net.nsd.MDnsManager;
26 import android.net.nsd.NsdManager;
27 import android.net.thread.IThreadNetworkManager;
28 import android.net.thread.ThreadNetworkManager;
29 
30 import com.android.modules.utils.build.SdkLevel;
31 
32 /**
33  * Class for performing registration for Connectivity services which are exposed via updatable APIs
34  * since Android T.
35  *
36  * @hide
37  */
38 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
39 public final class ConnectivityFrameworkInitializerTiramisu {
ConnectivityFrameworkInitializerTiramisu()40     private ConnectivityFrameworkInitializerTiramisu() {}
41 
42     /**
43      * Called by {@link SystemServiceRegistry}'s static initializer and registers NetworkStats, nsd,
44      * ipsec and ethernet services to {@link Context}, so that {@link Context#getSystemService} can
45      * return them.
46      *
47      * @throws IllegalStateException if this is called anywhere besides
48      * {@link SystemServiceRegistry}.
49      */
registerServiceWrappers()50     public static void registerServiceWrappers() {
51         SystemServiceRegistry.registerContextAwareService(
52                 Context.NSD_SERVICE,
53                 NsdManager.class,
54                 (context, serviceBinder) -> {
55                     INsdManager service = INsdManager.Stub.asInterface(serviceBinder);
56                     return new NsdManager(context, service);
57                 }
58         );
59 
60         SystemServiceRegistry.registerContextAwareService(
61                 Context.IPSEC_SERVICE,
62                 IpSecManager.class,
63                 (context, serviceBinder) -> {
64                     IIpSecService service = IIpSecService.Stub.asInterface(serviceBinder);
65                     return new IpSecManager(context, service);
66                 }
67         );
68 
69         SystemServiceRegistry.registerContextAwareService(
70                 Context.NETWORK_STATS_SERVICE,
71                 NetworkStatsManager.class,
72                 (context, serviceBinder) -> {
73                     INetworkStatsService service =
74                             INetworkStatsService.Stub.asInterface(serviceBinder);
75                     return new NetworkStatsManager(context, service);
76                 }
77         );
78 
79         SystemServiceRegistry.registerContextAwareService(
80                 Context.ETHERNET_SERVICE,
81                 EthernetManager.class,
82                 (context, serviceBinder) -> {
83                     IEthernetManager service = IEthernetManager.Stub.asInterface(serviceBinder);
84                     return new EthernetManager(context, service);
85                 }
86         );
87 
88         // mdns service is removed from Netd from Android V.
89         if (!SdkLevel.isAtLeastV()) {
90             SystemServiceRegistry.registerStaticService(
91                     MDnsManager.MDNS_SERVICE,
92                     MDnsManager.class,
93                     (serviceBinder) -> {
94                         IMDns service = IMDns.Stub.asInterface(serviceBinder);
95                         return new MDnsManager(service);
96                     }
97             );
98         }
99 
100         SystemServiceRegistry.registerContextAwareService(
101                 ThreadNetworkManager.SERVICE_NAME,
102                 ThreadNetworkManager.class,
103                 (context, serviceBinder) -> {
104                     IThreadNetworkManager managerService =
105                             IThreadNetworkManager.Stub.asInterface(serviceBinder);
106                     return new ThreadNetworkManager(context, managerService);
107                 });
108     }
109 }
110