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 androidx.annotation.DrawableRes
22 import androidx.annotation.RequiresApi
23 import androidx.preference.Preference
24 import androidx.preference.PreferenceViewHolder
25 import com.android.permissioncontroller.R
26 import com.android.permissioncontroller.safetycenter.ui.view.MoreIssuesHeaderView
27 
28 /** A preference that displays a card linking to a list of more {@link SafetyCenterIssue}. */
29 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
30 internal class MoreIssuesCardPreference(
31     context: Context,
32     @DrawableRes val overrideChevronIconResId: Int?,
33     private var previousMoreIssuesCardData: MoreIssuesCardData?,
34     private var newMoreIssuesCardData: MoreIssuesCardData,
35     private val dismissedOnly: Boolean,
36     val isStaticHeader: Boolean,
37     private val onClickListener: () -> Unit
38 ) : Preference(context), ComparablePreference {
39 
40     init {
41         layoutResource = R.layout.preference_more_issues_card
42     }
43 
onBindViewHoldernull44     override fun onBindViewHolder(holder: PreferenceViewHolder) {
45         super.onBindViewHolder(holder)
46 
47         val issueHeaderView = holder.itemView as MoreIssuesHeaderView
48         if (isStaticHeader) {
49             issueHeaderView.showStaticHeader(
50                 context.getString(R.string.safety_center_dismissed_issues_card_title),
51                 newMoreIssuesCardData.severityLevel
52             )
53         } else {
54             issueHeaderView.showExpandableHeader(
55                 previousMoreIssuesCardData,
56                 newMoreIssuesCardData,
57                 context.getString(
58                     if (dismissedOnly) {
59                         R.string.safety_center_dismissed_issues_card_title
60                     } else {
61                         R.string.safety_center_more_issues_card_title
62                     }
63                 ),
64                 overrideChevronIconResId,
65                 onClickListener
66             )
67         }
68     }
69 
setNewMoreIssuesCardDatanull70     fun setNewMoreIssuesCardData(moreIssuesCardData: MoreIssuesCardData) {
71         previousMoreIssuesCardData = newMoreIssuesCardData
72         newMoreIssuesCardData = moreIssuesCardData
73         notifyChanged()
74     }
75 
isSameItemnull76     override fun isSameItem(preference: Preference): Boolean {
77         return preference is MoreIssuesCardPreference && isStaticHeader == preference.isStaticHeader
78     }
79 
hasSameContentsnull80     override fun hasSameContents(preference: Preference): Boolean {
81         return preference is MoreIssuesCardPreference &&
82             isStaticHeader == preference.isStaticHeader &&
83             previousMoreIssuesCardData == preference.previousMoreIssuesCardData &&
84             newMoreIssuesCardData == preference.newMoreIssuesCardData &&
85             overrideChevronIconResId == preference.overrideChevronIconResId &&
86             dismissedOnly == preference.dismissedOnly
87     }
88 
89     companion object {
90         val TAG: String = MoreIssuesCardPreference::class.java.simpleName
91     }
92 }
93 
94 internal data class MoreIssuesCardData(
95     val severityLevel: Int,
96     val hiddenIssueCount: Int,
97     val isExpanded: Boolean
98 )
99