1 /* 2 * 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.safetycenter.ui.model 18 19 import android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE 20 import android.safetycenter.SafetyCenterData 21 import android.safetycenter.SafetyCenterEntryGroup 22 import android.safetycenter.SafetyCenterEntryOrGroup 23 import android.safetycenter.SafetyCenterIssue 24 import androidx.annotation.RequiresApi 25 import com.android.safetycenter.internaldata.SafetyCenterBundles.ISSUES_TO_GROUPS_BUNDLE_KEY 26 27 /** UI model representation of Safety Center Data */ 28 data class SafetyCenterUiData( 29 val safetyCenterData: SafetyCenterData, 30 val resolvedIssues: Map<IssueId, ActionId> = emptyMap() 31 ) { 32 /** Returns the [SafetyCenterEntryGroup] corresponding to the provided ID */ 33 @RequiresApi(UPSIDE_DOWN_CAKE) getMatchingGroupnull34 fun getMatchingGroup(groupId: String): SafetyCenterEntryGroup? { 35 val entryOrGroups: List<SafetyCenterEntryOrGroup> = safetyCenterData.entriesOrGroups 36 val entryGroups = entryOrGroups.mapNotNull { it.entryGroup } 37 return entryGroups.find { it.id == groupId } 38 } 39 40 /** 41 * Returns a list of [SafetyCenterIssue] corresponding to the provided ID. This will be 42 * displayed as warning cards on a subpage in Safety Center. 43 */ 44 @RequiresApi(UPSIDE_DOWN_CAKE) getMatchingIssuesnull45 fun getMatchingIssues(groupId: String): List<SafetyCenterIssue> = 46 selectMatchingIssuesForGroup(groupId, safetyCenterData.issues) 47 48 /** 49 * Returns a list of dismissed [SafetyCenterIssue] corresponding to the provided ID. This will 50 * be displayed as dismissed warning cards on a subpage in Safety Center. 51 */ 52 @RequiresApi(UPSIDE_DOWN_CAKE) 53 fun getMatchingDismissedIssues(groupId: String): List<SafetyCenterIssue> = 54 selectMatchingIssuesForGroup(groupId, safetyCenterData.dismissedIssues) 55 56 @RequiresApi(UPSIDE_DOWN_CAKE) 57 private fun selectMatchingIssuesForGroup( 58 groupId: String, 59 issues: List<SafetyCenterIssue> 60 ): List<SafetyCenterIssue> { 61 val issuesToGroups = safetyCenterData.extras.getBundle(ISSUES_TO_GROUPS_BUNDLE_KEY) 62 return issues.filter { 63 val mappingExists = issuesToGroups?.containsKey(it.id) ?: false 64 val matchesInMapping = 65 issuesToGroups?.getStringArrayList(it.id)?.contains(groupId) ?: false 66 val matchesByDefault = it.groupId == groupId 67 68 if (mappingExists) matchesInMapping else matchesByDefault 69 } 70 } 71 } 72