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.content.Context
19 import android.os.Handler
20 import android.util.Log
21 import android.view.View
22 import com.android.internal.jank.InteractionJankMonitor
23 import com.android.internal.logging.UiEventLogger
24 import com.android.systemui.animation.DialogCuj
25 import com.android.systemui.animation.DialogLaunchAnimator
26 import com.android.systemui.dagger.SysUISingleton
27 import com.android.systemui.dagger.qualifiers.Background
28 import com.android.systemui.dagger.qualifiers.Main
29 import com.android.systemui.statusbar.policy.KeyguardStateController
30 import java.util.concurrent.Executor
31 import javax.inject.Inject
32 
33 private const val TAG = "InternetDialogFactory"
34 private val DEBUG = Log.isLoggable(TAG, Log.DEBUG)
35 
36 /**
37  * Factory to create [InternetDialog] objects.
38  */
39 @SysUISingleton
40 class InternetDialogFactory @Inject constructor(
41     @Main private val handler: Handler,
42     @Background private val executor: Executor,
43     private val internetDialogController: InternetDialogController,
44     private val context: Context,
45     private val uiEventLogger: UiEventLogger,
46     private val dialogLaunchAnimator: DialogLaunchAnimator,
47     private val keyguardStateController: KeyguardStateController
48 ) {
49     companion object {
50         private const val INTERACTION_JANK_TAG = "internet"
51         var internetDialog: InternetDialog? = null
52     }
53 
54     /** Creates a [InternetDialog]. The dialog will be animated from [view] if it is not null. */
55     fun create(
56         aboveStatusBar: Boolean,
57         canConfigMobileData: Boolean,
58         canConfigWifi: Boolean,
59         view: View?
60     ) {
61         if (internetDialog != null) {
62             if (DEBUG) {
63                 Log.d(TAG, "InternetDialog is showing, do not create it twice.")
64             }
65             return
66         } else {
67             internetDialog = InternetDialog(
68                 context, this, internetDialogController,
69                 canConfigMobileData, canConfigWifi, aboveStatusBar, uiEventLogger,
70                     dialogLaunchAnimator, handler,
71                 executor, keyguardStateController
72             )
73             if (view != null) {
74                 dialogLaunchAnimator.showFromView(
75                     internetDialog!!, view,
76                     animateBackgroundBoundsChange = true,
77                     cuj = DialogCuj(
78                         InteractionJankMonitor.CUJ_SHADE_DIALOG_OPEN,
79                         INTERACTION_JANK_TAG
80                     )
81                 )
82             } else {
83                 internetDialog?.show()
84             }
85         }
86     }
87 
88     fun destroyDialog() {
89         if (DEBUG) {
90             Log.d(TAG, "destroyDialog")
91         }
92         internetDialog = null
93     }
94 }
95