1 /* 2 * Copyright (C) 2022 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 17 package com.android.systemui.media.dialog 18 19 import android.content.Context 20 import android.view.View 21 import com.android.systemui.animation.DialogTransitionAnimator 22 import com.android.systemui.broadcast.BroadcastSender 23 import com.android.systemui.dagger.qualifiers.Background 24 import com.android.systemui.dagger.qualifiers.Main 25 import java.util.concurrent.Executor 26 import javax.inject.Inject 27 28 /** Manager to create and show a [MediaOutputBroadcastDialog]. */ 29 class MediaOutputBroadcastDialogManager 30 @Inject 31 constructor( 32 private val context: Context, 33 private val broadcastSender: BroadcastSender, 34 private val dialogTransitionAnimator: DialogTransitionAnimator, 35 private val mediaSwitchingControllerFactory: MediaSwitchingController.Factory, 36 @Main private val mainExecutor: Executor, 37 @Background private val backgroundExecutor: Executor, 38 ) { 39 var mediaOutputBroadcastDialog: MediaOutputBroadcastDialog? = null 40 41 /** Creates a [MediaOutputBroadcastDialog] for the given package. */ createAndShownull42 fun createAndShow(packageName: String, aboveStatusBar: Boolean, view: View? = null) { 43 // Dismiss the previous dialog, if any. 44 mediaOutputBroadcastDialog?.dismiss() 45 46 // TODO: b/321969740 - Populate the userHandle parameter. The user handle is necessary to 47 // disambiguate the same package running on different users. 48 val controller = 49 mediaSwitchingControllerFactory.create( 50 packageName, 51 /* userHandle= */ null, 52 /* token */ null, 53 ) 54 val dialog = 55 MediaOutputBroadcastDialog( 56 context, 57 aboveStatusBar, 58 broadcastSender, 59 controller, 60 mainExecutor, 61 backgroundExecutor, 62 ) 63 mediaOutputBroadcastDialog = dialog 64 65 // Show the dialog. 66 if (view != null) { 67 dialogTransitionAnimator.showFromView(dialog, view) 68 } else { 69 dialog.show() 70 } 71 } 72 73 /** dismiss [MediaOutputBroadcastDialog] if exist. */ dismissnull74 fun dismiss() { 75 mediaOutputBroadcastDialog?.dismiss() 76 mediaOutputBroadcastDialog = null 77 } 78 } 79