Add setEthernetEnabled and its listener API
Bug: 171872016 Test: atest EthernetServiceTests Merged-In: Iefd24d955572589c5bd3ca9a8139ea6e44979e8a Change-Id: Iefd24d955572589c5bd3ca9a8139ea6e44979e8a
This commit is contained in:
@@ -41,6 +41,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.function.IntConsumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that manages and configures Ethernet interfaces.
|
* A class that manages and configures Ethernet interfaces.
|
||||||
@@ -53,15 +54,31 @@ public class EthernetManager {
|
|||||||
private static final String TAG = "EthernetManager";
|
private static final String TAG = "EthernetManager";
|
||||||
|
|
||||||
private final IEthernetManager mService;
|
private final IEthernetManager mService;
|
||||||
@GuardedBy("mListeners")
|
@GuardedBy("mListenerLock")
|
||||||
private final ArrayList<ListenerInfo> mListeners = new ArrayList<>();
|
private final ArrayList<ListenerInfo<InterfaceStateListener>> mIfaceListeners =
|
||||||
|
new ArrayList<>();
|
||||||
|
@GuardedBy("mListenerLock")
|
||||||
|
private final ArrayList<ListenerInfo<IntConsumer>> mEthernetStateListeners =
|
||||||
|
new ArrayList<>();
|
||||||
|
final Object mListenerLock = new Object();
|
||||||
private final IEthernetServiceListener.Stub mServiceListener =
|
private final IEthernetServiceListener.Stub mServiceListener =
|
||||||
new IEthernetServiceListener.Stub() {
|
new IEthernetServiceListener.Stub() {
|
||||||
|
@Override
|
||||||
|
public void onEthernetStateChanged(int state) {
|
||||||
|
synchronized (mListenerLock) {
|
||||||
|
for (ListenerInfo<IntConsumer> li : mEthernetStateListeners) {
|
||||||
|
li.executor.execute(() -> {
|
||||||
|
li.listener.accept(state);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInterfaceStateChanged(String iface, int state, int role,
|
public void onInterfaceStateChanged(String iface, int state, int role,
|
||||||
IpConfiguration configuration) {
|
IpConfiguration configuration) {
|
||||||
synchronized (mListeners) {
|
synchronized (mListenerLock) {
|
||||||
for (ListenerInfo li : mListeners) {
|
for (ListenerInfo<InterfaceStateListener> li : mIfaceListeners) {
|
||||||
li.executor.execute(() ->
|
li.executor.execute(() ->
|
||||||
li.listener.onInterfaceStateChanged(iface, state, role,
|
li.listener.onInterfaceStateChanged(iface, state, role,
|
||||||
configuration));
|
configuration));
|
||||||
@@ -70,13 +87,29 @@ public class EthernetManager {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private static class ListenerInfo {
|
/**
|
||||||
|
* Indicates that Ethernet is disabled.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
|
public static final int ETHERNET_STATE_DISABLED = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that Ethernet is enabled.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
|
public static final int ETHERNET_STATE_ENABLED = 1;
|
||||||
|
|
||||||
|
private static class ListenerInfo<T> {
|
||||||
@NonNull
|
@NonNull
|
||||||
public final Executor executor;
|
public final Executor executor;
|
||||||
@NonNull
|
@NonNull
|
||||||
public final InterfaceStateListener listener;
|
public final T listener;
|
||||||
|
|
||||||
private ListenerInfo(@NonNull Executor executor, @NonNull InterfaceStateListener listener) {
|
private ListenerInfo(@NonNull Executor executor, @NonNull T listener) {
|
||||||
this.executor = executor;
|
this.executor = executor;
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
@@ -289,16 +322,22 @@ public class EthernetManager {
|
|||||||
if (listener == null || executor == null) {
|
if (listener == null || executor == null) {
|
||||||
throw new NullPointerException("listener and executor must not be null");
|
throw new NullPointerException("listener and executor must not be null");
|
||||||
}
|
}
|
||||||
synchronized (mListeners) {
|
synchronized (mListenerLock) {
|
||||||
mListeners.add(new ListenerInfo(executor, listener));
|
maybeAddServiceListener();
|
||||||
if (mListeners.size() == 1) {
|
mIfaceListeners.add(new ListenerInfo<InterfaceStateListener>(executor, listener));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GuardedBy("mListenerLock")
|
||||||
|
private void maybeAddServiceListener() {
|
||||||
|
if (!mIfaceListeners.isEmpty() || !mEthernetStateListeners.isEmpty()) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mService.addListener(mServiceListener);
|
mService.addListener(mServiceListener);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw e.rethrowFromSystemServer();
|
throw e.rethrowFromSystemServer();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -323,17 +362,22 @@ public class EthernetManager {
|
|||||||
@SystemApi(client = MODULE_LIBRARIES)
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
public void removeInterfaceStateListener(@NonNull InterfaceStateListener listener) {
|
public void removeInterfaceStateListener(@NonNull InterfaceStateListener listener) {
|
||||||
Objects.requireNonNull(listener);
|
Objects.requireNonNull(listener);
|
||||||
synchronized (mListeners) {
|
synchronized (mListenerLock) {
|
||||||
mListeners.removeIf(l -> l.listener == listener);
|
mIfaceListeners.removeIf(l -> l.listener == listener);
|
||||||
if (mListeners.isEmpty()) {
|
maybeRemoveServiceListener();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GuardedBy("mListenerLock")
|
||||||
|
private void maybeRemoveServiceListener() {
|
||||||
|
if (!mIfaceListeners.isEmpty() || !mEthernetStateListeners.isEmpty()) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mService.removeListener(mServiceListener);
|
mService.removeListener(mServiceListener);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw e.rethrowFromSystemServer();
|
throw e.rethrowFromSystemServer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a listener.
|
* Removes a listener.
|
||||||
@@ -611,4 +655,61 @@ public class EthernetManager {
|
|||||||
throw e.rethrowFromSystemServer();
|
throw e.rethrowFromSystemServer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change ethernet setting.
|
||||||
|
*
|
||||||
|
* @param enabled enable or disable ethernet settings.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@RequiresPermission(anyOf = {
|
||||||
|
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
||||||
|
android.Manifest.permission.NETWORK_STACK,
|
||||||
|
android.Manifest.permission.NETWORK_SETTINGS})
|
||||||
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
|
public void setEthernetEnabled(boolean enabled) {
|
||||||
|
try {
|
||||||
|
mService.setEthernetEnabled(enabled);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
throw e.rethrowFromSystemServer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen to changes in the state of ethernet.
|
||||||
|
*
|
||||||
|
* @param executor to run callbacks on.
|
||||||
|
* @param listener to listen ethernet state changed.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
|
||||||
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
|
public void addEthernetStateListener(@NonNull Executor executor,
|
||||||
|
@NonNull IntConsumer listener) {
|
||||||
|
Objects.requireNonNull(executor);
|
||||||
|
Objects.requireNonNull(listener);
|
||||||
|
synchronized (mListenerLock) {
|
||||||
|
maybeAddServiceListener();
|
||||||
|
mEthernetStateListeners.add(new ListenerInfo<IntConsumer>(executor, listener));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a listener.
|
||||||
|
*
|
||||||
|
* @param listener to listen ethernet state changed.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
|
||||||
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
|
public void removeEthernetStateListener(@NonNull IntConsumer listener) {
|
||||||
|
Objects.requireNonNull(listener);
|
||||||
|
synchronized (mListenerLock) {
|
||||||
|
mEthernetStateListeners.removeIf(l -> l.listener == listener);
|
||||||
|
maybeRemoveServiceListener();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,4 +43,5 @@ interface IEthernetManager
|
|||||||
in INetworkInterfaceOutcomeReceiver listener);
|
in INetworkInterfaceOutcomeReceiver listener);
|
||||||
void connectNetwork(String iface, in INetworkInterfaceOutcomeReceiver listener);
|
void connectNetwork(String iface, in INetworkInterfaceOutcomeReceiver listener);
|
||||||
void disconnectNetwork(String iface, in INetworkInterfaceOutcomeReceiver listener);
|
void disconnectNetwork(String iface, in INetworkInterfaceOutcomeReceiver listener);
|
||||||
|
void setEthernetEnabled(boolean enabled);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import android.net.IpConfiguration;
|
|||||||
/** @hide */
|
/** @hide */
|
||||||
oneway interface IEthernetServiceListener
|
oneway interface IEthernetServiceListener
|
||||||
{
|
{
|
||||||
|
void onEthernetStateChanged(int state);
|
||||||
void onInterfaceStateChanged(String iface, int state, int role,
|
void onInterfaceStateChanged(String iface, int state, int role,
|
||||||
in IpConfiguration configuration);
|
in IpConfiguration configuration);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user