blob: 67757affb946acb0dc1ea04e1d443786f5fea866 [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;
23
Aaron Huang96011892020-06-27 07:18:23 +080024/**
25 * Connectivity service initializer for core networking. This is called by system server to create
26 * a new instance of ConnectivityService.
27 */
28public final class ConnectivityServiceInitializer extends SystemService {
29 private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName();
30 private final ConnectivityService mConnectivity;
Aaron Huang2e778ee2022-01-06 19:30:43 +080031 private final IpSecService mIpSecService;
paulhu802ab972021-12-14 01:30:22 +000032 private final NsdService mNsdService;
Aaron Huang96011892020-06-27 07:18:23 +080033
34 public ConnectivityServiceInitializer(Context context) {
35 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000036 // Load JNI libraries used by ConnectivityService and its dependencies
37 System.loadLibrary("service-connectivity");
junyulaie7c7d2a2021-01-26 15:29:15 +080038 mConnectivity = new ConnectivityService(context);
Aaron Huang2e778ee2022-01-06 19:30:43 +080039 mIpSecService = createIpSecService(context);
paulhu802ab972021-12-14 01:30:22 +000040 mNsdService = createNsdService(context);
Aaron Huang96011892020-06-27 07:18:23 +080041 }
42
43 @Override
44 public void onStart() {
45 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
46 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080047 /* allowIsolated= */ false);
Aaron Huang2e778ee2022-01-06 19:30:43 +080048
49 if (mIpSecService != null) {
50 Log.i(TAG, "Registering " + Context.IPSEC_SERVICE);
51 publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false);
52 }
53
paulhu802ab972021-12-14 01:30:22 +000054 if (mNsdService != null) {
55 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
56 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
57 }
58 }
59
Aaron Huang2e778ee2022-01-06 19:30:43 +080060 /**
61 * Return IpSecService instance, or null if current SDK is lower than T.
62 */
63 private IpSecService createIpSecService(final Context context) {
64 if (!SdkLevel.isAtLeastT()) return null;
65
66 return new IpSecService(context);
67 }
68
paulhu802ab972021-12-14 01:30:22 +000069 /** Return NsdService instance or null if current SDK is lower than T */
70 private NsdService createNsdService(final Context context) {
71 if (!SdkLevel.isAtLeastT()) return null;
72 try {
73 return NsdService.create(context);
74 } catch (InterruptedException e) {
75 Log.d(TAG, "Unable to get NSD service", e);
76 return null;
77 }
Aaron Huang96011892020-06-27 07:18:23 +080078 }
Aaron Huang96011892020-06-27 07:18:23 +080079}