diff --git a/service/src/com/android/server/connectivity/MulticastRoutingCoordinatorService.java b/service/src/com/android/server/connectivity/MulticastRoutingCoordinatorService.java index 4d5001bf83..ac479b895b 100644 --- a/service/src/com/android/server/connectivity/MulticastRoutingCoordinatorService.java +++ b/service/src/com/android/server/connectivity/MulticastRoutingCoordinatorService.java @@ -27,6 +27,8 @@ import static android.system.OsConstants.SOCK_CLOEXEC; import static android.system.OsConstants.SOCK_NONBLOCK; import static android.system.OsConstants.SOCK_RAW; +import static com.android.net.module.util.CollectionUtils.getIndexForValue; + import android.annotation.NonNull; import android.annotation.Nullable; import android.net.MulticastRoutingConfig; @@ -150,7 +152,7 @@ public class MulticastRoutingCoordinatorService { } private Integer getInterfaceIndex(String ifName) { - int mapIndex = mInterfaces.indexOfValue(ifName); + int mapIndex = getIndexForValue(mInterfaces, ifName); if (mapIndex < 0) return null; return mInterfaces.keyAt(mapIndex); } @@ -246,7 +248,7 @@ public class MulticastRoutingCoordinatorService { if (virtualIndex == null) return; updateMfcs(); - mInterfaces.removeAt(mInterfaces.indexOfValue(ifName)); + mInterfaces.removeAt(getIndexForValue(mInterfaces, ifName)); mVirtualInterfaces.remove(virtualIndex); try { mDependencies.setsockoptMrt6DelMif(mMulticastRoutingFd, virtualIndex); @@ -270,7 +272,7 @@ public class MulticastRoutingCoordinatorService { @VisibleForTesting public Integer getVirtualInterfaceIndex(String ifName) { - int mapIndex = mVirtualInterfaces.indexOfValue(ifName); + int mapIndex = getIndexForValue(mVirtualInterfaces, ifName); if (mapIndex < 0) return null; return mVirtualInterfaces.keyAt(mapIndex); } @@ -291,7 +293,7 @@ public class MulticastRoutingCoordinatorService { private void maybeAddAndTrackInterface(String ifName) { checkOnHandlerThread(); - if (mVirtualInterfaces.indexOfValue(ifName) >= 0) return; + if (getIndexForValue(mVirtualInterfaces, ifName) >= 0) return; int nextVirtualIndex = getNextAvailableVirtualIndex(); int ifIndex = mDependencies.getInterfaceIndex(ifName); diff --git a/staticlibs/framework/com/android/net/module/util/CollectionUtils.java b/staticlibs/framework/com/android/net/module/util/CollectionUtils.java index 39e7ce9456..f3d8c4aeae 100644 --- a/staticlibs/framework/com/android/net/module/util/CollectionUtils.java +++ b/staticlibs/framework/com/android/net/module/util/CollectionUtils.java @@ -389,4 +389,28 @@ public final class CollectionUtils { } return dest; } + + /** + * Returns an index of the given SparseArray that contains the given value, or -1 + * number if no keys map to the given value. + * + *
Note this is a linear search, and if multiple keys can map to the same value + * then the smallest index is returned. + * + *
This function compares values with {@code equals} while the
+ * {@link SparseArray#indexOfValue} compares values using {@code ==}.
+ */
+ public static