1 /*
2  * Copyright (C) 2023 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 
18 package com.android.systemui.controls.management
19 
20 import android.app.Dialog
21 import android.content.Context
22 import android.content.DialogInterface
23 import com.android.systemui.R
24 import com.android.systemui.statusbar.phone.SystemUIDialog
25 import java.util.function.Consumer
26 import javax.inject.Inject
27 
28 /**
29  * Factory to create dialogs for consenting to show app panels for specific apps.
30  *
31  * [internalDialogFactory] is for facilitating testing.
32  */
33 class PanelConfirmationDialogFactory(
34     private val internalDialogFactory: (Context) -> SystemUIDialog
35 ) {
36     @Inject constructor() : this({ SystemUIDialog(it) })
37 
38     /**
39      * Creates a dialog to show to the user. [response] will be true if an only if the user responds
40      * affirmatively.
41      */
42     fun createConfirmationDialog(
43         context: Context,
44         appName: CharSequence,
45         response: Consumer<Boolean>
46     ): Dialog {
47         val listener =
48             DialogInterface.OnClickListener { _, which ->
49                 response.accept(which == DialogInterface.BUTTON_POSITIVE)
50             }
51         return internalDialogFactory(context).apply {
52             setTitle(this.context.getString(R.string.controls_panel_authorization_title, appName))
53             setMessage(this.context.getString(R.string.controls_panel_authorization, appName))
54             setCanceledOnTouchOutside(true)
55             setOnCancelListener { response.accept(false) }
56             setPositiveButton(R.string.controls_dialog_ok, listener)
57             setNeutralButton(R.string.cancel, listener)
58         }
59     }
60 }
61