Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 19 | import android.content.Context; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 20 | import android.util.Log; |
| 21 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame^] | 22 | import com.android.modules.utils.build.SdkLevel; |
| 23 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 24 | /** |
| 25 | * Connectivity service initializer for core networking. This is called by system server to create |
| 26 | * a new instance of ConnectivityService. |
| 27 | */ |
| 28 | public final class ConnectivityServiceInitializer extends SystemService { |
| 29 | private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName(); |
| 30 | private final ConnectivityService mConnectivity; |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame^] | 31 | private final NsdService mNsdService; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 32 | |
| 33 | public ConnectivityServiceInitializer(Context context) { |
| 34 | super(context); |
Remi NGUYEN VAN | e724f63 | 2021-01-08 01:19:44 +0000 | [diff] [blame] | 35 | // Load JNI libraries used by ConnectivityService and its dependencies |
| 36 | System.loadLibrary("service-connectivity"); |
junyulai | e7c7d2a | 2021-01-26 15:29:15 +0800 | [diff] [blame] | 37 | mConnectivity = new ConnectivityService(context); |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame^] | 38 | mNsdService = createNsdService(context); |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void onStart() { |
| 43 | Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE); |
| 44 | publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity, |
Chiachang Wang | 96e1a0b | 2021-03-09 14:55:31 +0800 | [diff] [blame] | 45 | /* allowIsolated= */ false); |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame^] | 46 | if (mNsdService != null) { |
| 47 | Log.i(TAG, "Registering " + Context.NSD_SERVICE); |
| 48 | publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** Return NsdService instance or null if current SDK is lower than T */ |
| 53 | private NsdService createNsdService(final Context context) { |
| 54 | if (!SdkLevel.isAtLeastT()) return null; |
| 55 | try { |
| 56 | return NsdService.create(context); |
| 57 | } catch (InterruptedException e) { |
| 58 | Log.d(TAG, "Unable to get NSD service", e); |
| 59 | return null; |
| 60 | } |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 61 | } |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 62 | } |