Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 android.net; |
| 18 | |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 19 | import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; |
| 20 | |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 21 | import android.annotation.CallbackExecutor; |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 22 | import android.annotation.IntDef; |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 23 | import android.annotation.NonNull; |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 24 | import android.annotation.Nullable; |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 25 | import android.annotation.RequiresFeature; |
Remi NGUYEN VAN | 11f3924 | 2020-03-09 13:56:18 +0900 | [diff] [blame] | 26 | import android.annotation.RequiresPermission; |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 27 | import android.annotation.SystemApi; |
Jeff Sharkey | ad1cebe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 28 | import android.annotation.SystemService; |
Artur Satayev | 164c756 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 29 | import android.compat.annotation.UnsupportedAppUsage; |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 30 | import android.content.Context; |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 32 | import android.os.Build; |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 33 | import android.os.OutcomeReceiver; |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 34 | import android.os.RemoteException; |
| 35 | |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 36 | import com.android.internal.annotations.GuardedBy; |
Xiao Ma | dbc8e0f | 2021-12-28 09:16:09 +0000 | [diff] [blame] | 37 | import com.android.modules.utils.BackgroundThread; |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 38 | |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 41 | import java.util.ArrayList; |
Xiao Ma | 1edd455 | 2022-03-17 05:56:09 +0000 | [diff] [blame] | 42 | import java.util.List; |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 43 | import java.util.Objects; |
markchien | c8e7d75 | 2020-02-26 20:54:55 +0800 | [diff] [blame] | 44 | import java.util.concurrent.Executor; |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 45 | import java.util.function.IntConsumer; |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 46 | |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 47 | /** |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 48 | * A class that manages and configures Ethernet interfaces. |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 49 | * |
| 50 | * @hide |
| 51 | */ |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 52 | @SystemApi |
Jeff Sharkey | ad1cebe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 53 | @SystemService(Context.ETHERNET_SERVICE) |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 54 | public class EthernetManager { |
| 55 | private static final String TAG = "EthernetManager"; |
| 56 | |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 57 | private final IEthernetManager mService; |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 58 | @GuardedBy("mListenerLock") |
| 59 | private final ArrayList<ListenerInfo<InterfaceStateListener>> mIfaceListeners = |
| 60 | new ArrayList<>(); |
| 61 | @GuardedBy("mListenerLock") |
| 62 | private final ArrayList<ListenerInfo<IntConsumer>> mEthernetStateListeners = |
| 63 | new ArrayList<>(); |
| 64 | final Object mListenerLock = new Object(); |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 65 | private final IEthernetServiceListener.Stub mServiceListener = |
| 66 | new IEthernetServiceListener.Stub() { |
| 67 | @Override |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 68 | public void onEthernetStateChanged(int state) { |
| 69 | synchronized (mListenerLock) { |
| 70 | for (ListenerInfo<IntConsumer> li : mEthernetStateListeners) { |
| 71 | li.executor.execute(() -> { |
| 72 | li.listener.accept(state); |
| 73 | }); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Override |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 79 | public void onInterfaceStateChanged(String iface, int state, int role, |
| 80 | IpConfiguration configuration) { |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 81 | synchronized (mListenerLock) { |
| 82 | for (ListenerInfo<InterfaceStateListener> li : mIfaceListeners) { |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 83 | li.executor.execute(() -> |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 84 | li.listener.onInterfaceStateChanged(iface, state, role, |
| 85 | configuration)); |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 86 | } |
| 87 | } |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 88 | } |
| 89 | }; |
| 90 | |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 91 | /** |
| 92 | * Indicates that Ethernet is disabled. |
| 93 | * |
| 94 | * @hide |
| 95 | */ |
| 96 | @SystemApi(client = MODULE_LIBRARIES) |
| 97 | public static final int ETHERNET_STATE_DISABLED = 0; |
| 98 | |
| 99 | /** |
| 100 | * Indicates that Ethernet is enabled. |
| 101 | * |
| 102 | * @hide |
| 103 | */ |
| 104 | @SystemApi(client = MODULE_LIBRARIES) |
| 105 | public static final int ETHERNET_STATE_ENABLED = 1; |
| 106 | |
| 107 | private static class ListenerInfo<T> { |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 108 | @NonNull |
| 109 | public final Executor executor; |
| 110 | @NonNull |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 111 | public final T listener; |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 112 | |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 113 | private ListenerInfo(@NonNull Executor executor, @NonNull T listener) { |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 114 | this.executor = executor; |
| 115 | this.listener = listener; |
| 116 | } |
| 117 | } |
| 118 | |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 119 | /** |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 120 | * The interface is absent. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 121 | * @hide |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 122 | */ |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 123 | @SystemApi(client = MODULE_LIBRARIES) |
| 124 | public static final int STATE_ABSENT = 0; |
| 125 | |
| 126 | /** |
| 127 | * The interface is present but link is down. |
| 128 | * @hide |
| 129 | */ |
| 130 | @SystemApi(client = MODULE_LIBRARIES) |
| 131 | public static final int STATE_LINK_DOWN = 1; |
| 132 | |
| 133 | /** |
| 134 | * The interface is present and link is up. |
| 135 | * @hide |
| 136 | */ |
| 137 | @SystemApi(client = MODULE_LIBRARIES) |
| 138 | public static final int STATE_LINK_UP = 2; |
| 139 | |
| 140 | /** @hide */ |
| 141 | @IntDef(prefix = "STATE_", value = {STATE_ABSENT, STATE_LINK_DOWN, STATE_LINK_UP}) |
| 142 | @Retention(RetentionPolicy.SOURCE) |
| 143 | public @interface InterfaceState {} |
| 144 | |
| 145 | /** |
| 146 | * The interface currently does not have any specific role. |
| 147 | * @hide |
| 148 | */ |
| 149 | @SystemApi(client = MODULE_LIBRARIES) |
| 150 | public static final int ROLE_NONE = 0; |
| 151 | |
| 152 | /** |
| 153 | * The interface is in client mode (e.g., connected to the Internet). |
| 154 | * @hide |
| 155 | */ |
| 156 | @SystemApi(client = MODULE_LIBRARIES) |
| 157 | public static final int ROLE_CLIENT = 1; |
| 158 | |
| 159 | /** |
| 160 | * Ethernet interface is in server mode (e.g., providing Internet access to tethered devices). |
| 161 | * @hide |
| 162 | */ |
| 163 | @SystemApi(client = MODULE_LIBRARIES) |
| 164 | public static final int ROLE_SERVER = 2; |
| 165 | |
| 166 | /** @hide */ |
| 167 | @IntDef(prefix = "ROLE_", value = {ROLE_NONE, ROLE_CLIENT, ROLE_SERVER}) |
| 168 | @Retention(RetentionPolicy.SOURCE) |
| 169 | public @interface Role {} |
| 170 | |
| 171 | /** |
| 172 | * A listener that receives notifications about the state of Ethernet interfaces on the system. |
| 173 | * @hide |
| 174 | */ |
| 175 | @SystemApi(client = MODULE_LIBRARIES) |
| 176 | public interface InterfaceStateListener { |
| 177 | /** |
| 178 | * Called when an Ethernet interface changes state. |
| 179 | * |
| 180 | * @param iface the name of the interface. |
| 181 | * @param state the current state of the interface, or {@link #STATE_ABSENT} if the |
| 182 | * interface was removed. |
Lorenzo Colitti | 6c032e5 | 2022-02-11 12:18:59 +0900 | [diff] [blame] | 183 | * @param role whether the interface is in client mode or server mode. |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 184 | * @param configuration the current IP configuration of the interface. |
| 185 | * @hide |
| 186 | */ |
| 187 | @SystemApi(client = MODULE_LIBRARIES) |
| 188 | void onInterfaceStateChanged(@NonNull String iface, @InterfaceState int state, |
| 189 | @Role int role, @Nullable IpConfiguration configuration); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * A listener interface to receive notification on changes in Ethernet. |
| 194 | * This has never been a supported API. Use {@link InterfaceStateListener} instead. |
| 195 | * @hide |
| 196 | */ |
| 197 | public interface Listener extends InterfaceStateListener { |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 198 | /** |
| 199 | * Called when Ethernet port's availability is changed. |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 200 | * @param iface Ethernet interface name |
| 201 | * @param isAvailable {@code true} if Ethernet port exists. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 202 | * @hide |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 203 | */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 204 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 205 | void onAvailabilityChanged(String iface, boolean isAvailable); |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 206 | |
| 207 | /** Default implementation for backwards compatibility. Only calls the legacy listener. */ |
| 208 | default void onInterfaceStateChanged(@NonNull String iface, @InterfaceState int state, |
| 209 | @Role int role, @Nullable IpConfiguration configuration) { |
| 210 | onAvailabilityChanged(iface, (state >= STATE_LINK_UP)); |
| 211 | } |
| 212 | |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 213 | } |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Create a new EthernetManager instance. |
| 217 | * Applications will almost always want to use |
| 218 | * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve |
| 219 | * the standard {@link android.content.Context#ETHERNET_SERVICE Context.ETHERNET_SERVICE}. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 220 | * @hide |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 221 | */ |
| 222 | public EthernetManager(Context context, IEthernetManager service) { |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 223 | mService = service; |
| 224 | } |
| 225 | |
| 226 | /** |
Lorenzo Colitti | 56e4e58 | 2014-05-22 11:51:27 -0700 | [diff] [blame] | 227 | * Get Ethernet configuration. |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 228 | * @return the Ethernet Configuration, contained in {@link IpConfiguration}. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 229 | * @hide |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 230 | */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 231 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 232 | public IpConfiguration getConfiguration(String iface) { |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 233 | try { |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 234 | return mService.getConfiguration(iface); |
Jeff Sharkey | f713828 | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 235 | } catch (RemoteException e) { |
| 236 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
Lorenzo Colitti | 56e4e58 | 2014-05-22 11:51:27 -0700 | [diff] [blame] | 241 | * Set Ethernet configuration. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 242 | * @hide |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 243 | */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 244 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 245 | public void setConfiguration(@NonNull String iface, @NonNull IpConfiguration config) { |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 246 | try { |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 247 | mService.setConfiguration(iface, config); |
Jeff Sharkey | f713828 | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 248 | } catch (RemoteException e) { |
| 249 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 254 | * Indicates whether the system currently has one or more Ethernet interfaces. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 255 | * @hide |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 256 | */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 257 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 258 | public boolean isAvailable() { |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 259 | return getAvailableInterfaces().length > 0; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Indicates whether the system has given interface. |
| 264 | * |
| 265 | * @param iface Ethernet interface name |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 266 | * @hide |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 267 | */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 268 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 269 | public boolean isAvailable(String iface) { |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 270 | try { |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 271 | return mService.isAvailable(iface); |
Jeff Sharkey | f713828 | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 272 | } catch (RemoteException e) { |
| 273 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Adds a listener. |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 279 | * This has never been a supported API. Use {@link #addInterfaceStateListener} instead. |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 280 | * |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 281 | * @param listener A {@link Listener} to add. |
| 282 | * @throws IllegalArgumentException If the listener is null. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 283 | * @hide |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 284 | */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 285 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 286 | public void addListener(@NonNull Listener listener) { |
| 287 | addListener(listener, BackgroundThread.getExecutor()); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Adds a listener. |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 292 | * This has never been a supported API. Use {@link #addInterfaceStateListener} instead. |
| 293 | * |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 294 | * @param listener A {@link Listener} to add. |
| 295 | * @param executor Executor to run callbacks on. |
| 296 | * @throws IllegalArgumentException If the listener or executor is null. |
| 297 | * @hide |
| 298 | */ |
| 299 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 300 | public void addListener(@NonNull Listener listener, @NonNull Executor executor) { |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 301 | addInterfaceStateListener(executor, listener); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Listen to changes in the state of Ethernet interfaces. |
| 306 | * |
| 307 | * Adds a listener to receive notification for any state change of all existing Ethernet |
| 308 | * interfaces. |
| 309 | * <p>{@link Listener#onInterfaceStateChanged} will be triggered immediately for all |
| 310 | * existing interfaces upon adding a listener. The same method will be called on the |
| 311 | * listener every time any of the interface changes state. In particular, if an |
| 312 | * interface is removed, it will be called with state {@link #STATE_ABSENT}. |
| 313 | * <p>Use {@link #removeInterfaceStateListener} with the same object to stop listening. |
| 314 | * |
| 315 | * @param executor Executor to run callbacks on. |
| 316 | * @param listener A {@link Listener} to add. |
| 317 | * @hide |
| 318 | */ |
| 319 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 320 | @SystemApi(client = MODULE_LIBRARIES) |
| 321 | public void addInterfaceStateListener(@NonNull Executor executor, |
| 322 | @NonNull InterfaceStateListener listener) { |
Remi NGUYEN VAN | 55838a8 | 2020-12-03 17:58:49 +0900 | [diff] [blame] | 323 | if (listener == null || executor == null) { |
| 324 | throw new NullPointerException("listener and executor must not be null"); |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 325 | } |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 326 | synchronized (mListenerLock) { |
| 327 | maybeAddServiceListener(); |
| 328 | mIfaceListeners.add(new ListenerInfo<InterfaceStateListener>(executor, listener)); |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 332 | @GuardedBy("mListenerLock") |
| 333 | private void maybeAddServiceListener() { |
| 334 | if (!mIfaceListeners.isEmpty() || !mEthernetStateListeners.isEmpty()) return; |
| 335 | |
| 336 | try { |
| 337 | mService.addListener(mServiceListener); |
| 338 | } catch (RemoteException e) { |
| 339 | throw e.rethrowFromSystemServer(); |
| 340 | } |
| 341 | |
| 342 | } |
| 343 | |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 344 | /** |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 345 | * Returns an array of available Ethernet interface names. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 346 | * @hide |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 347 | */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 348 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Pavel Maltsev | 50ec1f3 | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 349 | public String[] getAvailableInterfaces() { |
| 350 | try { |
| 351 | return mService.getAvailableInterfaces(); |
| 352 | } catch (RemoteException e) { |
| 353 | throw e.rethrowAsRuntimeException(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 358 | * Removes a listener. |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 359 | * |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 360 | * @param listener A {@link Listener} to remove. |
Remi NGUYEN VAN | 199d685 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 361 | * @hide |
Jaewan Kim | 32b3f2c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 362 | */ |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 363 | @SystemApi(client = MODULE_LIBRARIES) |
| 364 | public void removeInterfaceStateListener(@NonNull InterfaceStateListener listener) { |
| 365 | Objects.requireNonNull(listener); |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 366 | synchronized (mListenerLock) { |
| 367 | mIfaceListeners.removeIf(l -> l.listener == listener); |
| 368 | maybeRemoveServiceListener(); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | @GuardedBy("mListenerLock") |
| 373 | private void maybeRemoveServiceListener() { |
| 374 | if (!mIfaceListeners.isEmpty() || !mEthernetStateListeners.isEmpty()) return; |
| 375 | |
| 376 | try { |
| 377 | mService.removeListener(mServiceListener); |
| 378 | } catch (RemoteException e) { |
| 379 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 382 | |
| 383 | /** |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 384 | * Removes a listener. |
| 385 | * This has never been a supported API. Use {@link #removeInterfaceStateListener} instead. |
| 386 | * @param listener A {@link Listener} to remove. |
| 387 | * @hide |
| 388 | */ |
| 389 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 390 | public void removeListener(@NonNull Listener listener) { |
| 391 | if (listener == null) { |
| 392 | throw new IllegalArgumentException("listener must not be null"); |
| 393 | } |
| 394 | removeInterfaceStateListener(listener); |
| 395 | } |
| 396 | |
| 397 | /** |
Lorenzo Colitti | 013187d | 2020-03-17 00:16:13 +0900 | [diff] [blame] | 398 | * Whether to treat interfaces created by {@link TestNetworkManager#createTapInterface} |
| 399 | * as Ethernet interfaces. The effects of this method apply to any test interfaces that are |
| 400 | * already present on the system. |
| 401 | * @hide |
| 402 | */ |
Xiao Ma | 7f9ac50 | 2022-01-30 11:27:16 +0000 | [diff] [blame] | 403 | @SystemApi(client = MODULE_LIBRARIES) |
Lorenzo Colitti | 013187d | 2020-03-17 00:16:13 +0900 | [diff] [blame] | 404 | public void setIncludeTestInterfaces(boolean include) { |
| 405 | try { |
| 406 | mService.setIncludeTestInterfaces(include); |
| 407 | } catch (RemoteException e) { |
| 408 | throw e.rethrowFromSystemServer(); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /** |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 413 | * A request for a tethered interface. |
| 414 | */ |
| 415 | public static class TetheredInterfaceRequest { |
| 416 | private final IEthernetManager mService; |
| 417 | private final ITetheredInterfaceCallback mCb; |
| 418 | |
| 419 | private TetheredInterfaceRequest(@NonNull IEthernetManager service, |
| 420 | @NonNull ITetheredInterfaceCallback cb) { |
| 421 | this.mService = service; |
| 422 | this.mCb = cb; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Release the request, causing the interface to revert back from tethering mode if there |
| 427 | * is no other requestor. |
| 428 | */ |
| 429 | public void release() { |
| 430 | try { |
| 431 | mService.releaseTetheredInterface(mCb); |
| 432 | } catch (RemoteException e) { |
| 433 | e.rethrowFromSystemServer(); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Callback for {@link #requestTetheredInterface(TetheredInterfaceCallback)}. |
| 440 | */ |
| 441 | public interface TetheredInterfaceCallback { |
| 442 | /** |
| 443 | * Called when the tethered interface is available. |
| 444 | * @param iface The name of the interface. |
| 445 | */ |
| 446 | void onAvailable(@NonNull String iface); |
| 447 | |
| 448 | /** |
| 449 | * Called when the tethered interface is now unavailable. |
| 450 | */ |
| 451 | void onUnavailable(); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Request a tethered interface in tethering mode. |
| 456 | * |
| 457 | * <p>When this method is called and there is at least one ethernet interface available, the |
| 458 | * system will designate one to act as a tethered interface. If there is already a tethered |
| 459 | * interface, the existing interface will be used. |
| 460 | * @param callback A callback to be called once the request has been fulfilled. |
| 461 | */ |
Remi NGUYEN VAN | 11f3924 | 2020-03-09 13:56:18 +0900 | [diff] [blame] | 462 | @RequiresPermission(anyOf = { |
| 463 | android.Manifest.permission.NETWORK_STACK, |
| 464 | android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 465 | }) |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 466 | @NonNull |
markchien | c8e7d75 | 2020-02-26 20:54:55 +0800 | [diff] [blame] | 467 | public TetheredInterfaceRequest requestTetheredInterface(@NonNull final Executor executor, |
| 468 | @NonNull final TetheredInterfaceCallback callback) { |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 469 | Objects.requireNonNull(callback, "Callback must be non-null"); |
markchien | c8e7d75 | 2020-02-26 20:54:55 +0800 | [diff] [blame] | 470 | Objects.requireNonNull(executor, "Executor must be non-null"); |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 471 | final ITetheredInterfaceCallback cbInternal = new ITetheredInterfaceCallback.Stub() { |
| 472 | @Override |
| 473 | public void onAvailable(String iface) { |
markchien | c8e7d75 | 2020-02-26 20:54:55 +0800 | [diff] [blame] | 474 | executor.execute(() -> callback.onAvailable(iface)); |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public void onUnavailable() { |
markchien | c8e7d75 | 2020-02-26 20:54:55 +0800 | [diff] [blame] | 479 | executor.execute(() -> callback.onUnavailable()); |
Lorenzo Colitti | 19b8b88 | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 480 | } |
| 481 | }; |
| 482 | |
| 483 | try { |
| 484 | mService.requestTetheredInterface(cbInternal); |
| 485 | } catch (RemoteException e) { |
| 486 | throw e.rethrowFromSystemServer(); |
| 487 | } |
| 488 | return new TetheredInterfaceRequest(mService, cbInternal); |
| 489 | } |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 490 | |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 491 | private static final class NetworkInterfaceOutcomeReceiver |
| 492 | extends INetworkInterfaceOutcomeReceiver.Stub { |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 493 | @NonNull |
| 494 | private final Executor mExecutor; |
| 495 | @NonNull |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 496 | private final OutcomeReceiver<String, EthernetNetworkManagementException> mCallback; |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 497 | |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 498 | NetworkInterfaceOutcomeReceiver( |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 499 | @NonNull final Executor executor, |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 500 | @NonNull final OutcomeReceiver<String, EthernetNetworkManagementException> |
| 501 | callback) { |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 502 | Objects.requireNonNull(executor, "Pass a non-null executor"); |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 503 | Objects.requireNonNull(callback, "Pass a non-null callback"); |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 504 | mExecutor = executor; |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 505 | mCallback = callback; |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Override |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 509 | public void onResult(@NonNull String iface) { |
| 510 | mExecutor.execute(() -> mCallback.onResult(iface)); |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public void onError(@NonNull EthernetNetworkManagementException e) { |
| 515 | mExecutor.execute(() -> mCallback.onError(e)); |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 519 | private NetworkInterfaceOutcomeReceiver makeNetworkInterfaceOutcomeReceiver( |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 520 | @Nullable final Executor executor, |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 521 | @Nullable final OutcomeReceiver<String, EthernetNetworkManagementException> callback) { |
| 522 | if (null != callback) { |
| 523 | Objects.requireNonNull(executor, "Pass a non-null executor, or a null callback"); |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 524 | } |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 525 | final NetworkInterfaceOutcomeReceiver proxy; |
| 526 | if (null == callback) { |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 527 | proxy = null; |
| 528 | } else { |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 529 | proxy = new NetworkInterfaceOutcomeReceiver(executor, callback); |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 530 | } |
| 531 | return proxy; |
| 532 | } |
| 533 | |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 534 | /** |
| 535 | * Updates the configuration of an automotive device's ethernet network. |
| 536 | * |
| 537 | * The {@link EthernetNetworkUpdateRequest} {@code request} argument describes how to update the |
| 538 | * configuration for this network. |
| 539 | * Use {@link StaticIpConfiguration.Builder} to build a {@code StaticIpConfiguration} object for |
| 540 | * this network to put inside the {@code request}. |
| 541 | * Similarly, use {@link NetworkCapabilities.Builder} to build a {@code NetworkCapabilities} |
| 542 | * object for this network to put inside the {@code request}. |
| 543 | * |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 544 | * This function accepts an {@link OutcomeReceiver} that is called once the operation has |
| 545 | * finished execution. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 546 | * |
| 547 | * @param iface the name of the interface to act upon. |
| 548 | * @param request the {@link EthernetNetworkUpdateRequest} used to set an ethernet network's |
| 549 | * {@link StaticIpConfiguration} and {@link NetworkCapabilities} values. |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 550 | * @param executor an {@link Executor} to execute the callback on. Optional if callback is null. |
| 551 | * @param callback an optional {@link OutcomeReceiver} to listen for completion of the |
| 552 | * operation. On success, {@link OutcomeReceiver#onResult} is called with the |
| 553 | * interface name. On error, {@link OutcomeReceiver#onError} is called with more |
| 554 | * information about the error. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 555 | * @throws SecurityException if the process doesn't hold |
| 556 | * {@link android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}. |
| 557 | * @throws UnsupportedOperationException if called on a non-automotive device or on an |
| 558 | * unsupported interface. |
| 559 | * @hide |
| 560 | */ |
| 561 | @SystemApi |
| 562 | @RequiresPermission(anyOf = { |
| 563 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 564 | android.Manifest.permission.NETWORK_STACK, |
| 565 | android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}) |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 566 | public void updateConfiguration( |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 567 | @NonNull String iface, |
James Mattis | 1ecadfa | 2022-01-28 13:42:38 -0800 | [diff] [blame] | 568 | @NonNull EthernetNetworkUpdateRequest request, |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 569 | @Nullable @CallbackExecutor Executor executor, |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 570 | @Nullable OutcomeReceiver<String, EthernetNetworkManagementException> callback) { |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 571 | Objects.requireNonNull(iface, "iface must be non-null"); |
| 572 | Objects.requireNonNull(request, "request must be non-null"); |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 573 | final NetworkInterfaceOutcomeReceiver proxy = makeNetworkInterfaceOutcomeReceiver( |
| 574 | executor, callback); |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 575 | try { |
| 576 | mService.updateConfiguration(iface, request, proxy); |
| 577 | } catch (RemoteException e) { |
| 578 | throw e.rethrowFromSystemServer(); |
| 579 | } |
| 580 | } |
| 581 | |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 582 | /** |
Patrick Rohr | 3879cf1 | 2022-03-16 12:34:41 +0100 | [diff] [blame] | 583 | * Enable a network interface. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 584 | * |
Patrick Rohr | 3879cf1 | 2022-03-16 12:34:41 +0100 | [diff] [blame] | 585 | * Enables a previously disabled network interface. |
| 586 | * This function accepts an {@link OutcomeReceiver} that is called once the operation has |
| 587 | * finished execution. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 588 | * |
Patrick Rohr | 3879cf1 | 2022-03-16 12:34:41 +0100 | [diff] [blame] | 589 | * @param iface the name of the interface to enable. |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 590 | * @param executor an {@link Executor} to execute the callback on. Optional if callback is null. |
| 591 | * @param callback an optional {@link OutcomeReceiver} to listen for completion of the |
| 592 | * operation. On success, {@link OutcomeReceiver#onResult} is called with the |
| 593 | * interface name. On error, {@link OutcomeReceiver#onError} is called with more |
| 594 | * information about the error. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 595 | * @throws SecurityException if the process doesn't hold |
| 596 | * {@link android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 597 | * @hide |
| 598 | */ |
| 599 | @SystemApi |
| 600 | @RequiresPermission(anyOf = { |
| 601 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 602 | android.Manifest.permission.NETWORK_STACK, |
| 603 | android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}) |
| 604 | @RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE) |
Patrick Rohr | 3879cf1 | 2022-03-16 12:34:41 +0100 | [diff] [blame] | 605 | public void enableInterface( |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 606 | @NonNull String iface, |
| 607 | @Nullable @CallbackExecutor Executor executor, |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 608 | @Nullable OutcomeReceiver<String, EthernetNetworkManagementException> callback) { |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 609 | Objects.requireNonNull(iface, "iface must be non-null"); |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 610 | final NetworkInterfaceOutcomeReceiver proxy = makeNetworkInterfaceOutcomeReceiver( |
| 611 | executor, callback); |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 612 | try { |
| 613 | mService.connectNetwork(iface, proxy); |
| 614 | } catch (RemoteException e) { |
| 615 | throw e.rethrowFromSystemServer(); |
| 616 | } |
| 617 | } |
| 618 | |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 619 | /** |
Patrick Rohr | 3879cf1 | 2022-03-16 12:34:41 +0100 | [diff] [blame] | 620 | * Disable a network interface. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 621 | * |
Patrick Rohr | 3879cf1 | 2022-03-16 12:34:41 +0100 | [diff] [blame] | 622 | * Disables the use of a network interface to fulfill network requests. If the interface |
| 623 | * currently serves a request, the network will be torn down. |
| 624 | * This function accepts an {@link OutcomeReceiver} that is called once the operation has |
| 625 | * finished execution. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 626 | * |
Patrick Rohr | 3879cf1 | 2022-03-16 12:34:41 +0100 | [diff] [blame] | 627 | * @param iface the name of the interface to disable. |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 628 | * @param executor an {@link Executor} to execute the callback on. Optional if callback is null. |
| 629 | * @param callback an optional {@link OutcomeReceiver} to listen for completion of the |
| 630 | * operation. On success, {@link OutcomeReceiver#onResult} is called with the |
| 631 | * interface name. On error, {@link OutcomeReceiver#onError} is called with more |
| 632 | * information about the error. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 633 | * @throws SecurityException if the process doesn't hold |
| 634 | * {@link android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}. |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 635 | * @hide |
| 636 | */ |
| 637 | @SystemApi |
| 638 | @RequiresPermission(anyOf = { |
| 639 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 640 | android.Manifest.permission.NETWORK_STACK, |
| 641 | android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}) |
| 642 | @RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE) |
Patrick Rohr | 3879cf1 | 2022-03-16 12:34:41 +0100 | [diff] [blame] | 643 | public void disableInterface( |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 644 | @NonNull String iface, |
| 645 | @Nullable @CallbackExecutor Executor executor, |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 646 | @Nullable OutcomeReceiver<String, EthernetNetworkManagementException> callback) { |
James Mattis | 2854737 | 2022-02-02 14:32:40 -0800 | [diff] [blame] | 647 | Objects.requireNonNull(iface, "iface must be non-null"); |
Patrick Rohr | d6156a9 | 2022-03-17 19:19:21 +0000 | [diff] [blame] | 648 | final NetworkInterfaceOutcomeReceiver proxy = makeNetworkInterfaceOutcomeReceiver( |
| 649 | executor, callback); |
James Mattis | 407ea1d | 2021-12-14 18:23:30 -0800 | [diff] [blame] | 650 | try { |
| 651 | mService.disconnectNetwork(iface, proxy); |
| 652 | } catch (RemoteException e) { |
| 653 | throw e.rethrowFromSystemServer(); |
| 654 | } |
| 655 | } |
markchien | 5912ab6 | 2022-03-17 18:19:39 +0800 | [diff] [blame] | 656 | |
| 657 | /** |
| 658 | * Change ethernet setting. |
| 659 | * |
| 660 | * @param enabled enable or disable ethernet settings. |
| 661 | * |
| 662 | * @hide |
| 663 | */ |
| 664 | @RequiresPermission(anyOf = { |
| 665 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 666 | android.Manifest.permission.NETWORK_STACK, |
| 667 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 668 | @SystemApi(client = MODULE_LIBRARIES) |
| 669 | public void setEthernetEnabled(boolean enabled) { |
| 670 | try { |
| 671 | mService.setEthernetEnabled(enabled); |
| 672 | } catch (RemoteException e) { |
| 673 | throw e.rethrowFromSystemServer(); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Listen to changes in the state of ethernet. |
| 679 | * |
| 680 | * @param executor to run callbacks on. |
| 681 | * @param listener to listen ethernet state changed. |
| 682 | * |
| 683 | * @hide |
| 684 | */ |
| 685 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 686 | @SystemApi(client = MODULE_LIBRARIES) |
| 687 | public void addEthernetStateListener(@NonNull Executor executor, |
| 688 | @NonNull IntConsumer listener) { |
| 689 | Objects.requireNonNull(executor); |
| 690 | Objects.requireNonNull(listener); |
| 691 | synchronized (mListenerLock) { |
| 692 | maybeAddServiceListener(); |
| 693 | mEthernetStateListeners.add(new ListenerInfo<IntConsumer>(executor, listener)); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Removes a listener. |
| 699 | * |
| 700 | * @param listener to listen ethernet state changed. |
| 701 | * |
| 702 | * @hide |
| 703 | */ |
| 704 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 705 | @SystemApi(client = MODULE_LIBRARIES) |
| 706 | public void removeEthernetStateListener(@NonNull IntConsumer listener) { |
| 707 | Objects.requireNonNull(listener); |
| 708 | synchronized (mListenerLock) { |
| 709 | mEthernetStateListeners.removeIf(l -> l.listener == listener); |
| 710 | maybeRemoveServiceListener(); |
| 711 | } |
| 712 | } |
Xiao Ma | 1edd455 | 2022-03-17 05:56:09 +0000 | [diff] [blame] | 713 | |
| 714 | /** |
| 715 | * Returns an array of existing Ethernet interface names regardless whether the interface |
| 716 | * is available or not currently. |
| 717 | * @hide |
| 718 | */ |
| 719 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 720 | @SystemApi(client = MODULE_LIBRARIES) |
| 721 | @NonNull |
| 722 | public List<String> getInterfaceList() { |
| 723 | try { |
| 724 | return mService.getInterfaceList(); |
| 725 | } catch (RemoteException e) { |
| 726 | throw e.rethrowAsRuntimeException(); |
| 727 | } |
| 728 | } |
Lorenzo Colitti | 2edad7a | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 729 | } |