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.volume
18 
19 import android.content.Context
20 import android.util.Log
21 import android.view.View
22 import com.android.systemui.animation.DialogLaunchAnimator
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.plugins.ActivityStarter
25 import javax.inject.Inject
26 
27 private const val TAG = "VolumePanelFactory"
28 private val DEBUG = Log.isLoggable(TAG, Log.DEBUG)
29 
30 /**
31  * Factory to create [VolumePanelDialog] objects. This is the dialog that allows the user to adjust
32  * multiple streams with sliders.
33  */
34 @SysUISingleton
35 class VolumePanelFactory @Inject constructor(
36     private val context: Context,
37     private val activityStarter: ActivityStarter,
38     private val dialogLaunchAnimator: DialogLaunchAnimator
39 ) {
40     companion object {
41         var volumePanelDialog: VolumePanelDialog? = null
42     }
43 
44     /** Creates a [VolumePanelDialog]. The dialog will be animated from [view] if it is not null. */
45     fun create(aboveStatusBar: Boolean, view: View? = null) {
46         if (volumePanelDialog?.isShowing == true) {
47             return
48         }
49 
50         val dialog = VolumePanelDialog(context, activityStarter, aboveStatusBar)
51         volumePanelDialog = dialog
52 
53         // Show the dialog.
54         if (view != null) {
55             dialogLaunchAnimator.showFromView(dialog, view, animateBackgroundBoundsChange = true)
56         } else {
57             dialog.show()
58         }
59     }
60 
61     /** Dismiss [VolumePanelDialog] if exist. */
62     fun dismiss() {
63         if (DEBUG) {
64             Log.d(TAG, "dismiss dialog")
65         }
66         volumePanelDialog?.dismiss()
67         volumePanelDialog = null
68     }
69 }
70