Track controllers that don't await the connection

This removes some stack traces in unit tests when a
`RemoveMediaController` was created with `waitForConnection=false`
in which case the missing controller in the
`MediaControllerProviderService` caused a `NullPointerException`.

PiperOrigin-RevId: 546795181
This commit is contained in:
bachinger 2023-07-10 09:28:49 +01:00 committed by Rohit Singh
parent 9c74e78f33
commit 175c64a07b

View file

@ -15,6 +15,7 @@
*/ */
package androidx.media3.session; package androidx.media3.session;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.test.session.common.CommonConstants.ACTION_MEDIA3_CONTROLLER; import static androidx.media3.test.session.common.CommonConstants.ACTION_MEDIA3_CONTROLLER;
import static androidx.media3.test.session.common.TestUtils.SERVICE_CONNECTION_TIMEOUT_MS; import static androidx.media3.test.session.common.TestUtils.SERVICE_CONNECTION_TIMEOUT_MS;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
@ -40,8 +41,10 @@ import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -56,6 +59,7 @@ public class MediaControllerProviderService extends Service {
private static final String TAG = "MCProviderService"; private static final String TAG = "MCProviderService";
Map<String, MediaController> mediaControllerMap = new HashMap<>(); Map<String, MediaController> mediaControllerMap = new HashMap<>();
Set<String> untrackedControllerIds = new HashSet<>();
RemoteMediaControllerStub binder; RemoteMediaControllerStub binder;
TestHandler handler; TestHandler handler;
@ -136,6 +140,7 @@ public class MediaControllerProviderService extends Service {
}); });
if (!waitForConnection) { if (!waitForConnection) {
untrackedControllerIds.add(controllerId);
return; return;
} }
@ -696,7 +701,11 @@ public class MediaControllerProviderService extends Service {
runOnHandler( runOnHandler(
() -> { () -> {
MediaController controller = mediaControllerMap.get(controllerId); MediaController controller = mediaControllerMap.get(controllerId);
controller.release(); if (controller != null) {
controller.release();
} else {
checkState(untrackedControllerIds.remove(controllerId));
}
}); });
} }