1 /*
2  * Copyright (C) 2022 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.user.ui.dialog
18 
19 import android.annotation.UserIdInt
20 import android.content.Context
21 import android.content.DialogInterface
22 import com.android.settingslib.R
23 import com.android.systemui.animation.DialogTransitionAnimator
24 import com.android.systemui.plugins.FalsingManager
25 import com.android.systemui.statusbar.phone.SystemUIDialog
26 
27 /** Dialog for exiting the guest user. */
28 class ExitGuestDialog(
29     context: Context,
30     private val guestUserId: Int,
31     private val isGuestEphemeral: Boolean,
32     private val targetUserId: Int,
33     isKeyguardShowing: Boolean,
34     private val falsingManager: FalsingManager,
35     private val dialogTransitionAnimator: DialogTransitionAnimator,
36     private val onExitGuestUserListener: OnExitGuestUserListener,
37 ) : SystemUIDialog(context) {
38 
interfacenull39     fun interface OnExitGuestUserListener {
40         fun onExitGuestUser(
41             @UserIdInt guestId: Int,
42             @UserIdInt targetId: Int,
43             forceRemoveGuest: Boolean,
44         )
45     }
46 
47     private val onClickListener =
48         object : DialogInterface.OnClickListener {
onClicknull49             override fun onClick(dialog: DialogInterface, which: Int) {
50                 val penalty =
51                     if (which == BUTTON_NEGATIVE) {
52                         FalsingManager.NO_PENALTY
53                     } else {
54                         FalsingManager.MODERATE_PENALTY
55                     }
56                 if (falsingManager.isFalseTap(penalty)) {
57                     return
58                 }
59 
60                 if (isGuestEphemeral) {
61                     if (which == BUTTON_POSITIVE) {
62                         dialogTransitionAnimator.dismissStack(this@ExitGuestDialog)
63                         // Ephemeral guest: exit guest, guest is removed by the system
64                         // on exit, since its marked ephemeral
65                         onExitGuestUserListener.onExitGuestUser(guestUserId, targetUserId, false)
66                     } else if (which == BUTTON_NEGATIVE) {
67                         // Cancel clicked, do nothing
68                         cancel()
69                     }
70                 } else {
71                     when (which) {
72                         BUTTON_POSITIVE -> {
73                             dialogTransitionAnimator.dismissStack(this@ExitGuestDialog)
74                             // Non-ephemeral guest: exit guest, guest is not removed by the system
75                             // on exit, since its marked non-ephemeral
76                             onExitGuestUserListener.onExitGuestUser(
77                                 guestUserId,
78                                 targetUserId,
79                                 false
80                             )
81                         }
82                         BUTTON_NEGATIVE -> {
83                             dialogTransitionAnimator.dismissStack(this@ExitGuestDialog)
84                             // Non-ephemeral guest: remove guest and then exit
85                             onExitGuestUserListener.onExitGuestUser(guestUserId, targetUserId, true)
86                         }
87                         BUTTON_NEUTRAL -> {
88                             // Cancel clicked, do nothing
89                             cancel()
90                         }
91                     }
92                 }
93             }
94         }
95 
96     init {
97         if (isGuestEphemeral) {
98             setTitle(context.getString(R.string.guest_exit_dialog_title))
99             setMessage(context.getString(R.string.guest_exit_dialog_message))
100             setButton(
101                 BUTTON_NEUTRAL,
102                 context.getString(android.R.string.cancel),
103                 onClickListener,
104             )
105             setButton(
106                 BUTTON_POSITIVE,
107                 context.getString(R.string.guest_exit_dialog_button),
108                 onClickListener,
109             )
110         } else {
111             setTitle(context.getString(R.string.guest_exit_dialog_title_non_ephemeral))
112             setMessage(context.getString(R.string.guest_exit_dialog_message_non_ephemeral))
113             setButton(
114                 BUTTON_NEUTRAL,
115                 context.getString(android.R.string.cancel),
116                 onClickListener,
117             )
118             setButton(
119                 BUTTON_NEGATIVE,
120                 context.getString(R.string.guest_exit_clear_data_button),
121                 onClickListener,
122             )
123             setButton(
124                 BUTTON_POSITIVE,
125                 context.getString(R.string.guest_exit_save_data_button),
126                 onClickListener,
127             )
128         }
129         setWindowOnTop(this, isKeyguardShowing)
130         setCanceledOnTouchOutside(false)
131     }
132 }
133