Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | package com.android.systemui.privacy |
| 16 | |
| 17 | import android.content.Context |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 18 | import android.graphics.drawable.Drawable |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 19 | import com.android.systemui.R |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 20 | |
Fabian Kozynski | 20b609c | 2019-02-11 12:56:27 -0500 | [diff] [blame] | 21 | class PrivacyDialogBuilder(private val context: Context, itemsList: List<PrivacyItem>) { |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 22 | |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 23 | val appsAndTypes: List<Pair<PrivacyApplication, List<PrivacyType>>> |
| 24 | val types: List<PrivacyType> |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 25 | val app: PrivacyApplication? |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 26 | 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 Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 28 | |
| 29 | init { |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 30 | appsAndTypes = itemsList.groupBy({ it.application }, { it.privacyType }) |
| 31 | .toList() |
Fabian Kozynski | 8e2bc70 | 2018-12-20 14:49:16 -0500 | [diff] [blame] | 32 | .sortedWith(compareBy({ -it.second.size }, // Sort by number of AppOps |
Fabian Kozynski | a37d523 | 2019-02-01 10:14:26 -0500 | [diff] [blame] | 33 | { it.second.min() })) // Sort by "smallest" AppOpp (Location is largest) |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 34 | types = itemsList.map { it.privacyType }.distinct().sorted() |
| 35 | val singleApp = appsAndTypes.size == 1 |
| 36 | app = if (singleApp) appsAndTypes[0].first else null |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 39 | 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 Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 52 | 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 Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 60 | fun getDialogTitle(): String { |
| 61 | if (app != null) { |
| 62 | return context.getString(R.string.ongoing_privacy_dialog_single_app_title, joinTypes()) |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 63 | } else { |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 64 | return context.getString(R.string.ongoing_privacy_dialog_multiple_apps_title, |
| 65 | joinTypes()) |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 68 | } |