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.permissionui 18 19 import androidx.test.uiautomator.By 20 import com.android.compatibility.common.util.SystemUtil.getEventually 21 import com.android.compatibility.common.util.UiAutomatorUtils2.waitFindObject 22 23 private const val SUMMARY_TEXT = "apps allowed" 24 25 /** 26 * Read the {@link UsageCount} of the group of the permission from the Ui. 27 * 28 * @param groupLabel label fo the group the count should be read for 29 * @return usage counts for the group of the permission 30 */ getUsageCountsFromUinull31fun getUsageCountsFromUi(groupLabel: CharSequence): UsageCount { 32 waitFindObject(By.text(groupLabel.toString())) 33 34 return getEventually { 35 val summaryText = 36 waitFindObject( 37 By.hasChild(By.text(groupLabel.toString())) 38 .hasChild(By.textContains(SUMMARY_TEXT)) 39 ) 40 .findObject(By.textContains(SUMMARY_TEXT)) 41 .text 42 43 // Matches two numbers out of the summary line, i.e. "...3...12..." -> "3", "12" 44 val groups = 45 Regex("^[^\\d]*(\\d+)[^\\d]*(\\d+)[^\\d]*\$").find(summaryText)?.groupValues 46 ?: throw Exception("No usage counts found") 47 48 UsageCount(groups[1].toInt(), groups[2].toInt()) 49 } 50 } 51 52 /** Usage counts as read via {@link #getUsageCountsFromUi}. */ 53 data class UsageCount( 54 /** Number of apps with permission granted */ 55 val granted: Int, 56 /** Number of apps that request permissions */ 57 val total: Int 58 ) 59