• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.systemui.media.controls.util
17 
18 import android.content.Context
19 import android.media.session.MediaController
20 import android.media.session.MediaSession
21 import android.os.Looper
22 import android.util.Log
23 import androidx.concurrent.futures.await
24 import androidx.media3.session.MediaController as Media3Controller
25 import androidx.media3.session.SessionToken
26 import java.util.concurrent.ExecutionException
27 import javax.inject.Inject
28 
29 /** Testable wrapper for media controller construction */
30 open class MediaControllerFactory @Inject constructor(private val context: Context) {
31     private val TAG = "MediaControllerFactory"
32 
33     /**
34      * Creates a new [MediaController] from the framework session token.
35      *
36      * @param token The token for the session. This value must never be null.
37      */
createnull38     open fun create(token: MediaSession.Token): MediaController {
39         return MediaController(context, token)
40     }
41 
42     /**
43      * Creates a new [Media3Controller] from the media3 [SessionToken].
44      *
45      * @param token The token for the session
46      * @param looper The looper that will be used for this controller's operations
47      */
createnull48     open suspend fun create(token: SessionToken, looper: Looper): Media3Controller? {
49         try {
50             return Media3Controller.Builder(context, token)
51                 .setApplicationLooper(looper)
52                 .buildAsync()
53                 .await()
54         } catch (e: ExecutionException) {
55             if (e.cause is SecurityException) {
56                 // The session rejected the connection
57                 Log.d(TAG, "SecurityException creating media3 controller")
58             }
59             return null
60         }
61     }
62 }
63