blob: d975017f9c8e84205a022087f820abc928149116 [file] [log] [blame]
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -07001/*
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
17package android.net;
18
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +090019import android.annotation.NonNull;
Automerger Merge Worker2398e9b2020-03-09 08:40:23 +000020import android.annotation.RequiresPermission;
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +090021import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060022import android.annotation.SystemService;
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +090023import android.annotation.TestApi;
Artur Satayev26958002019-12-10 17:47:52 +000024import android.compat.annotation.UnsupportedAppUsage;
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -070025import android.content.Context;
Jaewan Kimd109a7c2014-10-20 12:04:13 +090026import android.os.Handler;
27import android.os.Message;
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -070028import android.os.RemoteException;
29
Jaewan Kimd109a7c2014-10-20 12:04:13 +090030import java.util.ArrayList;
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +090031import java.util.Objects;
Automerger Merge Worker06fe92d2020-02-28 03:42:44 +000032import java.util.concurrent.Executor;
Jaewan Kimd109a7c2014-10-20 12:04:13 +090033
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -070034/**
35 * A class representing the IP configuration of the Ethernet network.
36 *
37 * @hide
38 */
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +090039@SystemApi
40@TestApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060041@SystemService(Context.ETHERNET_SERVICE)
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -070042public class EthernetManager {
43 private static final String TAG = "EthernetManager";
Jaewan Kimd109a7c2014-10-20 12:04:13 +090044 private static final int MSG_AVAILABILITY_CHANGED = 1000;
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -070045
46 private final Context mContext;
47 private final IEthernetManager mService;
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +090048 private final Handler mHandler = new Handler(ConnectivityThread.getInstanceLooper()) {
Jaewan Kimd109a7c2014-10-20 12:04:13 +090049 @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 Maltsevc07a96d2017-10-31 15:34:16 -070054 listener.onAvailabilityChanged((String) msg.obj, isAvailable);
Jaewan Kimd109a7c2014-10-20 12:04:13 +090055 }
56 }
57 }
58 };
Pavel Maltsevc07a96d2017-10-31 15:34:16 -070059 private final ArrayList<Listener> mListeners = new ArrayList<>();
Jaewan Kimd109a7c2014-10-20 12:04:13 +090060 private final IEthernetServiceListener.Stub mServiceListener =
61 new IEthernetServiceListener.Stub() {
62 @Override
Pavel Maltsevc07a96d2017-10-31 15:34:16 -070063 public void onAvailabilityChanged(String iface, boolean isAvailable) {
Jaewan Kimd109a7c2014-10-20 12:04:13 +090064 mHandler.obtainMessage(
Pavel Maltsevc07a96d2017-10-31 15:34:16 -070065 MSG_AVAILABILITY_CHANGED, isAvailable ? 1 : 0, 0, iface).sendToTarget();
Jaewan Kimd109a7c2014-10-20 12:04:13 +090066 }
67 };
68
69 /**
70 * A listener interface to receive notification on changes in Ethernet.
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +090071 * @hide
Jaewan Kimd109a7c2014-10-20 12:04:13 +090072 */
73 public interface Listener {
74 /**
75 * Called when Ethernet port's availability is changed.
Pavel Maltsevc07a96d2017-10-31 15:34:16 -070076 * @param iface Ethernet interface name
77 * @param isAvailable {@code true} if Ethernet port exists.
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +090078 * @hide
Jaewan Kimd109a7c2014-10-20 12:04:13 +090079 */
Mathew Inwood53f089f2018-08-08 14:44:44 +010080 @UnsupportedAppUsage
Pavel Maltsevc07a96d2017-10-31 15:34:16 -070081 void onAvailabilityChanged(String iface, boolean isAvailable);
Jaewan Kimd109a7c2014-10-20 12:04:13 +090082 }
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -070083
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 VAN84229e02020-01-24 22:57:09 +090089 * @hide
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -070090 */
91 public EthernetManager(Context context, IEthernetManager service) {
92 mContext = context;
93 mService = service;
94 }
95
96 /**
Lorenzo Colitti9c1ccc52014-05-22 11:51:27 -070097 * Get Ethernet configuration.
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -070098 * @return the Ethernet Configuration, contained in {@link IpConfiguration}.
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +090099 * @hide
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -0700100 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100101 @UnsupportedAppUsage
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700102 public IpConfiguration getConfiguration(String iface) {
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -0700103 try {
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700104 return mService.getConfiguration(iface);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700105 } catch (RemoteException e) {
106 throw e.rethrowFromSystemServer();
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -0700107 }
108 }
109
110 /**
Lorenzo Colitti9c1ccc52014-05-22 11:51:27 -0700111 * Set Ethernet configuration.
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +0900112 * @hide
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -0700113 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100114 @UnsupportedAppUsage
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700115 public void setConfiguration(String iface, IpConfiguration config) {
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -0700116 try {
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700117 mService.setConfiguration(iface, config);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700118 } catch (RemoteException e) {
119 throw e.rethrowFromSystemServer();
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900120 }
121 }
122
123 /**
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700124 * Indicates whether the system currently has one or more Ethernet interfaces.
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +0900125 * @hide
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900126 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100127 @UnsupportedAppUsage
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900128 public boolean isAvailable() {
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700129 return getAvailableInterfaces().length > 0;
130 }
131
132 /**
133 * Indicates whether the system has given interface.
134 *
135 * @param iface Ethernet interface name
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +0900136 * @hide
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700137 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100138 @UnsupportedAppUsage
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700139 public boolean isAvailable(String iface) {
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900140 try {
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700141 return mService.isAvailable(iface);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700142 } catch (RemoteException e) {
143 throw e.rethrowFromSystemServer();
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900144 }
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 VAN84229e02020-01-24 22:57:09 +0900151 * @hide
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900152 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100153 @UnsupportedAppUsage
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900154 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 Sharkeyc53962d2016-03-01 19:27:23 -0700162 } catch (RemoteException e) {
163 throw e.rethrowFromSystemServer();
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900164 }
165 }
166 }
167
168 /**
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700169 * Returns an array of available Ethernet interface names.
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +0900170 * @hide
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700171 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100172 @UnsupportedAppUsage
Pavel Maltsevc07a96d2017-10-31 15:34:16 -0700173 public String[] getAvailableInterfaces() {
174 try {
175 return mService.getAvailableInterfaces();
176 } catch (RemoteException e) {
177 throw e.rethrowAsRuntimeException();
178 }
179 }
180
181 /**
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900182 * Removes a listener.
183 * @param listener A {@link Listener} to remove.
184 * @throws IllegalArgumentException If the listener is null.
Remi NGUYEN VAN84229e02020-01-24 22:57:09 +0900185 * @hide
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900186 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100187 @UnsupportedAppUsage
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900188 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 Sharkeyc53962d2016-03-01 19:27:23 -0700196 } catch (RemoteException e) {
197 throw e.rethrowFromSystemServer();
Jaewan Kimd109a7c2014-10-20 12:04:13 +0900198 }
Lorenzo Colitti4e5aa2c2014-05-21 16:23:43 -0700199 }
200 }
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +0900201
202 /**
Lorenzo Colitti6fba2a82020-03-19 11:46:55 +0000203 * 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 Colitti30a8f7e2020-01-24 19:29:17 +0900218 * 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 Worker2398e9b2020-03-09 08:40:23 +0000267 @RequiresPermission(anyOf = {
268 android.Manifest.permission.NETWORK_STACK,
269 android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK
270 })
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +0900271 @NonNull
Automerger Merge Worker06fe92d2020-02-28 03:42:44 +0000272 public TetheredInterfaceRequest requestTetheredInterface(@NonNull final Executor executor,
273 @NonNull final TetheredInterfaceCallback callback) {
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +0900274 Objects.requireNonNull(callback, "Callback must be non-null");
Automerger Merge Worker06fe92d2020-02-28 03:42:44 +0000275 Objects.requireNonNull(executor, "Executor must be non-null");
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +0900276 final ITetheredInterfaceCallback cbInternal = new ITetheredInterfaceCallback.Stub() {
277 @Override
278 public void onAvailable(String iface) {
Automerger Merge Worker06fe92d2020-02-28 03:42:44 +0000279 executor.execute(() -> callback.onAvailable(iface));
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +0900280 }
281
282 @Override
283 public void onUnavailable() {
Automerger Merge Worker06fe92d2020-02-28 03:42:44 +0000284 executor.execute(() -> callback.onUnavailable());
Lorenzo Colitti30a8f7e2020-01-24 19:29:17 +0900285 }
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 Colitti4e5aa2c2014-05-21 16:23:43 -0700295}