• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.qs.panels.ui.dialog
18 
19 import android.util.Log
20 import androidx.compose.material3.Text
21 import androidx.compose.runtime.Composable
22 import androidx.compose.ui.res.stringResource
23 import androidx.lifecycle.DefaultLifecycleObserver
24 import androidx.lifecycle.LifecycleOwner
25 import com.android.compose.PlatformButton
26 import com.android.compose.PlatformTextButton
27 import com.android.systemui.dagger.SysUISingleton
28 import com.android.systemui.dialog.ui.composable.AlertDialogContent
29 import com.android.systemui.qs.panels.domain.interactor.EditTilesResetInteractor
30 import com.android.systemui.res.R
31 import com.android.systemui.shade.domain.interactor.ShadeDialogContextInteractor
32 import com.android.systemui.statusbar.phone.ComponentSystemUIDialog
33 import com.android.systemui.statusbar.phone.SystemUIDialog
34 import com.android.systemui.statusbar.phone.SystemUIDialogFactory
35 import com.android.systemui.statusbar.phone.create
36 import com.android.systemui.util.Assert
37 import javax.inject.Inject
38 
39 @SysUISingleton
40 class QSResetDialogDelegate
41 @Inject
42 constructor(
43     private val sysuiDialogFactory: SystemUIDialogFactory,
44     private val shadeDialogContextInteractor: ShadeDialogContextInteractor,
45     private val resetInteractor: EditTilesResetInteractor,
46 ) : SystemUIDialog.Delegate {
47     private var currentDialog: ComponentSystemUIDialog? = null
48 
createDialognull49     override fun createDialog(): SystemUIDialog {
50         Assert.isMainThread()
51         if (currentDialog != null) {
52             Log.d(TAG, "Dialog is already open, dismissing it and creating a new one.")
53             currentDialog?.dismiss()
54         }
55 
56         currentDialog =
57             sysuiDialogFactory
58                 .create(context = shadeDialogContextInteractor.context) {
59                     ResetConfirmationDialog(it)
60                 }
61                 .also {
62                     it.lifecycle.addObserver(
63                         object : DefaultLifecycleObserver {
64                             override fun onStop(owner: LifecycleOwner) {
65                                 Assert.isMainThread()
66                                 currentDialog = null
67                             }
68                         }
69                     )
70                 }
71         return currentDialog!!
72     }
73 
74     @Composable
ResetConfirmationDialognull75     private fun ResetConfirmationDialog(dialog: SystemUIDialog) {
76         AlertDialogContent(
77             title = { Text(text = stringResource(id = R.string.qs_edit_mode_reset_dialog_title)) },
78             content = {
79                 Text(text = stringResource(id = R.string.qs_edit_mode_reset_dialog_content))
80             },
81             positiveButton = {
82                 PlatformButton(
83                     onClick = {
84                         dialog.dismiss()
85                         resetInteractor.reset()
86                     }
87                 ) {
88                     Text(stringResource(id = android.R.string.ok))
89                 }
90             },
91             negativeButton = {
92                 PlatformTextButton(onClick = { dialog.dismiss() }) {
93                     Text(stringResource(id = android.R.string.cancel))
94                 }
95             },
96         )
97     }
98 
showDialognull99     fun showDialog() {
100         if (currentDialog == null) {
101             createDialog()
102         }
103         currentDialog?.show()
104     }
105 
106     companion object {
107         private const val TAG = "ResetDialogDelegate"
108     }
109 }
110