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.app.ActivityManager
20 import android.content.Context
21 import android.content.DialogInterface
22 import android.content.Intent
23 import android.os.UserHandle
24 import com.android.settingslib.R
25 import com.android.systemui.animation.DialogTransitionAnimator
26 import com.android.systemui.broadcast.BroadcastSender
27 import com.android.systemui.plugins.FalsingManager
28 import com.android.systemui.statusbar.phone.SystemUIDialog
29 import com.android.systemui.user.CreateUserActivity
30 
31 /** Dialog for adding a new user to the device. */
32 class AddUserDialog(
33     context: Context,
34     userHandle: UserHandle,
35     isKeyguardShowing: Boolean,
36     showEphemeralMessage: Boolean,
37     private val falsingManager: FalsingManager,
38     private val broadcastSender: BroadcastSender,
39     private val dialogTransitionAnimator: DialogTransitionAnimator
40 ) : SystemUIDialog(context) {
41 
42     private val onClickListener =
43         object : DialogInterface.OnClickListener {
onClicknull44             override fun onClick(dialog: DialogInterface, which: Int) {
45                 val penalty =
46                     if (which == BUTTON_NEGATIVE) {
47                         FalsingManager.NO_PENALTY
48                     } else {
49                         FalsingManager.MODERATE_PENALTY
50                     }
51                 if (falsingManager.isFalseTap(penalty)) {
52                     return
53                 }
54 
55                 if (which == BUTTON_NEUTRAL) {
56                     cancel()
57                     return
58                 }
59 
60                 dialogTransitionAnimator.dismissStack(this@AddUserDialog)
61                 if (ActivityManager.isUserAMonkey()) {
62                     return
63                 }
64 
65                 // Use broadcast instead of ShadeController, as this dialog may have started in
66                 // another process where normal dagger bindings are not available.
67                 broadcastSender.sendBroadcastAsUser(
68                     Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS),
69                     userHandle
70                 )
71 
72                 context.startActivityAsUser(
73                     CreateUserActivity.createIntentForStart(context, isKeyguardShowing),
74                     userHandle,
75                 )
76             }
77         }
78 
79     init {
80         setTitle(R.string.user_add_user_title)
81         val message =
82             context.getString(R.string.user_add_user_message_short) +
83                 if (showEphemeralMessage) {
84                     context.getString(
85                         com.android.systemui.res.R.string.user_add_user_message_guest_remove
86                     )
87                 } else {
88                     ""
89                 }
90         setMessage(message)
91 
92         setButton(
93             BUTTON_NEUTRAL,
94             context.getString(android.R.string.cancel),
95             onClickListener,
96         )
97 
98         setButton(
99             BUTTON_POSITIVE,
100             context.getString(android.R.string.ok),
101             onClickListener,
102         )
103 
104         setWindowOnTop(this, isKeyguardShowing)
105     }
106 }
107