1 /*
<lambda>null2  * 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 package com.android.permissioncontroller.role.ui.wear
18 
19 import android.content.Context
20 import android.content.pm.ApplicationInfo
21 import android.graphics.drawable.Drawable
22 import android.util.Pair
23 import com.android.permissioncontroller.R
24 import com.android.permissioncontroller.permission.utils.Utils
25 import com.android.permissioncontroller.role.model.UserDeniedManager
26 import com.android.permissioncontroller.role.ui.RequestRoleViewModel
27 import com.android.permissioncontroller.role.ui.wear.model.WearRequestRoleViewModel
28 import com.android.role.controller.model.Role
29 
30 /** A helper class for [WearRequestRoleScreen]. */
31 class WearRequestRoleHelper(
32     val context: Context,
33     val applicationInfo: ApplicationInfo,
34     val role: Role,
35     val roleName: String,
36     val packageName: String,
37     val viewModel: RequestRoleViewModel,
38     val wearViewModel: WearRequestRoleViewModel
39 ) {
40     fun getIcon() = Utils.getBadgedIcon(context, applicationInfo)
41 
42     fun getTitle() =
43         context.getString(role.requestTitleResource, Utils.getAppLabel(applicationInfo, context))
44 
45     // Only show this button when the user denied once
46     fun showDontAskButton() =
47         UserDeniedManager.getInstance(context).isDeniedOnce(roleName, packageName)
48 
49     fun getNonePreference(
50         qualifyingApplications: List<Pair<ApplicationInfo, Boolean>>,
51         selectedPackage: String?
52     ): RequestRolePreference? =
53         if (role.shouldShowNone()) {
54             val hasHolderApplication = hasHolderApplication(qualifyingApplications)
55             RequestRolePreference(
56                 packageName = null,
57                 label = context.getString(R.string.default_app_none),
58                 subTitle =
59                     if (!hasHolderApplication) {
60                         context.getString(R.string.request_role_current_default)
61                     } else {
62                         null
63                     },
64                 icon = context.getDrawable(R.drawable.ic_remove_circle),
65                 checked = selectedPackage.isNullOrEmpty(),
66                 enabled =
67                     if (!wearViewModel.dontAskAgain()) {
68                         true
69                     } else {
70                         !hasHolderApplication
71                     },
72                 isHolder = !hasHolderApplication
73             )
74         } else {
75             null
76         }
77 
78     fun getPreferences(
79         qualifyingApplications: List<Pair<ApplicationInfo, Boolean>>,
80         selectedPackage: String?
81     ): List<RequestRolePreference> {
82         return qualifyingApplications
83             .map { qualifyingApplication ->
84                 RequestRolePreference(
85                     packageName = qualifyingApplication.first.packageName,
86                     label = Utils.getAppLabel(qualifyingApplication.first, context),
87                     subTitle =
88                         if (qualifyingApplication.second) {
89                             context.getString(R.string.request_role_current_default)
90                         } else {
91                             context.getString(role.requestDescriptionResource)
92                         },
93                     icon = Utils.getBadgedIcon(context, qualifyingApplication.first),
94                     checked = qualifyingApplication.first.packageName.equals(selectedPackage),
95                     enabled =
96                         if (!wearViewModel.dontAskAgain()) {
97                             true
98                         } else {
99                             qualifyingApplication.second
100                         },
101                     isHolder = qualifyingApplication.second
102                 )
103             }
104             .toList()
105     }
106 
107     private fun hasHolderApplication(
108         qualifyingApplications: List<Pair<ApplicationInfo, Boolean>>
109     ): Boolean = qualifyingApplications.map { it.second }.contains(true)
110 
111     fun shouldSetAsDefaultEnabled(enabled: Boolean): Boolean {
112         return enabled && (wearViewModel.dontAskAgain() || !wearViewModel.isHolderChecked)
113     }
114 
115     fun initializeHolderPackageName(qualifyingApplications: List<Pair<ApplicationInfo, Boolean>>) {
116         wearViewModel.holderPackageName =
117             qualifyingApplications.find { it.second }?.first?.packageName
118     }
119 
120     fun initializeSelectedPackageName() {
121         if (wearViewModel.holderPackageName == null) {
122             wearViewModel.selectedPackageName.value = null
123         } else {
124             wearViewModel.selectedPackageName.value = packageName
125         }
126     }
127 
128     data class RequestRolePreference(
129         val label: String,
130         val subTitle: String?,
131         val icon: Drawable?,
132         val checked: Boolean,
133         val enabled: Boolean,
134         val packageName: String?,
135         val isHolder: Boolean
136     )
137 }
138