Lorenzo Colitti | 4e5aa2c | 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 | |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Automerger Merge Worker | 2398e9b | 2020-03-09 08:40:23 +0000 | [diff] [blame] | 20 | import android.annotation.RequiresPermission; |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 22 | import android.annotation.SystemService; |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 23 | import android.annotation.TestApi; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 24 | import android.compat.annotation.UnsupportedAppUsage; |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.Message; |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 30 | import java.util.ArrayList; |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 31 | import java.util.Objects; |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 32 | import java.util.concurrent.Executor; |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 33 | |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 34 | /** |
| 35 | * A class representing the IP configuration of the Ethernet network. |
| 36 | * |
| 37 | * @hide |
| 38 | */ |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 39 | @SystemApi |
| 40 | @TestApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 41 | @SystemService(Context.ETHERNET_SERVICE) |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 42 | public class EthernetManager { |
| 43 | private static final String TAG = "EthernetManager"; |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 44 | private static final int MSG_AVAILABILITY_CHANGED = 1000; |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 45 | |
| 46 | private final Context mContext; |
| 47 | private final IEthernetManager mService; |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 48 | private final Handler mHandler = new Handler(ConnectivityThread.getInstanceLooper()) { |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 49 | @Override |
| 50 | public void handleMessage(Message msg) { |
| 51 | if (msg.what == MSG_AVAILABILITY_CHANGED) { |
| 52 | boolean isAvailable = (msg.arg1 == 1); |
| 53 | for (Listener listener : mListeners) { |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 54 | listener.onAvailabilityChanged((String) msg.obj, isAvailable); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |
| 58 | }; |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 59 | private final ArrayList<Listener> mListeners = new ArrayList<>(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 60 | private final IEthernetServiceListener.Stub mServiceListener = |
| 61 | new IEthernetServiceListener.Stub() { |
| 62 | @Override |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 63 | public void onAvailabilityChanged(String iface, boolean isAvailable) { |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 64 | mHandler.obtainMessage( |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 65 | MSG_AVAILABILITY_CHANGED, isAvailable ? 1 : 0, 0, iface).sendToTarget(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 66 | } |
| 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * A listener interface to receive notification on changes in Ethernet. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 71 | * @hide |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 72 | */ |
| 73 | public interface Listener { |
| 74 | /** |
| 75 | * Called when Ethernet port's availability is changed. |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 76 | * @param iface Ethernet interface name |
| 77 | * @param isAvailable {@code true} if Ethernet port exists. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 78 | * @hide |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 79 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 80 | @UnsupportedAppUsage |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 81 | void onAvailabilityChanged(String iface, boolean isAvailable); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 82 | } |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Create a new EthernetManager instance. |
| 86 | * Applications will almost always want to use |
| 87 | * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve |
| 88 | * the standard {@link android.content.Context#ETHERNET_SERVICE Context.ETHERNET_SERVICE}. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 89 | * @hide |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 90 | */ |
| 91 | public EthernetManager(Context context, IEthernetManager service) { |
| 92 | mContext = context; |
| 93 | mService = service; |
| 94 | } |
| 95 | |
| 96 | /** |
Lorenzo Colitti | 9c1ccc5 | 2014-05-22 11:51:27 -0700 | [diff] [blame] | 97 | * Get Ethernet configuration. |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 98 | * @return the Ethernet Configuration, contained in {@link IpConfiguration}. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 99 | * @hide |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 100 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 101 | @UnsupportedAppUsage |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 102 | public IpConfiguration getConfiguration(String iface) { |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 103 | try { |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 104 | return mService.getConfiguration(iface); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 105 | } catch (RemoteException e) { |
| 106 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
Lorenzo Colitti | 9c1ccc5 | 2014-05-22 11:51:27 -0700 | [diff] [blame] | 111 | * Set Ethernet configuration. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 112 | * @hide |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 113 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 114 | @UnsupportedAppUsage |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 115 | public void setConfiguration(String iface, IpConfiguration config) { |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 116 | try { |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 117 | mService.setConfiguration(iface, config); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 118 | } catch (RemoteException e) { |
| 119 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 124 | * Indicates whether the system currently has one or more Ethernet interfaces. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 125 | * @hide |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 126 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 127 | @UnsupportedAppUsage |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 128 | public boolean isAvailable() { |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 129 | return getAvailableInterfaces().length > 0; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Indicates whether the system has given interface. |
| 134 | * |
| 135 | * @param iface Ethernet interface name |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 136 | * @hide |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 137 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 138 | @UnsupportedAppUsage |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 139 | public boolean isAvailable(String iface) { |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 140 | try { |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 141 | return mService.isAvailable(iface); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 142 | } catch (RemoteException e) { |
| 143 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Adds a listener. |
| 149 | * @param listener A {@link Listener} to add. |
| 150 | * @throws IllegalArgumentException If the listener is null. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 151 | * @hide |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 152 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 153 | @UnsupportedAppUsage |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 154 | public void addListener(Listener listener) { |
| 155 | if (listener == null) { |
| 156 | throw new IllegalArgumentException("listener must not be null"); |
| 157 | } |
| 158 | mListeners.add(listener); |
| 159 | if (mListeners.size() == 1) { |
| 160 | try { |
| 161 | mService.addListener(mServiceListener); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 162 | } catch (RemoteException e) { |
| 163 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 169 | * Returns an array of available Ethernet interface names. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 170 | * @hide |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 171 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 172 | @UnsupportedAppUsage |
Pavel Maltsev | c07a96d | 2017-10-31 15:34:16 -0700 | [diff] [blame] | 173 | public String[] getAvailableInterfaces() { |
| 174 | try { |
| 175 | return mService.getAvailableInterfaces(); |
| 176 | } catch (RemoteException e) { |
| 177 | throw e.rethrowAsRuntimeException(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 182 | * Removes a listener. |
| 183 | * @param listener A {@link Listener} to remove. |
| 184 | * @throws IllegalArgumentException If the listener is null. |
Remi NGUYEN VAN | 84229e0 | 2020-01-24 22:57:09 +0900 | [diff] [blame] | 185 | * @hide |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 186 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 187 | @UnsupportedAppUsage |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 188 | public void removeListener(Listener listener) { |
| 189 | if (listener == null) { |
| 190 | throw new IllegalArgumentException("listener must not be null"); |
| 191 | } |
| 192 | mListeners.remove(listener); |
| 193 | if (mListeners.isEmpty()) { |
| 194 | try { |
| 195 | mService.removeListener(mServiceListener); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 196 | } catch (RemoteException e) { |
| 197 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 198 | } |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 201 | |
| 202 | /** |
Lorenzo Colitti | 6fba2a8 | 2020-03-19 11:46:55 +0000 | [diff] [blame] | 203 | * Whether to treat interfaces created by {@link TestNetworkManager#createTapInterface} |
| 204 | * as Ethernet interfaces. The effects of this method apply to any test interfaces that are |
| 205 | * already present on the system. |
| 206 | * @hide |
| 207 | */ |
| 208 | @TestApi |
| 209 | public void setIncludeTestInterfaces(boolean include) { |
| 210 | try { |
| 211 | mService.setIncludeTestInterfaces(include); |
| 212 | } catch (RemoteException e) { |
| 213 | throw e.rethrowFromSystemServer(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 218 | * A request for a tethered interface. |
| 219 | */ |
| 220 | public static class TetheredInterfaceRequest { |
| 221 | private final IEthernetManager mService; |
| 222 | private final ITetheredInterfaceCallback mCb; |
| 223 | |
| 224 | private TetheredInterfaceRequest(@NonNull IEthernetManager service, |
| 225 | @NonNull ITetheredInterfaceCallback cb) { |
| 226 | this.mService = service; |
| 227 | this.mCb = cb; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Release the request, causing the interface to revert back from tethering mode if there |
| 232 | * is no other requestor. |
| 233 | */ |
| 234 | public void release() { |
| 235 | try { |
| 236 | mService.releaseTetheredInterface(mCb); |
| 237 | } catch (RemoteException e) { |
| 238 | e.rethrowFromSystemServer(); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Callback for {@link #requestTetheredInterface(TetheredInterfaceCallback)}. |
| 245 | */ |
| 246 | public interface TetheredInterfaceCallback { |
| 247 | /** |
| 248 | * Called when the tethered interface is available. |
| 249 | * @param iface The name of the interface. |
| 250 | */ |
| 251 | void onAvailable(@NonNull String iface); |
| 252 | |
| 253 | /** |
| 254 | * Called when the tethered interface is now unavailable. |
| 255 | */ |
| 256 | void onUnavailable(); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Request a tethered interface in tethering mode. |
| 261 | * |
| 262 | * <p>When this method is called and there is at least one ethernet interface available, the |
| 263 | * system will designate one to act as a tethered interface. If there is already a tethered |
| 264 | * interface, the existing interface will be used. |
| 265 | * @param callback A callback to be called once the request has been fulfilled. |
| 266 | */ |
Automerger Merge Worker | 2398e9b | 2020-03-09 08:40:23 +0000 | [diff] [blame] | 267 | @RequiresPermission(anyOf = { |
| 268 | android.Manifest.permission.NETWORK_STACK, |
| 269 | android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 270 | }) |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 271 | @NonNull |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 272 | public TetheredInterfaceRequest requestTetheredInterface(@NonNull final Executor executor, |
| 273 | @NonNull final TetheredInterfaceCallback callback) { |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 274 | Objects.requireNonNull(callback, "Callback must be non-null"); |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 275 | Objects.requireNonNull(executor, "Executor must be non-null"); |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 276 | final ITetheredInterfaceCallback cbInternal = new ITetheredInterfaceCallback.Stub() { |
| 277 | @Override |
| 278 | public void onAvailable(String iface) { |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 279 | executor.execute(() -> callback.onAvailable(iface)); |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void onUnavailable() { |
Automerger Merge Worker | 06fe92d | 2020-02-28 03:42:44 +0000 | [diff] [blame] | 284 | executor.execute(() -> callback.onUnavailable()); |
Lorenzo Colitti | 30a8f7e | 2020-01-24 19:29:17 +0900 | [diff] [blame] | 285 | } |
| 286 | }; |
| 287 | |
| 288 | try { |
| 289 | mService.requestTetheredInterface(cbInternal); |
| 290 | } catch (RemoteException e) { |
| 291 | throw e.rethrowFromSystemServer(); |
| 292 | } |
| 293 | return new TetheredInterfaceRequest(mService, cbInternal); |
| 294 | } |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 295 | } |