Add global change ethernet state API

Provide a new API to enable or disable ethernet. Also have a listener
for call to check whether enable state is sucessful.

Bug: 171872016
Test: atest EthernetServiceTests

Change-Id: Iee4b48511ff668a2a7df90fd9bfe563d7ff23940
Merged-In: Iee4b48511ff668a2a7df90fd9bfe563d7ff23940
This commit is contained in:
markchien
2022-03-16 15:59:33 +08:00
committed by Mark Chien
parent 641bc0c4dc
commit c0939b76f0
5 changed files with 157 additions and 3 deletions

View File

@@ -183,7 +183,8 @@ public class EthernetNetworkFactory extends NetworkFactory {
* Returns an array of available interface names. The array is sorted: unrestricted interfaces
* goes first, then sorted by name.
*/
String[] getAvailableInterfaces(boolean includeRestricted) {
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
protected String[] getAvailableInterfaces(boolean includeRestricted) {
return mTrackingInterfaces.values()
.stream()
.filter(iface -> !iface.isRestricted() || includeRestricted)
@@ -195,7 +196,8 @@ public class EthernetNetworkFactory extends NetworkFactory {
.toArray(String[]::new);
}
void addInterface(@NonNull final String ifaceName, @NonNull final String hwAddress,
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
protected void addInterface(@NonNull final String ifaceName, @NonNull final String hwAddress,
@NonNull final IpConfiguration ipConfig,
@NonNull final NetworkCapabilities capabilities) {
if (mTrackingInterfaces.containsKey(ifaceName)) {
@@ -282,7 +284,8 @@ public class EthernetNetworkFactory extends NetworkFactory {
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET).build();
}
void removeInterface(String interfaceName) {
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
protected void removeInterface(String interfaceName) {
NetworkInterfaceState iface = mTrackingInterfaces.remove(interfaceName);
if (iface != null) {
iface.maybeSendNetworkManagementCallbackForAbort();

View File

@@ -281,4 +281,12 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
mTracker.disconnectNetwork(iface, listener);
}
@Override
public void setEthernetEnabled(boolean enabled) {
PermissionUtils.enforceNetworkStackPermissionOr(mContext,
android.Manifest.permission.NETWORK_SETTINGS);
mTracker.setEthernetEnabled(enabled);
}
}

View File

@@ -16,6 +16,8 @@
package com.android.server.ethernet;
import static android.net.EthernetManager.ETHERNET_STATE_DISABLED;
import static android.net.EthernetManager.ETHERNET_STATE_ENABLED;
import static android.net.TestNetworkManager.TEST_TAP_PREFIX;
import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE;
@@ -119,6 +121,8 @@ public class EthernetTracker {
private boolean mTetheredInterfaceWasAvailable = false;
private volatile IpConfiguration mIpConfigForDefaultInterface;
private int mEthernetState = ETHERNET_STATE_ENABLED;
private class TetheredInterfaceRequestList extends
RemoteCallbackList<ITetheredInterfaceCallback> {
@Override
@@ -355,6 +359,8 @@ public class EthernetTracker {
for (String iface : getInterfaces(canUseRestrictedNetworks)) {
unicastInterfaceStateChange(listener, iface);
}
unicastEthernetStateChange(listener, mEthernetState);
});
}
@@ -825,6 +831,53 @@ public class EthernetTracker {
}
}
@VisibleForTesting(visibility = PACKAGE)
protected void setEthernetEnabled(boolean enabled) {
mHandler.post(() -> {
int newState = enabled ? ETHERNET_STATE_ENABLED : ETHERNET_STATE_DISABLED;
if (mEthernetState == newState) return;
mEthernetState = newState;
if (enabled) {
trackAvailableInterfaces();
} else {
// TODO: maybe also disable server mode interface as well.
untrackFactoryInterfaces();
}
broadcastEthernetStateChange(mEthernetState);
});
}
private void untrackFactoryInterfaces() {
for (String iface : mFactory.getAvailableInterfaces(true /* includeRestricted */)) {
stopTrackingInterface(iface);
}
}
private void unicastEthernetStateChange(@NonNull IEthernetServiceListener listener,
int state) {
ensureRunningOnEthernetServiceThread();
try {
listener.onEthernetStateChanged(state);
} catch (RemoteException e) {
// Do nothing here.
}
}
private void broadcastEthernetStateChange(int state) {
ensureRunningOnEthernetServiceThread();
final int n = mListeners.beginBroadcast();
for (int i = 0; i < n; i++) {
try {
mListeners.getBroadcastItem(i).onEthernetStateChanged(state);
} catch (RemoteException e) {
// Do nothing here.
}
}
mListeners.finishBroadcast();
}
void dump(FileDescriptor fd, IndentingPrintWriter pw, String[] args) {
postAndWaitForRunnable(() -> {
pw.println(getClass().getSimpleName());