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; |
Remi NGUYEN VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 23 | import com.android.networkstack.apishim.ConstantsShim; |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 24 | import com.android.server.ethernet.EthernetService; |
| 25 | import com.android.server.ethernet.EthernetServiceImpl; |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 26 | import com.android.server.nearby.NearbyService; |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 27 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 28 | /** |
| 29 | * Connectivity service initializer for core networking. This is called by system server to create |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 30 | * a new instance of connectivity services. |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 31 | */ |
| 32 | public final class ConnectivityServiceInitializer extends SystemService { |
| 33 | private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName(); |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 34 | private final Context mContext; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 35 | private final ConnectivityService mConnectivity; |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 36 | private final IpSecService mIpSecService; |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 37 | private final NsdService mNsdService; |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 38 | private final NearbyService mNearbyService; |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 39 | private final EthernetServiceImpl mEthernetServiceImpl; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 40 | |
| 41 | public ConnectivityServiceInitializer(Context context) { |
| 42 | super(context); |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 43 | mContext = context; |
Remi NGUYEN VAN | e724f63 | 2021-01-08 01:19:44 +0000 | [diff] [blame] | 44 | // Load JNI libraries used by ConnectivityService and its dependencies |
| 45 | System.loadLibrary("service-connectivity"); |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 46 | mEthernetServiceImpl = createEthernetService(context); |
junyulai | e7c7d2a | 2021-01-26 15:29:15 +0800 | [diff] [blame] | 47 | mConnectivity = new ConnectivityService(context); |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 48 | mIpSecService = createIpSecService(context); |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 49 | mNsdService = createNsdService(context); |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 50 | mNearbyService = createNearbyService(context); |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void onStart() { |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 55 | if (mConnectivity.deviceSupportsEthernet(mContext)) { |
| 56 | Log.i(TAG, "Registering " + Context.ETHERNET_SERVICE); |
| 57 | publishBinderService(Context.ETHERNET_SERVICE, mEthernetServiceImpl, |
| 58 | /* allowIsolated= */ false); |
| 59 | } |
| 60 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 61 | Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE); |
| 62 | publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity, |
Chiachang Wang | 96e1a0b | 2021-03-09 14:55:31 +0800 | [diff] [blame] | 63 | /* allowIsolated= */ false); |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 64 | |
| 65 | if (mIpSecService != null) { |
| 66 | Log.i(TAG, "Registering " + Context.IPSEC_SERVICE); |
| 67 | publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false); |
| 68 | } |
| 69 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 70 | if (mNsdService != null) { |
| 71 | Log.i(TAG, "Registering " + Context.NSD_SERVICE); |
| 72 | publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false); |
| 73 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 74 | |
| 75 | if (mNearbyService != null) { |
Remi NGUYEN VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 76 | Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE); |
| 77 | publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService, |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 78 | /* allowIsolated= */ false); |
| 79 | } |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 80 | |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onBootPhase(int phase) { |
| 85 | if (mNearbyService != null) { |
| 86 | mNearbyService.onBootPhase(phase); |
| 87 | } |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 88 | |
| 89 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY && mEthernetServiceImpl != null) { |
| 90 | mEthernetServiceImpl.start(); |
| 91 | } |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 94 | /** |
| 95 | * Return IpSecService instance, or null if current SDK is lower than T. |
| 96 | */ |
| 97 | private IpSecService createIpSecService(final Context context) { |
| 98 | if (!SdkLevel.isAtLeastT()) return null; |
| 99 | |
| 100 | return new IpSecService(context); |
| 101 | } |
| 102 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 103 | /** Return NsdService instance or null if current SDK is lower than T */ |
| 104 | private NsdService createNsdService(final Context context) { |
| 105 | if (!SdkLevel.isAtLeastT()) return null; |
| 106 | try { |
| 107 | return NsdService.create(context); |
| 108 | } catch (InterruptedException e) { |
| 109 | Log.d(TAG, "Unable to get NSD service", e); |
| 110 | return null; |
| 111 | } |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 112 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 113 | |
| 114 | /** Return Nearby service instance or null if current SDK is lower than T */ |
| 115 | private NearbyService createNearbyService(final Context context) { |
| 116 | if (!SdkLevel.isAtLeastT()) return null; |
Remi NGUYEN VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 117 | try { |
| 118 | return new NearbyService(context); |
| 119 | } catch (UnsupportedOperationException e) { |
| 120 | // Nearby is not yet supported in all branches |
| 121 | // TODO: remove catch clause when it is available. |
| 122 | Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE); |
| 123 | return null; |
| 124 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 125 | } |
Xiao Ma | eaaf7d3 | 2022-01-23 16:14:51 +0000 | [diff] [blame^] | 126 | |
| 127 | /** |
| 128 | * Return EthernetServiceImpl instance or null if current SDK is lower than T or Ethernet |
| 129 | * service isn't necessary. |
| 130 | */ |
| 131 | private EthernetServiceImpl createEthernetService(final Context context) { |
| 132 | if (!SdkLevel.isAtLeastT() || !mConnectivity.deviceSupportsEthernet(context)) { |
| 133 | return null; |
| 134 | } |
| 135 | return EthernetService.create(context); |
| 136 | } |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 137 | } |