Merge "Register ethernet listeners on specific Executors"
This commit is contained in:
@@ -24,10 +24,11 @@ import android.annotation.TestApi;
|
|||||||
import android.compat.annotation.UnsupportedAppUsage;
|
import android.compat.annotation.UnsupportedAppUsage;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.Message;
|
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
|
||||||
|
import com.android.internal.annotations.GuardedBy;
|
||||||
|
import com.android.internal.os.BackgroundThread;
|
||||||
|
|
||||||
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;
|
||||||
@@ -41,31 +42,35 @@ import java.util.concurrent.Executor;
|
|||||||
@SystemService(Context.ETHERNET_SERVICE)
|
@SystemService(Context.ETHERNET_SERVICE)
|
||||||
public class EthernetManager {
|
public class EthernetManager {
|
||||||
private static final String TAG = "EthernetManager";
|
private static final String TAG = "EthernetManager";
|
||||||
private static final int MSG_AVAILABILITY_CHANGED = 1000;
|
|
||||||
|
|
||||||
private final Context mContext;
|
|
||||||
private final IEthernetManager mService;
|
private final IEthernetManager mService;
|
||||||
private final Handler mHandler = new Handler(ConnectivityThread.getInstanceLooper()) {
|
@GuardedBy("mListeners")
|
||||||
@Override
|
private final ArrayList<ListenerInfo> mListeners = new ArrayList<>();
|
||||||
public void handleMessage(Message msg) {
|
|
||||||
if (msg.what == MSG_AVAILABILITY_CHANGED) {
|
|
||||||
boolean isAvailable = (msg.arg1 == 1);
|
|
||||||
for (Listener listener : mListeners) {
|
|
||||||
listener.onAvailabilityChanged((String) msg.obj, isAvailable);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
private final ArrayList<Listener> mListeners = new ArrayList<>();
|
|
||||||
private final IEthernetServiceListener.Stub mServiceListener =
|
private final IEthernetServiceListener.Stub mServiceListener =
|
||||||
new IEthernetServiceListener.Stub() {
|
new IEthernetServiceListener.Stub() {
|
||||||
@Override
|
@Override
|
||||||
public void onAvailabilityChanged(String iface, boolean isAvailable) {
|
public void onAvailabilityChanged(String iface, boolean isAvailable) {
|
||||||
mHandler.obtainMessage(
|
synchronized (mListeners) {
|
||||||
MSG_AVAILABILITY_CHANGED, isAvailable ? 1 : 0, 0, iface).sendToTarget();
|
for (ListenerInfo li : mListeners) {
|
||||||
|
li.executor.execute(() ->
|
||||||
|
li.listener.onAvailabilityChanged(iface, isAvailable));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static class ListenerInfo {
|
||||||
|
@NonNull
|
||||||
|
public final Executor executor;
|
||||||
|
@NonNull
|
||||||
|
public final Listener listener;
|
||||||
|
|
||||||
|
private ListenerInfo(@NonNull Executor executor, @NonNull Listener listener) {
|
||||||
|
this.executor = executor;
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A listener interface to receive notification on changes in Ethernet.
|
* A listener interface to receive notification on changes in Ethernet.
|
||||||
* @hide
|
* @hide
|
||||||
@@ -89,7 +94,6 @@ public class EthernetManager {
|
|||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
public EthernetManager(Context context, IEthernetManager service) {
|
public EthernetManager(Context context, IEthernetManager service) {
|
||||||
mContext = context;
|
|
||||||
mService = service;
|
mService = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,21 +150,38 @@ public class EthernetManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a listener.
|
* Adds a listener.
|
||||||
|
*
|
||||||
|
* Consider using {@link #addListener(Listener, Executor)} instead: this method uses a default
|
||||||
|
* executor that may have higher latency than a provided executor.
|
||||||
* @param listener A {@link Listener} to add.
|
* @param listener A {@link Listener} to add.
|
||||||
* @throws IllegalArgumentException If the listener is null.
|
* @throws IllegalArgumentException If the listener is null.
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
|
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
|
||||||
public void addListener(Listener listener) {
|
public void addListener(@NonNull Listener listener) {
|
||||||
if (listener == null) {
|
addListener(listener, BackgroundThread.getExecutor());
|
||||||
throw new IllegalArgumentException("listener must not be null");
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a listener.
|
||||||
|
* @param listener A {@link Listener} to add.
|
||||||
|
* @param executor Executor to run callbacks on.
|
||||||
|
* @throws IllegalArgumentException If the listener or executor is null.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
|
||||||
|
public void addListener(@NonNull Listener listener, @NonNull Executor executor) {
|
||||||
|
if (listener == null || executor == null) {
|
||||||
|
throw new NullPointerException("listener and executor must not be null");
|
||||||
}
|
}
|
||||||
mListeners.add(listener);
|
synchronized (mListeners) {
|
||||||
if (mListeners.size() == 1) {
|
mListeners.add(new ListenerInfo(executor, listener));
|
||||||
try {
|
if (mListeners.size() == 1) {
|
||||||
mService.addListener(mServiceListener);
|
try {
|
||||||
} catch (RemoteException e) {
|
mService.addListener(mServiceListener);
|
||||||
throw e.rethrowFromSystemServer();
|
} catch (RemoteException e) {
|
||||||
|
throw e.rethrowFromSystemServer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,16 +206,18 @@ public class EthernetManager {
|
|||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
|
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
|
||||||
public void removeListener(Listener listener) {
|
public void removeListener(@NonNull Listener listener) {
|
||||||
if (listener == null) {
|
if (listener == null) {
|
||||||
throw new IllegalArgumentException("listener must not be null");
|
throw new IllegalArgumentException("listener must not be null");
|
||||||
}
|
}
|
||||||
mListeners.remove(listener);
|
synchronized (mListeners) {
|
||||||
if (mListeners.isEmpty()) {
|
mListeners.removeIf(l -> l.listener == listener);
|
||||||
try {
|
if (mListeners.isEmpty()) {
|
||||||
mService.removeListener(mServiceListener);
|
try {
|
||||||
} catch (RemoteException e) {
|
mService.removeListener(mServiceListener);
|
||||||
throw e.rethrowFromSystemServer();
|
} catch (RemoteException e) {
|
||||||
|
throw e.rethrowFromSystemServer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user