blob: 25fe5e90806f32a13a33633ab6b959cd307c5fe0 [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;
Aaron Huang96011892020-06-27 07:18:23 +080020import android.util.Log;
21
paulhu802ab972021-12-14 01:30:22 +000022import com.android.modules.utils.build.SdkLevel;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090023import com.android.networkstack.apishim.ConstantsShim;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090024import com.android.server.nearby.NearbyService;
paulhu802ab972021-12-14 01:30:22 +000025
Aaron Huang96011892020-06-27 07:18:23 +080026/**
27 * Connectivity service initializer for core networking. This is called by system server to create
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090028 * a new instance of connectivity services.
Aaron Huang96011892020-06-27 07:18:23 +080029 */
30public final class ConnectivityServiceInitializer extends SystemService {
31 private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName();
32 private final ConnectivityService mConnectivity;
Aaron Huang2e778ee2022-01-06 19:30:43 +080033 private final IpSecService mIpSecService;
paulhu802ab972021-12-14 01:30:22 +000034 private final NsdService mNsdService;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090035 private final NearbyService mNearbyService;
Aaron Huang96011892020-06-27 07:18:23 +080036
37 public ConnectivityServiceInitializer(Context context) {
38 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000039 // Load JNI libraries used by ConnectivityService and its dependencies
40 System.loadLibrary("service-connectivity");
junyulaie7c7d2a2021-01-26 15:29:15 +080041 mConnectivity = new ConnectivityService(context);
Aaron Huang2e778ee2022-01-06 19:30:43 +080042 mIpSecService = createIpSecService(context);
paulhu802ab972021-12-14 01:30:22 +000043 mNsdService = createNsdService(context);
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090044 mNearbyService = createNearbyService(context);
Aaron Huang96011892020-06-27 07:18:23 +080045 }
46
47 @Override
48 public void onStart() {
49 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
50 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080051 /* allowIsolated= */ false);
Aaron Huang2e778ee2022-01-06 19:30:43 +080052
53 if (mIpSecService != null) {
54 Log.i(TAG, "Registering " + Context.IPSEC_SERVICE);
55 publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false);
56 }
57
paulhu802ab972021-12-14 01:30:22 +000058 if (mNsdService != null) {
59 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
60 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
61 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090062
63 if (mNearbyService != null) {
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090064 Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE);
65 publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService,
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090066 /* allowIsolated= */ false);
67 }
68 }
69
70 @Override
71 public void onBootPhase(int phase) {
72 if (mNearbyService != null) {
73 mNearbyService.onBootPhase(phase);
74 }
paulhu802ab972021-12-14 01:30:22 +000075 }
76
Aaron Huang2e778ee2022-01-06 19:30:43 +080077 /**
78 * Return IpSecService instance, or null if current SDK is lower than T.
79 */
80 private IpSecService createIpSecService(final Context context) {
81 if (!SdkLevel.isAtLeastT()) return null;
82
83 return new IpSecService(context);
84 }
85
paulhu802ab972021-12-14 01:30:22 +000086 /** Return NsdService instance or null if current SDK is lower than T */
87 private NsdService createNsdService(final Context context) {
88 if (!SdkLevel.isAtLeastT()) return null;
89 try {
90 return NsdService.create(context);
91 } catch (InterruptedException e) {
92 Log.d(TAG, "Unable to get NSD service", e);
93 return null;
94 }
Aaron Huang96011892020-06-27 07:18:23 +080095 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090096
97 /** Return Nearby service instance or null if current SDK is lower than T */
98 private NearbyService createNearbyService(final Context context) {
99 if (!SdkLevel.isAtLeastT()) return null;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +0900100 try {
101 return new NearbyService(context);
102 } catch (UnsupportedOperationException e) {
103 // Nearby is not yet supported in all branches
104 // TODO: remove catch clause when it is available.
105 Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE);
106 return null;
107 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900108 }
Aaron Huang96011892020-06-27 07:18:23 +0800109}