blob: d11f0f26a125cf957a6fb8ab3f7167bd468769c6 [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 VAN5906c8b2022-01-20 18:40:29 +090023import com.android.server.nearby.NearbyService;
paulhu802ab972021-12-14 01:30:22 +000024
Aaron Huang96011892020-06-27 07:18:23 +080025/**
26 * Connectivity service initializer for core networking. This is called by system server to create
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090027 * a new instance of connectivity services.
Aaron Huang96011892020-06-27 07:18:23 +080028 */
29public final class ConnectivityServiceInitializer extends SystemService {
30 private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName();
31 private final ConnectivityService mConnectivity;
Aaron Huang2e778ee2022-01-06 19:30:43 +080032 private final IpSecService mIpSecService;
paulhu802ab972021-12-14 01:30:22 +000033 private final NsdService mNsdService;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090034 private final NearbyService mNearbyService;
Aaron Huang96011892020-06-27 07:18:23 +080035
36 public ConnectivityServiceInitializer(Context context) {
37 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000038 // Load JNI libraries used by ConnectivityService and its dependencies
39 System.loadLibrary("service-connectivity");
junyulaie7c7d2a2021-01-26 15:29:15 +080040 mConnectivity = new ConnectivityService(context);
Aaron Huang2e778ee2022-01-06 19:30:43 +080041 mIpSecService = createIpSecService(context);
paulhu802ab972021-12-14 01:30:22 +000042 mNsdService = createNsdService(context);
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090043 mNearbyService = createNearbyService(context);
Aaron Huang96011892020-06-27 07:18:23 +080044 }
45
46 @Override
47 public void onStart() {
48 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
49 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080050 /* allowIsolated= */ false);
Aaron Huang2e778ee2022-01-06 19:30:43 +080051
52 if (mIpSecService != null) {
53 Log.i(TAG, "Registering " + Context.IPSEC_SERVICE);
54 publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false);
55 }
56
paulhu802ab972021-12-14 01:30:22 +000057 if (mNsdService != null) {
58 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
59 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
60 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090061
62 if (mNearbyService != null) {
63 Log.i(TAG, "Registering " + Context.NEARBY_SERVICE);
64 publishBinderService(Context.NEARBY_SERVICE, mNearbyService,
65 /* allowIsolated= */ false);
66 }
67 }
68
69 @Override
70 public void onBootPhase(int phase) {
71 if (mNearbyService != null) {
72 mNearbyService.onBootPhase(phase);
73 }
paulhu802ab972021-12-14 01:30:22 +000074 }
75
Aaron Huang2e778ee2022-01-06 19:30:43 +080076 /**
77 * Return IpSecService instance, or null if current SDK is lower than T.
78 */
79 private IpSecService createIpSecService(final Context context) {
80 if (!SdkLevel.isAtLeastT()) return null;
81
82 return new IpSecService(context);
83 }
84
paulhu802ab972021-12-14 01:30:22 +000085 /** Return NsdService instance or null if current SDK is lower than T */
86 private NsdService createNsdService(final Context context) {
87 if (!SdkLevel.isAtLeastT()) return null;
88 try {
89 return NsdService.create(context);
90 } catch (InterruptedException e) {
91 Log.d(TAG, "Unable to get NSD service", e);
92 return null;
93 }
Aaron Huang96011892020-06-27 07:18:23 +080094 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090095
96 /** Return Nearby service instance or null if current SDK is lower than T */
97 private NearbyService createNearbyService(final Context context) {
98 if (!SdkLevel.isAtLeastT()) return null;
99 return new NearbyService(context);
100 }
Aaron Huang96011892020-06-27 07:18:23 +0800101}