blob: 2da067a93ac11eb55411aa06b12ca1f84a50bc36 [file] [log] [blame]
Aaron Huang96011892020-06-27 07:18:23 +08001/*
2 * Copyright (C) 2020 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
17package com.android.server;
18
Aaron Huang96011892020-06-27 07:18:23 +080019import android.content.Context;
Igor Zaslavskyec344f42023-08-08 04:28:45 +000020import android.remoteauth.RemoteAuthManager;
Aaron Huang96011892020-06-27 07:18:23 +080021import android.util.Log;
22
paulhu802ab972021-12-14 01:30:22 +000023import com.android.modules.utils.build.SdkLevel;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090024import com.android.networkstack.apishim.ConstantsShim;
Tyler Wearb37f5512021-10-01 13:22:00 -070025import com.android.server.connectivity.ConnectivityNativeService;
Xiao Ma0a171c02022-01-23 16:14:51 +000026import com.android.server.ethernet.EthernetService;
27import com.android.server.ethernet.EthernetServiceImpl;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090028import com.android.server.nearby.NearbyService;
Igor Zaslavskyec344f42023-08-08 04:28:45 +000029import com.android.server.remoteauth.RemoteAuthService;
paulhu802ab972021-12-14 01:30:22 +000030
Aaron Huang96011892020-06-27 07:18:23 +080031/**
32 * Connectivity service initializer for core networking. This is called by system server to create
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090033 * a new instance of connectivity services.
Aaron Huang96011892020-06-27 07:18:23 +080034 */
35public final class ConnectivityServiceInitializer extends SystemService {
36 private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName();
Tyler Wearb37f5512021-10-01 13:22:00 -070037 private final ConnectivityNativeService mConnectivityNative;
Aaron Huang96011892020-06-27 07:18:23 +080038 private final ConnectivityService mConnectivity;
Aaron Huang2e778ee2022-01-06 19:30:43 +080039 private final IpSecService mIpSecService;
paulhu802ab972021-12-14 01:30:22 +000040 private final NsdService mNsdService;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090041 private final NearbyService mNearbyService;
Xiao Ma0a171c02022-01-23 16:14:51 +000042 private final EthernetServiceImpl mEthernetServiceImpl;
Igor Zaslavskyec344f42023-08-08 04:28:45 +000043 private final RemoteAuthService mRemoteAuthService;
Aaron Huang96011892020-06-27 07:18:23 +080044
45 public ConnectivityServiceInitializer(Context context) {
46 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000047 // Load JNI libraries used by ConnectivityService and its dependencies
48 System.loadLibrary("service-connectivity");
Xiao Ma0a171c02022-01-23 16:14:51 +000049 mEthernetServiceImpl = createEthernetService(context);
junyulaie7c7d2a2021-01-26 15:29:15 +080050 mConnectivity = new ConnectivityService(context);
Aaron Huang2e778ee2022-01-06 19:30:43 +080051 mIpSecService = createIpSecService(context);
Tyler Wearb37f5512021-10-01 13:22:00 -070052 mConnectivityNative = createConnectivityNativeService(context);
paulhu802ab972021-12-14 01:30:22 +000053 mNsdService = createNsdService(context);
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090054 mNearbyService = createNearbyService(context);
Igor Zaslavskyec344f42023-08-08 04:28:45 +000055 mRemoteAuthService = createRemoteAuthService(context);
Aaron Huang96011892020-06-27 07:18:23 +080056 }
57
58 @Override
59 public void onStart() {
Xiao Ma799d0002022-03-16 03:25:25 +000060 if (mEthernetServiceImpl != null) {
Xiao Ma0a171c02022-01-23 16:14:51 +000061 Log.i(TAG, "Registering " + Context.ETHERNET_SERVICE);
62 publishBinderService(Context.ETHERNET_SERVICE, mEthernetServiceImpl,
63 /* allowIsolated= */ false);
64 }
65
Aaron Huang96011892020-06-27 07:18:23 +080066 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
67 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080068 /* allowIsolated= */ false);
Aaron Huang2e778ee2022-01-06 19:30:43 +080069
70 if (mIpSecService != null) {
71 Log.i(TAG, "Registering " + Context.IPSEC_SERVICE);
72 publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false);
73 }
74
Tyler Wearb37f5512021-10-01 13:22:00 -070075 if (mConnectivityNative != null) {
76 Log.i(TAG, "Registering " + ConnectivityNativeService.SERVICE_NAME);
77 publishBinderService(ConnectivityNativeService.SERVICE_NAME, mConnectivityNative,
78 /* allowIsolated= */ false);
79 }
80
paulhu802ab972021-12-14 01:30:22 +000081 if (mNsdService != null) {
82 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
83 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
84 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090085
86 if (mNearbyService != null) {
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090087 Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE);
88 publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService,
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090089 /* allowIsolated= */ false);
90 }
Xiao Ma0a171c02022-01-23 16:14:51 +000091
Igor Zaslavskyec344f42023-08-08 04:28:45 +000092 if (mRemoteAuthService != null) {
93 Log.i(TAG, "Registering " + RemoteAuthManager.REMOTE_AUTH_SERVICE);
94 publishBinderService(RemoteAuthManager.REMOTE_AUTH_SERVICE, mRemoteAuthService,
95 /* allowIsolated= */ false);
96 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090097 }
98
99 @Override
100 public void onBootPhase(int phase) {
101 if (mNearbyService != null) {
102 mNearbyService.onBootPhase(phase);
103 }
Xiao Ma0a171c02022-01-23 16:14:51 +0000104
105 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY && mEthernetServiceImpl != null) {
106 mEthernetServiceImpl.start();
107 }
paulhu802ab972021-12-14 01:30:22 +0000108 }
109
Aaron Huang2e778ee2022-01-06 19:30:43 +0800110 /**
111 * Return IpSecService instance, or null if current SDK is lower than T.
112 */
113 private IpSecService createIpSecService(final Context context) {
114 if (!SdkLevel.isAtLeastT()) return null;
115
116 return new IpSecService(context);
117 }
118
Tyler Wearb37f5512021-10-01 13:22:00 -0700119 /**
120 * Return ConnectivityNativeService instance, or null if current SDK is lower than T.
121 */
122 private ConnectivityNativeService createConnectivityNativeService(final Context context) {
123 if (!SdkLevel.isAtLeastT()) return null;
124 try {
125 return new ConnectivityNativeService(context);
126 } catch (UnsupportedOperationException e) {
127 Log.d(TAG, "Unable to get ConnectivityNative service", e);
128 return null;
129 }
130 }
131
paulhu802ab972021-12-14 01:30:22 +0000132 /** Return NsdService instance or null if current SDK is lower than T */
133 private NsdService createNsdService(final Context context) {
134 if (!SdkLevel.isAtLeastT()) return null;
paulhu1b35e822022-04-08 14:48:41 +0800135
136 return NsdService.create(context);
Aaron Huang96011892020-06-27 07:18:23 +0800137 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900138
139 /** Return Nearby service instance or null if current SDK is lower than T */
140 private NearbyService createNearbyService(final Context context) {
141 if (!SdkLevel.isAtLeastT()) return null;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +0900142 try {
143 return new NearbyService(context);
144 } catch (UnsupportedOperationException e) {
145 // Nearby is not yet supported in all branches
146 // TODO: remove catch clause when it is available.
147 Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE);
148 return null;
149 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900150 }
Xiao Ma0a171c02022-01-23 16:14:51 +0000151
Igor Zaslavskyec344f42023-08-08 04:28:45 +0000152 /** Return RemoteAuth service instance */
153 private RemoteAuthService createRemoteAuthService(final Context context) {
154 if (!SdkLevel.isAtLeastV()) return null;
155 try {
156 return new RemoteAuthService(context);
157 } catch (UnsupportedOperationException e) {
158 // RemoteAuth is not yet supported in all branches
159 // TODO: remove catch clause when it is available.
160 Log.i(TAG, "Skipping unsupported service "
161 + RemoteAuthManager.REMOTE_AUTH_SERVICE);
162 return null;
163 }
164 }
165
Xiao Ma0a171c02022-01-23 16:14:51 +0000166 /**
167 * Return EthernetServiceImpl instance or null if current SDK is lower than T or Ethernet
168 * service isn't necessary.
169 */
170 private EthernetServiceImpl createEthernetService(final Context context) {
171 if (!SdkLevel.isAtLeastT() || !mConnectivity.deviceSupportsEthernet(context)) {
172 return null;
173 }
174 return EthernetService.create(context);
175 }
Aaron Huang96011892020-06-27 07:18:23 +0800176}