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