blob: bbea6b277e90949464dcd616c45a3f4012181878 [file] [log] [blame]
Fabian Kozynski12638242018-10-12 15:33:41 -04001/*
2 * Copyright (C) 2018 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
15package com.android.systemui.privacy
16
17import android.content.Context
Fabian Kozynskief124492018-11-02 11:02:11 -040018import android.graphics.drawable.Drawable
Fabian Kozynski12638242018-10-12 15:33:41 -040019import com.android.systemui.R
Fabian Kozynski12638242018-10-12 15:33:41 -040020
Fabian Kozynski20b609c2019-02-11 12:56:27 -050021class PrivacyDialogBuilder(private val context: Context, itemsList: List<PrivacyItem>) {
Fabian Kozynski12638242018-10-12 15:33:41 -040022
Fabian Kozynskief124492018-11-02 11:02:11 -040023 val appsAndTypes: List<Pair<PrivacyApplication, List<PrivacyType>>>
24 val types: List<PrivacyType>
Fabian Kozynski12638242018-10-12 15:33:41 -040025 val app: PrivacyApplication?
Fabian Kozynskief124492018-11-02 11:02:11 -040026 private val separator = context.getString(R.string.ongoing_privacy_dialog_separator)
27 private val lastSeparator = context.getString(R.string.ongoing_privacy_dialog_last_separator)
Fabian Kozynski12638242018-10-12 15:33:41 -040028
29 init {
Fabian Kozynskief124492018-11-02 11:02:11 -040030 appsAndTypes = itemsList.groupBy({ it.application }, { it.privacyType })
31 .toList()
Fabian Kozynski8e2bc702018-12-20 14:49:16 -050032 .sortedWith(compareBy({ -it.second.size }, // Sort by number of AppOps
Fabian Kozynskia37d5232019-02-01 10:14:26 -050033 { it.second.min() })) // Sort by "smallest" AppOpp (Location is largest)
Fabian Kozynskief124492018-11-02 11:02:11 -040034 types = itemsList.map { it.privacyType }.distinct().sorted()
35 val singleApp = appsAndTypes.size == 1
36 app = if (singleApp) appsAndTypes[0].first else null
Fabian Kozynski12638242018-10-12 15:33:41 -040037 }
38
Fabian Kozynskief124492018-11-02 11:02:11 -040039 fun generateIconsForApp(types: List<PrivacyType>): List<Drawable> {
40 return types.sorted().map { it.getIcon(context) }
41 }
42
43 fun generateIcons() = types.map { it.getIcon(context) }
44
45 private fun <T> List<T>.joinWithAnd(): StringBuilder {
46 return subList(0, size - 1).joinTo(StringBuilder(), separator = separator).apply {
47 append(lastSeparator)
48 append(this@joinWithAnd.last())
Fabian Kozynski12638242018-10-12 15:33:41 -040049 }
50 }
51
Fabian Kozynskief124492018-11-02 11:02:11 -040052 fun joinTypes(): String {
53 return when (types.size) {
54 0 -> ""
55 1 -> types[0].getName(context)
56 else -> types.map { it.getName(context) }.joinWithAnd().toString()
57 }
Fabian Kozynski12638242018-10-12 15:33:41 -040058 }
59
Fabian Kozynskief124492018-11-02 11:02:11 -040060 fun getDialogTitle(): String {
61 if (app != null) {
62 return context.getString(R.string.ongoing_privacy_dialog_single_app_title, joinTypes())
Fabian Kozynski12638242018-10-12 15:33:41 -040063 } else {
Fabian Kozynskief124492018-11-02 11:02:11 -040064 return context.getString(R.string.ongoing_privacy_dialog_multiple_apps_title,
65 joinTypes())
Fabian Kozynski12638242018-10-12 15:33:41 -040066 }
67 }
Gus Prevasab336792018-11-14 13:52:20 -050068}