77
77
import androidx .media3 .common .Tracks ;
78
78
import androidx .media3 .common .VideoSize ;
79
79
import androidx .media3 .common .text .CueGroup ;
80
+ import androidx .media3 .common .util .BackgroundExecutor ;
80
81
import androidx .media3 .common .util .BundleCollectionUtil ;
81
82
import androidx .media3 .common .util .Clock ;
83
+ import androidx .media3 .common .util .Consumer ;
82
84
import androidx .media3 .common .util .ListenerSet ;
83
85
import androidx .media3 .common .util .Log ;
84
86
import androidx .media3 .common .util .Size ;
@@ -210,17 +212,19 @@ public MediaControllerImplBase(
210
212
211
213
@ Override
212
214
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
+ };
214
221
if (this .token .getType () == SessionToken .TYPE_SESSION ) {
215
222
// Session
216
223
serviceConnection = null ;
217
- connectionRequested = requestConnectToSession (connectionHints );
224
+ requestConnectToSession (connectionHints , onConnectionRequested );
218
225
} else {
219
226
serviceConnection = new SessionServiceConnection (connectionHints );
220
- connectionRequested = requestConnectToService ();
221
- }
222
- if (!connectionRequested ) {
223
- getInstance ().runOnApplicationLooper (getInstance ()::release );
227
+ requestConnectToService (onConnectionRequested );
224
228
}
225
229
}
226
230
@@ -2618,7 +2622,7 @@ private void notifyPlayerInfoListenersWithReasons(
2618
2622
listeners .flushEvents ();
2619
2623
}
2620
2624
2621
- private boolean requestConnectToService () {
2625
+ private void requestConnectToService (Consumer < Boolean > onConnectionRequested ) {
2622
2626
int flags =
2623
2627
SDK_INT >= 29
2624
2628
? Context .BIND_AUTO_CREATE | Context .BIND_INCLUDE_CAPABILITIES
@@ -2642,34 +2646,45 @@ private boolean requestConnectToService() {
2642
2646
// If a service wants to keep running, it should be either foreground service or
2643
2647
// bound service. But there had been request for the feature for system apps
2644
2648
// 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
+ });
2673
2688
}
2674
2689
2675
2690
private void clearSurfacesAndCallbacks () {
0 commit comments