7777import androidx .media3 .common .Tracks ;
7878import androidx .media3 .common .VideoSize ;
7979import androidx .media3 .common .text .CueGroup ;
80+ import androidx .media3 .common .util .BackgroundExecutor ;
8081import androidx .media3 .common .util .BundleCollectionUtil ;
8182import androidx .media3 .common .util .Clock ;
83+ import androidx .media3 .common .util .Consumer ;
8284import androidx .media3 .common .util .ListenerSet ;
8385import androidx .media3 .common .util .Log ;
8486import androidx .media3 .common .util .Size ;
@@ -210,17 +212,19 @@ public MediaControllerImplBase(
210212
211213 @ Override
212214 public void connect (@ UnderInitialization MediaControllerImplBase this ) {
213- boolean connectionRequested ;
215+ Consumer <Boolean > onConnectionRequested =
216+ connectionRequested -> {
217+ if (!connectionRequested ) {
218+ getInstance ().runOnApplicationLooper (getInstance ()::release );
219+ }
220+ };
214221 if (this .token .getType () == SessionToken .TYPE_SESSION ) {
215222 // Session
216223 serviceConnection = null ;
217- connectionRequested = requestConnectToSession (connectionHints );
224+ requestConnectToSession (connectionHints , onConnectionRequested );
218225 } else {
219226 serviceConnection = new SessionServiceConnection (connectionHints );
220- connectionRequested = requestConnectToService ();
221- }
222- if (!connectionRequested ) {
223- getInstance ().runOnApplicationLooper (getInstance ()::release );
227+ requestConnectToService (onConnectionRequested );
224228 }
225229 }
226230
@@ -2618,7 +2622,7 @@ private void notifyPlayerInfoListenersWithReasons(
26182622 listeners .flushEvents ();
26192623 }
26202624
2621- private boolean requestConnectToService () {
2625+ private void requestConnectToService (Consumer < Boolean > onConnectionRequested ) {
26222626 int flags =
26232627 SDK_INT >= 29
26242628 ? Context .BIND_AUTO_CREATE | Context .BIND_INCLUDE_CAPABILITIES
@@ -2642,34 +2646,45 @@ private boolean requestConnectToService() {
26422646 // If a service wants to keep running, it should be either foreground service or
26432647 // bound service. But there had been request for the feature for system apps
26442648 // and using bindService() will be better fit with it.
2645- try {
2646- if (context .bindService (intent , serviceConnection , flags )) {
2647- return true ;
2648- }
2649- Log .w (TAG , "bind to " + token + " failed" );
2650- } catch (SecurityException e ) {
2651- Log .w (TAG , "bind to " + token + " not allowed" , e );
2652- }
2653- return false ;
2654- }
2655-
2656- private boolean requestConnectToSession (Bundle connectionHints ) {
2657- IMediaSession iSession =
2658- IMediaSession .Stub .asInterface ((IBinder ) checkStateNotNull (token .getBinder ()));
2659- int seq = sequencedFutureManager .obtainNextSequenceNumber ();
2660- ConnectionRequest request =
2661- new ConnectionRequest (
2662- context .getPackageName (),
2663- Process .myPid (),
2664- connectionHints ,
2665- instance .getMaxCommandsForMediaItems ());
2666- try {
2667- iSession .connect (controllerStub , seq , request .toBundle ());
2668- } catch (RemoteException e ) {
2669- Log .w (TAG , "Failed to call connection request." , e );
2670- return false ;
2671- }
2672- return true ;
2649+ BackgroundExecutor .get ()
2650+ .execute (
2651+ () -> {
2652+ try {
2653+ if (context .bindService (intent , serviceConnection , flags )) {
2654+ onConnectionRequested .accept (true );
2655+ return ;
2656+ }
2657+ Log .w (TAG , "bind to " + token + " failed" );
2658+ } catch (SecurityException e ) {
2659+ Log .w (TAG , "bind to " + token + " not allowed" , e );
2660+ }
2661+ onConnectionRequested .accept (false );
2662+ });
2663+ }
2664+
2665+ private void requestConnectToSession (
2666+ Bundle connectionHints , Consumer <Boolean > onConnectionRequested ) {
2667+ BackgroundExecutor .get ()
2668+ .execute (
2669+ () -> {
2670+ IMediaSession iSession =
2671+ IMediaSession .Stub .asInterface ((IBinder ) checkStateNotNull (token .getBinder ()));
2672+ int seq = sequencedFutureManager .obtainNextSequenceNumber ();
2673+ ConnectionRequest request =
2674+ new ConnectionRequest (
2675+ context .getPackageName (),
2676+ Process .myPid (),
2677+ connectionHints ,
2678+ instance .getMaxCommandsForMediaItems ());
2679+ try {
2680+ iSession .connect (controllerStub , seq , request .toBundle ());
2681+ } catch (RemoteException e ) {
2682+ Log .w (TAG , "Failed to call connection request." , e );
2683+ onConnectionRequested .accept (false );
2684+ return ;
2685+ }
2686+ onConnectionRequested .accept (true );
2687+ });
26732688 }
26742689
26752690 private void clearSurfacesAndCallbacks () {
0 commit comments