Merge "Remove callback when UnregisterCallback is called" into udc-dev

This commit is contained in:
Andrew Sapperstein
2023-02-25 13:50:35 +00:00
committed by Android (Google) Code Review
2 changed files with 54 additions and 98 deletions

View File

@@ -258,7 +258,9 @@ public class SharedConnectivityManager {
} }
/** /**
* Registers a callback for receiving updates to the list of Tether Networks and Known Networks. * Registers a callback for receiving updates to the list of Tether Networks, Known Networks,
* shared connectivity settings state, tether network connection status and known network
* connection status.
* The {@link SharedConnectivityClientCallback#onRegisterCallbackFailed} will be called if the * The {@link SharedConnectivityClientCallback#onRegisterCallbackFailed} will be called if the
* registration failed. * registration failed.
* *

View File

@@ -36,14 +36,12 @@ import android.net.wifi.sharedconnectivity.app.TetherNetworkConnectionStatus;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
@@ -62,11 +60,8 @@ public abstract class SharedConnectivityService extends Service {
private static final boolean DEBUG = true; private static final boolean DEBUG = true;
private Handler mHandler; private Handler mHandler;
private final List<ISharedConnectivityCallback> mCallbacks = new ArrayList<>(); private final RemoteCallbackList<ISharedConnectivityCallback> mRemoteCallbackList =
// Used to find DeathRecipient when unregistering a callback to call unlinkToDeath. new RemoteCallbackList<>();
private final Map<ISharedConnectivityCallback, DeathRecipient> mDeathRecipientMap =
new HashMap<>();
private List<TetherNetwork> mTetherNetworks = Collections.emptyList(); private List<TetherNetwork> mTetherNetworks = Collections.emptyList();
private List<KnownNetwork> mKnownNetworks = Collections.emptyList(); private List<KnownNetwork> mKnownNetworks = Collections.emptyList();
private SharedConnectivitySettingsState mSettingsState = private SharedConnectivitySettingsState mSettingsState =
@@ -81,20 +76,6 @@ public abstract class SharedConnectivityService extends Service {
.setStatus(KnownNetworkConnectionStatus.CONNECTION_STATUS_UNKNOWN) .setStatus(KnownNetworkConnectionStatus.CONNECTION_STATUS_UNKNOWN)
.setExtras(Bundle.EMPTY).build(); .setExtras(Bundle.EMPTY).build();
private final class DeathRecipient implements IBinder.DeathRecipient {
ISharedConnectivityCallback mCallback;
DeathRecipient(ISharedConnectivityCallback callback) {
mCallback = callback;
}
@Override
public void binderDied() {
mCallbacks.remove(mCallback);
mDeathRecipientMap.remove(mCallback);
}
}
@Override @Override
@Nullable @Nullable
public final IBinder onBind(@NonNull Intent intent) { public final IBinder onBind(@NonNull Intent intent) {
@@ -194,76 +175,13 @@ public abstract class SharedConnectivityService extends Service {
public void onBind() {} public void onBind() {}
private void onRegisterCallback(ISharedConnectivityCallback callback) { private void onRegisterCallback(ISharedConnectivityCallback callback) {
DeathRecipient deathRecipient = new DeathRecipient(callback); mRemoteCallbackList.register(callback);
try {
callback.asBinder().linkToDeath(deathRecipient, 0);
mCallbacks.add(callback);
mDeathRecipientMap.put(callback, deathRecipient);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in registerCallback", e);
}
} }
private void onUnregisterCallback(ISharedConnectivityCallback callback) { private void onUnregisterCallback(ISharedConnectivityCallback callback) {
DeathRecipient deathRecipient = mDeathRecipientMap.get(callback); mRemoteCallbackList.unregister(callback);
if (deathRecipient != null) {
callback.asBinder().unlinkToDeath(deathRecipient, 0);
mDeathRecipientMap.remove(callback);
}
mCallbacks.remove(callback);
} }
private boolean notifyTetherNetworkUpdate(ISharedConnectivityCallback callback) {
try {
callback.onTetherNetworksUpdated(mTetherNetworks);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in notifyTetherNetworkUpdate", e);
return false;
}
return true;
}
private boolean notifyKnownNetworkUpdate(ISharedConnectivityCallback callback) {
try {
callback.onKnownNetworksUpdated(mKnownNetworks);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in notifyKnownNetworkUpdate", e);
return false;
}
return true;
}
private boolean notifySettingsStateUpdate(ISharedConnectivityCallback callback) {
try {
callback.onSharedConnectivitySettingsChanged(mSettingsState);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in notifySettingsStateUpdate", e);
return false;
}
return true;
}
private boolean notifyTetherNetworkConnectionStatusChanged(
ISharedConnectivityCallback callback) {
try {
callback.onTetherNetworkConnectionStatusChanged(mTetherNetworkConnectionStatus);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in notifyTetherNetworkConnectionStatusChanged", e);
return false;
}
return true;
}
private boolean notifyKnownNetworkConnectionStatusChanged(
ISharedConnectivityCallback callback) {
try {
callback.onKnownNetworkConnectionStatusChanged(mKnownNetworkConnectionStatus);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in notifyKnownNetworkConnectionStatusChanged", e);
return false;
}
return true;
}
/** /**
* Implementing application should call this method to provide an up-to-date list of Tether * Implementing application should call this method to provide an up-to-date list of Tether
* Networks to be displayed to the user. * Networks to be displayed to the user.
@@ -276,10 +194,16 @@ public abstract class SharedConnectivityService extends Service {
public final void setTetherNetworks(@NonNull List<TetherNetwork> networks) { public final void setTetherNetworks(@NonNull List<TetherNetwork> networks) {
mTetherNetworks = networks; mTetherNetworks = networks;
for (ISharedConnectivityCallback callback:mCallbacks) { int count = mRemoteCallbackList.beginBroadcast();
notifyTetherNetworkUpdate(callback); for (int i = 0; i < count; i++) {
try {
mRemoteCallbackList.getBroadcastItem(i).onTetherNetworksUpdated(mTetherNetworks);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in setTetherNetworks", e);
} }
} }
mRemoteCallbackList.finishBroadcast();
}
/** /**
* Implementing application should call this method to provide an up-to-date list of Known * Implementing application should call this method to provide an up-to-date list of Known
@@ -293,10 +217,16 @@ public abstract class SharedConnectivityService extends Service {
public final void setKnownNetworks(@NonNull List<KnownNetwork> networks) { public final void setKnownNetworks(@NonNull List<KnownNetwork> networks) {
mKnownNetworks = networks; mKnownNetworks = networks;
for (ISharedConnectivityCallback callback:mCallbacks) { int count = mRemoteCallbackList.beginBroadcast();
notifyKnownNetworkUpdate(callback); for (int i = 0; i < count; i++) {
try {
mRemoteCallbackList.getBroadcastItem(i).onKnownNetworksUpdated(mKnownNetworks);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in setKnownNetworks", e);
} }
} }
mRemoteCallbackList.finishBroadcast();
}
/** /**
* Implementing application should call this method to provide an up-to-date state of Shared * Implementing application should call this method to provide an up-to-date state of Shared
@@ -311,10 +241,17 @@ public abstract class SharedConnectivityService extends Service {
public final void setSettingsState(@NonNull SharedConnectivitySettingsState settingsState) { public final void setSettingsState(@NonNull SharedConnectivitySettingsState settingsState) {
mSettingsState = settingsState; mSettingsState = settingsState;
for (ISharedConnectivityCallback callback:mCallbacks) { int count = mRemoteCallbackList.beginBroadcast();
notifySettingsStateUpdate(callback); for (int i = 0; i < count; i++) {
try {
mRemoteCallbackList.getBroadcastItem(i).onSharedConnectivitySettingsChanged(
mSettingsState);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in setSettingsState", e);
} }
} }
mRemoteCallbackList.finishBroadcast();
}
/** /**
* Implementing application should call this method to provide an up-to-date status of enabling * Implementing application should call this method to provide an up-to-date status of enabling
@@ -326,10 +263,19 @@ public abstract class SharedConnectivityService extends Service {
public final void updateTetherNetworkConnectionStatus( public final void updateTetherNetworkConnectionStatus(
@NonNull TetherNetworkConnectionStatus status) { @NonNull TetherNetworkConnectionStatus status) {
mTetherNetworkConnectionStatus = status; mTetherNetworkConnectionStatus = status;
for (ISharedConnectivityCallback callback:mCallbacks) {
notifyTetherNetworkConnectionStatusChanged(callback); int count = mRemoteCallbackList.beginBroadcast();
for (int i = 0; i < count; i++) {
try {
mRemoteCallbackList
.getBroadcastItem(i).onTetherNetworkConnectionStatusChanged(
mTetherNetworkConnectionStatus);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in updateTetherNetworkConnectionStatus", e);
} }
} }
mRemoteCallbackList.finishBroadcast();
}
/** /**
* Implementing application should call this method to provide an up-to-date status of * Implementing application should call this method to provide an up-to-date status of
@@ -342,10 +288,18 @@ public abstract class SharedConnectivityService extends Service {
@NonNull KnownNetworkConnectionStatus status) { @NonNull KnownNetworkConnectionStatus status) {
mKnownNetworkConnectionStatus = status; mKnownNetworkConnectionStatus = status;
for (ISharedConnectivityCallback callback:mCallbacks) { int count = mRemoteCallbackList.beginBroadcast();
notifyKnownNetworkConnectionStatusChanged(callback); for (int i = 0; i < count; i++) {
try {
mRemoteCallbackList
.getBroadcastItem(i).onKnownNetworkConnectionStatusChanged(
mKnownNetworkConnectionStatus);
} catch (RemoteException e) {
if (DEBUG) Log.w(TAG, "Exception in updateKnownNetworkConnectionStatus", e);
} }
} }
mRemoteCallbackList.finishBroadcast();
}
/** /**
* Implementing application should implement this method. * Implementing application should implement this method.