1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.privacy 16 17 import android.content.Context 18 import com.android.systemui.res.R 19 20 class PrivacyChipBuilder(private val context: Context, itemsList: List<PrivacyItem>) { 21 22 val appsAndTypes: List<Pair<PrivacyApplication, List<PrivacyType>>> 23 val types: List<PrivacyType> 24 private val separator = context.getString(R.string.ongoing_privacy_dialog_separator) 25 private val lastSeparator = context.getString(R.string.ongoing_privacy_dialog_last_separator) 26 27 init { <lambda>null28 appsAndTypes = itemsList.groupBy({ it.application }, { it.privacyType }) 29 .toList() <lambda>null30 .sortedWith(compareBy({ -it.second.size }, // Sort by number of AppOps <lambda>null31 { it.second.minOrNull() })) // Sort by "smallest" AppOpp (Location is largest) <lambda>null32 types = itemsList.map { it.privacyType }.distinct().sorted() 33 } 34 <lambda>null35 fun generateIcons() = types.map { it.getIcon(context) } 36 joinWithAndnull37 private fun <T> List<T>.joinWithAnd(): StringBuilder { 38 return subList(0, size - 1).joinTo(StringBuilder(), separator = separator).apply { 39 append(lastSeparator) 40 append(this@joinWithAnd.last()) 41 } 42 } 43 joinTypesnull44 fun joinTypes(): String { 45 return when (types.size) { 46 0 -> "" 47 1 -> types[0].getName(context) 48 else -> types.map { it.getName(context) }.joinWithAnd().toString() 49 } 50 } 51 } 52