• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.screenshot.message
18 
19 import android.os.UserHandle
20 import android.view.View
21 import android.view.ViewGroup
22 import android.widget.ImageView
23 import android.widget.TextView
24 import com.android.systemui.res.R
25 import com.android.systemui.screenshot.data.model.ProfileType
26 import com.android.systemui.screenshot.data.repository.ProfileTypeRepository
27 import javax.inject.Inject
28 
29 /**
30  * Handles work profile and private profile first run, determining whether a first run UI should be
31  * shown and populating that UI if needed.
32  */
33 class ProfileMessageController
34 @Inject
35 constructor(
36     private val packageLabelIconProvider: PackageLabelIconProvider,
37     private val fileResources: ProfileFirstRunFileResources,
38     private val firstRunSettings: ProfileFirstRunSettings,
39     private val profileTypes: ProfileTypeRepository,
40 ) {
41 
42     /**
43      * @return a populated ProfileFirstRunData object if a profile first run message should be
44      *   shown, otherwise null.
45      */
46     suspend fun onScreenshotTaken(userHandle: UserHandle?): ProfileFirstRunData? {
47         if (userHandle == null) return null
48         val profileType =
49             when (profileTypes.getProfileType(userHandle.identifier)) {
50                 ProfileType.WORK -> FirstRunProfile.WORK
51                 ProfileType.PRIVATE -> FirstRunProfile.PRIVATE
52                 else -> return null
53             }
54 
55         if (firstRunSettings.messageAlreadyDismissed(profileType)) {
56             return null
57         }
58 
59         val fileApp =
60             runCatching {
61                     fileResources.fileManagerComponentName()?.let { fileManager ->
62                         packageLabelIconProvider.getPackageLabelIcon(fileManager, userHandle)
63                     }
64                 }
65                 .getOrNull() ?: fileResources.defaultFileApp()
66 
67         return ProfileFirstRunData(fileApp, profileType)
68     }
69 
70     /**
71      * Use the provided ProfileFirstRunData to populate the profile first run UI in the given view.
72      */
73     fun bindView(view: ViewGroup, data: ProfileFirstRunData, animateOut: () -> Unit) {
74         if (data.labeledIcon.badgedIcon != null) {
75             // Replace the default icon if one is provided.
76             val imageView = view.requireViewById<ImageView>(R.id.screenshot_message_icon)
77             imageView.setImageDrawable(data.labeledIcon.badgedIcon)
78         }
79         val messageContent = view.requireViewById<TextView>(R.id.screenshot_message_content)
80         messageContent.text =
81             view.context.getString(messageTemplate(data.profileType), data.labeledIcon.label)
82         view.requireViewById<View>(R.id.message_dismiss_button).setOnClickListener {
83             animateOut()
84             firstRunSettings.onMessageDismissed(data.profileType)
85         }
86     }
87 
88     private fun messageTemplate(profile: FirstRunProfile): Int {
89         return when (profile) {
90             FirstRunProfile.WORK -> R.string.screenshot_work_profile_notification
91             FirstRunProfile.PRIVATE -> R.string.screenshot_private_profile_notification
92         }
93     }
94 
95     data class ProfileFirstRunData(
96         val labeledIcon: LabeledIcon,
97         val profileType: FirstRunProfile,
98     )
99 
100     enum class FirstRunProfile {
101         WORK,
102         PRIVATE
103     }
104 
105     companion object {
106         const val TAG = "PrivateProfileMessageCtrl"
107     }
108 }
109