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.content.Intent
21 import android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE
22 import android.safetycenter.SafetyCenterEntryGroup
23 import android.safetycenter.SafetyCenterManager
24 import android.text.TextUtils
25 import androidx.annotation.RequiresApi
26 import androidx.preference.Preference
27 import com.android.permissioncontroller.Constants.EXTRA_SESSION_ID
28 import com.android.permissioncontroller.safetycenter.SafetyCenterConstants
29 import java.util.Objects
30 
31 /**
32  * A preference that displays a visual representation of a {@link SafetyCenterEntryGroup} on the
33  * Safety Center homepage.
34  */
35 @RequiresApi(UPSIDE_DOWN_CAKE)
36 internal class SafetyHomepageEntryPreference(
37     context: Context,
38     private val entryGroup: SafetyCenterEntryGroup,
39     sessionId: Long
40 ) : Preference(context), ComparablePreference {
41 
42     init {
43         setTitle(entryGroup.title)
44         setSummary(entryGroup.summary)
45         SeverityIconPicker.selectIconResIdOrNull(
46                 entryGroup.id,
47                 entryGroup.severityLevel,
48                 entryGroup.severityUnspecifiedIconType
49             )
<lambda>null50             ?.let { setIcon(it) }
51             ?: setIconSpaceReserved(false)
52 
53         val intent = Intent(Intent.ACTION_SAFETY_CENTER)
54         intent.putExtra(SafetyCenterManager.EXTRA_SAFETY_SOURCES_GROUP_ID, entryGroup.id)
55         intent.putExtra(SafetyCenterConstants.EXTRA_OPENED_FROM_HOMEPAGE, true)
56         intent.putExtra(EXTRA_SESSION_ID, sessionId)
57         NavigationSource.SAFETY_CENTER.addToIntent(intent)
58         setIntent(intent)
59         setKey(entryGroup.id)
60     }
61 
isSameItemnull62     override fun isSameItem(preference: Preference): Boolean =
63         preference is SafetyHomepageEntryPreference && entryGroup.id == preference.entryGroup.id
64 
65     override fun hasSameContents(preference: Preference): Boolean =
66         preference is SafetyHomepageEntryPreference &&
67             Objects.equals(entryGroup.id, preference.entryGroup.id) &&
68             TextUtils.equals(entryGroup.title, preference.entryGroup.title) &&
69             TextUtils.equals(entryGroup.summary, preference.entryGroup.summary) &&
70             entryGroup.severityLevel == preference.entryGroup.severityLevel &&
71             entryGroup.severityUnspecifiedIconType ==
72                 preference.entryGroup.severityUnspecifiedIconType
73 }
74