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.app.KeyguardManager
20 import android.content.Context
21 import android.media.AudioManager
22 import android.media.session.MediaSessionManager
23 import android.os.PowerExemptionManager
24 import android.view.View
25 import com.android.internal.logging.UiEventLogger
26 import com.android.settingslib.bluetooth.LocalBluetoothManager
27 import com.android.systemui.animation.DialogLaunchAnimator
28 import com.android.systemui.broadcast.BroadcastSender
29 import com.android.systemui.flags.FeatureFlags
30 import com.android.systemui.media.nearby.NearbyMediaDevicesManager
31 import com.android.systemui.plugins.ActivityStarter
32 import com.android.systemui.settings.UserTracker
33 import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection
34 import javax.inject.Inject
35 
36 /**
37  * Factory to create [MediaOutputBroadcastDialog] objects.
38  */
39 class MediaOutputBroadcastDialogFactory @Inject constructor(
40     private val context: Context,
41     private val mediaSessionManager: MediaSessionManager,
42     private val lbm: LocalBluetoothManager?,
43     private val starter: ActivityStarter,
44     private val broadcastSender: BroadcastSender,
45     private val notifCollection: CommonNotifCollection,
46     private val uiEventLogger: UiEventLogger,
47     private val dialogLaunchAnimator: DialogLaunchAnimator,
48     private val nearbyMediaDevicesManager: NearbyMediaDevicesManager,
49     private val audioManager: AudioManager,
50     private val powerExemptionManager: PowerExemptionManager,
51     private val keyGuardManager: KeyguardManager,
52     private val featureFlags: FeatureFlags,
53     private val userTracker: UserTracker
54 ) {
55     var mediaOutputBroadcastDialog: MediaOutputBroadcastDialog? = null
56 
57     /** Creates a [MediaOutputBroadcastDialog] for the given package. */
58     fun create(packageName: String, aboveStatusBar: Boolean, view: View? = null) {
59         // Dismiss the previous dialog, if any.
60         mediaOutputBroadcastDialog?.dismiss()
61 
62         val controller = MediaOutputController(context, packageName,
63                 mediaSessionManager, lbm, starter, notifCollection,
64                 dialogLaunchAnimator, nearbyMediaDevicesManager, audioManager,
65                 powerExemptionManager, keyGuardManager, featureFlags, userTracker)
66         val dialog =
67                 MediaOutputBroadcastDialog(context, aboveStatusBar, broadcastSender, controller)
68         mediaOutputBroadcastDialog = dialog
69 
70         // Show the dialog.
71         if (view != null) {
72             dialogLaunchAnimator.showFromView(dialog, view)
73         } else {
74             dialog.show()
75         }
76     }
77 
78     /** dismiss [MediaOutputBroadcastDialog] if exist. */
79     fun dismiss() {
80         mediaOutputBroadcastDialog?.dismiss()
81         mediaOutputBroadcastDialog = null
82     }
83 }
84