blob: c5090d9084d87dd81ed6ff627b3fcfe11993aae0 [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
paulhu667e8fb2021-12-14 01:30:22 +000022import com.android.modules.utils.build.SdkLevel;
Remi NGUYEN VANd8e74222022-01-20 18:40:29 +090023import com.android.server.nearby.NearbyService;
paulhu667e8fb2021-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 VANd8e74222022-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;
paulhu667e8fb2021-12-14 01:30:22 +000032 private final NsdService mNsdService;
Remi NGUYEN VANd8e74222022-01-20 18:40:29 +090033 private final NearbyService mNearbyService;
Aaron Huang96011892020-06-27 07:18:23 +080034
35 public ConnectivityServiceInitializer(Context context) {
36 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000037 // Load JNI libraries used by ConnectivityService and its dependencies
38 System.loadLibrary("service-connectivity");
junyulaie7c7d2a2021-01-26 15:29:15 +080039 mConnectivity = new ConnectivityService(context);
paulhu667e8fb2021-12-14 01:30:22 +000040 mNsdService = createNsdService(context);
Remi NGUYEN VANd8e74222022-01-20 18:40:29 +090041 mNearbyService = createNearbyService(context);
Aaron Huang96011892020-06-27 07:18:23 +080042 }
43
44 @Override
45 public void onStart() {
46 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
47 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080048 /* allowIsolated= */ false);
paulhu667e8fb2021-12-14 01:30:22 +000049 if (mNsdService != null) {
50 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
51 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
52 }
Remi NGUYEN VANd8e74222022-01-20 18:40:29 +090053
54 if (mNearbyService != null) {
55 Log.i(TAG, "Registering " + Context.NEARBY_SERVICE);
56 publishBinderService(Context.NEARBY_SERVICE, mNearbyService,
57 /* allowIsolated= */ false);
58 }
59 }
60
61 @Override
62 public void onBootPhase(int phase) {
63 if (mNearbyService != null) {
64 mNearbyService.onBootPhase(phase);
65 }
paulhu667e8fb2021-12-14 01:30:22 +000066 }
67
68 /** Return NsdService instance or null if current SDK is lower than T */
69 private NsdService createNsdService(final Context context) {
70 if (!SdkLevel.isAtLeastT()) return null;
71 try {
72 return NsdService.create(context);
73 } catch (InterruptedException e) {
74 Log.d(TAG, "Unable to get NSD service", e);
75 return null;
76 }
Aaron Huang96011892020-06-27 07:18:23 +080077 }
Remi NGUYEN VANd8e74222022-01-20 18:40:29 +090078
79 /** Return Nearby service instance or null if current SDK is lower than T */
80 private NearbyService createNearbyService(final Context context) {
81 if (!SdkLevel.isAtLeastT()) return null;
82 return new NearbyService(context);
83 }
Aaron Huang96011892020-06-27 07:18:23 +080084}