1 /*
2  * Copyright (C) 2021 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.qs.tiles.dialog
17 
18 import android.util.Log
19 import com.android.internal.jank.InteractionJankMonitor
20 import com.android.systemui.animation.DialogCuj
21 import com.android.systemui.animation.DialogTransitionAnimator
22 import com.android.systemui.animation.Expandable
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.dagger.qualifiers.Background
25 import com.android.systemui.statusbar.phone.SystemUIDialog
26 import javax.inject.Inject
27 import kotlinx.coroutines.CoroutineDispatcher
28 import kotlinx.coroutines.CoroutineScope
29 import kotlinx.coroutines.cancel
30 
31 private const val TAG = "InternetDialogFactory"
32 private val DEBUG = Log.isLoggable(TAG, Log.DEBUG)
33 
34 /** Factory to create [InternetDialogDelegate] objects. */
35 @SysUISingleton
36 class InternetDialogManager
37 @Inject
38 constructor(
39     private val dialogTransitionAnimator: DialogTransitionAnimator,
40     private val dialogFactory: InternetDialogDelegate.Factory,
41     @Background private val bgDispatcher: CoroutineDispatcher,
42 ) {
43     private lateinit var coroutineScope: CoroutineScope
44     companion object {
45         private const val INTERACTION_JANK_TAG = "internet"
46         var dialog: SystemUIDialog? = null
47     }
48 
49     /**
50      * Creates a [InternetDialogDelegate]. The dialog will be animated from [expandable] if it is
51      * not null.
52      */
createnull53     fun create(
54         aboveStatusBar: Boolean,
55         canConfigMobileData: Boolean,
56         canConfigWifi: Boolean,
57         expandable: Expandable?
58     ) {
59         if (dialog != null) {
60             if (DEBUG) {
61                 Log.d(TAG, "InternetDialog is showing, do not create it twice.")
62             }
63             return
64         } else {
65             coroutineScope = CoroutineScope(bgDispatcher)
66             dialog =
67                 dialogFactory
68                     .create(aboveStatusBar, canConfigMobileData, canConfigWifi, coroutineScope)
69                     .createDialog()
70             val controller =
71                 expandable?.dialogTransitionController(
72                     DialogCuj(InteractionJankMonitor.CUJ_SHADE_DIALOG_OPEN, INTERACTION_JANK_TAG)
73                 )
74             controller?.let {
75                 dialogTransitionAnimator.show(
76                     dialog!!,
77                     controller,
78                     animateBackgroundBoundsChange = true
79                 )
80             }
81                 ?: dialog?.show()
82         }
83     }
84 
destroyDialognull85     fun destroyDialog() {
86         if (DEBUG) {
87             Log.d(TAG, "destroyDialog")
88         }
89         if (dialog != null) {
90             coroutineScope.cancel()
91         }
92         dialog = null
93     }
94 }
95