1 /*
<lambda>null2  * 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 
17 package com.android.systemui.qs.user
18 
19 import android.app.Dialog
20 import android.content.Context
21 import android.content.DialogInterface
22 import android.content.DialogInterface.BUTTON_NEUTRAL
23 import android.content.Intent
24 import android.provider.Settings
25 import android.view.LayoutInflater
26 import com.android.internal.jank.InteractionJankMonitor
27 import com.android.internal.logging.UiEventLogger
28 import com.android.systemui.res.R
29 import com.android.systemui.animation.DialogCuj
30 import com.android.systemui.animation.DialogTransitionAnimator
31 import com.android.systemui.animation.Expandable
32 import com.android.systemui.dagger.SysUISingleton
33 import com.android.systemui.plugins.ActivityStarter
34 import com.android.systemui.plugins.FalsingManager
35 import com.android.systemui.qs.QSUserSwitcherEvent
36 import com.android.systemui.qs.tiles.UserDetailView
37 import com.android.systemui.statusbar.phone.SystemUIDialog
38 import com.android.systemui.user.ui.dialog.DialogShowerImpl
39 import javax.inject.Inject
40 import javax.inject.Provider
41 
42 /**
43  * Controller for [UserDialog].
44  */
45 @SysUISingleton
46 class UserSwitchDialogController @Inject constructor(
47     private val userDetailViewAdapterProvider: Provider<UserDetailView.Adapter>,
48     private val activityStarter: ActivityStarter,
49     private val falsingManager: FalsingManager,
50     private val dialogTransitionAnimator: DialogTransitionAnimator,
51     private val uiEventLogger: UiEventLogger,
52     private val dialogFactory: SystemUIDialog.Factory
53 ) {
54 
55     companion object {
56         private const val INTERACTION_JANK_TAG = "switch_user"
57         private val USER_SETTINGS_INTENT = Intent(Settings.ACTION_USER_SETTINGS)
58     }
59 
60     /**
61      * Show a [UserDialog].
62      *
63      * Populate the dialog with information from and adapter obtained from
64      * [userDetailViewAdapterProvider] and show it as launched from [expandable].
65      */
66     fun showDialog(context: Context, expandable: Expandable) {
67         with(dialogFactory.create()) {
68             setShowForAllUsers(true)
69             setCanceledOnTouchOutside(true)
70 
71             setTitle(R.string.qs_user_switch_dialog_title)
72             setPositiveButton(R.string.quick_settings_done) { _, _ ->
73                 uiEventLogger.log(QSUserSwitcherEvent.QS_USER_DETAIL_CLOSE)
74             }
75             setNeutralButton(R.string.quick_settings_more_user_settings, { _, _ ->
76                 if (!falsingManager.isFalseTap(FalsingManager.LOW_PENALTY)) {
77                     uiEventLogger.log(QSUserSwitcherEvent.QS_USER_MORE_SETTINGS)
78                     val controller = dialogTransitionAnimator.createActivityTransitionController(
79                         getButton(BUTTON_NEUTRAL)
80                     )
81 
82                     if (controller == null) {
83                         dismiss()
84                     }
85 
86                     activityStarter.postStartActivityDismissingKeyguard(
87                         USER_SETTINGS_INTENT, 0, controller
88                     )
89                 }
90             }, false /* dismissOnClick */)
91             val gridFrame = LayoutInflater.from(this.context)
92                 .inflate(R.layout.qs_user_dialog_content, null)
93             setView(gridFrame)
94 
95             val adapter = userDetailViewAdapterProvider.get()
96 
97             adapter.linkToViewGroup(gridFrame.findViewById(R.id.grid))
98 
99             val controller =
100                 expandable.dialogTransitionController(
101                     DialogCuj(InteractionJankMonitor.CUJ_SHADE_DIALOG_OPEN, INTERACTION_JANK_TAG)
102                 )
103             if (controller != null) {
104                 dialogTransitionAnimator.show(
105                     this,
106                     controller,
107                 )
108             } else {
109                 show()
110             }
111 
112             uiEventLogger.log(QSUserSwitcherEvent.QS_USER_DETAIL_OPEN)
113             adapter.injectDialogShower(DialogShowerImpl(this, dialogTransitionAnimator))
114         }
115     }
116 
117     interface DialogShower : DialogInterface {
118         fun showDialog(dialog: Dialog, cuj: DialogCuj)
119     }
120 }
121