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.permissioncontroller.safetycenter.ui
18 
19 import android.content.Context
20 import android.os.Build
21 import android.safetycenter.SafetyCenterEntryGroup
22 import android.text.TextUtils
23 import androidx.annotation.RequiresApi
24 import androidx.preference.Preference
25 import androidx.preference.PreferenceViewHolder
26 import com.android.permissioncontroller.R
27 import com.android.permissioncontroller.safetycenter.ui.model.SafetyCenterViewModel
28 import com.android.permissioncontroller.safetycenter.ui.view.SafetyEntryGroupView
29 
30 /** A preference that displays a visual representation of a {@link SafetyCenterEntryGroup}. */
31 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
32 class SafetyGroupPreference(
33     context: Context,
34     private val group: SafetyCenterEntryGroup,
35     private val isExpanded: (String) -> Boolean,
36     private val isFirstCard: Boolean,
37     private val isLastCard: Boolean,
38     private val getTaskIdForEntry: (String) -> Int,
39     private val viewModel: SafetyCenterViewModel,
40     private val onExpandedListener: (String) -> Unit,
41     private val onCollapsedListener: (String) -> Unit
42 ) : Preference(context), ComparablePreference {
43 
44     init {
45         layoutResource = R.layout.preference_group
46     }
47 
onBindViewHoldernull48     override fun onBindViewHolder(holder: PreferenceViewHolder) {
49         super.onBindViewHolder(holder)
50 
51         (holder.itemView as? SafetyEntryGroupView)?.showGroup(
52             group,
53             isExpanded,
54             isFirstCard,
55             isLastCard,
56             getTaskIdForEntry,
57             viewModel,
58             onExpandedListener,
59             onCollapsedListener
60         )
61     }
62 
isSameItemnull63     override fun isSameItem(preference: Preference): Boolean =
64         preference is SafetyGroupPreference && TextUtils.equals(group.id, preference.group.id)
65 
66     override fun hasSameContents(preference: Preference): Boolean =
67         preference is SafetyGroupPreference &&
68             group == preference.group &&
69             isFirstCard == preference.isFirstCard &&
70             isLastCard == preference.isLastCard
71 }
72