1 /* 2 * Copyright (C) 2020 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 18 19 import com.android.compatibility.common.util.SystemUtil 20 21 /** 22 * Read the {@link UsageCount} of the group of the permission from the Ui. 23 * 24 * @param groupLabel label fo the group the count should be read for 25 * 26 * @return usage counts for the group of the permission 27 */ 28 fun getUsageCountsFromUi(groupLabel: CharSequence): UsageCount { 29 scrollToPreference(groupLabel) 30 31 return SystemUtil.getEventually { 32 val summary = getPreferenceSummary(groupLabel) 33 34 // Matches two numbers out of the summary line, i.e. "...3...12..." -> "3", "12" 35 val groups = Regex("^[^\\d]*(\\d+)[^\\d]*(\\d+)[^\\d]*\$") 36 .find(summary)?.groupValues 37 ?: throw Exception("No usage counts found") 38 39 UsageCount(groups[1].toInt(), groups[2].toInt()) 40 } 41 } 42 43 /** 44 * Usage counts as read via {@link #getUsageCountsFromUi}. 45 */ 46 data class UsageCount( 47 /** Number of apps with permission granted */ 48 val granted: Int, 49 /** Number of apps that request permissions */ 50 val total: Int 51 )