1 /* 2 * Copyright (C) 2022 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.server.healthconnect.logging; 18 19 import static android.health.HealthFitnessStatsLog.HEALTH_CONNECT_PERMISSION_STATS; 20 21 import static com.android.healthfitness.flags.Flags.personalHealthRecordTelemetry; 22 23 import android.health.HealthFitnessStatsLog; 24 25 import java.util.List; 26 import java.util.Map; 27 28 /** 29 * Logs Health Connect usage stats. 30 * 31 * @hide 32 */ 33 final class UsageStatsLogger { 34 35 private final HealthFitnessStatsLog mStatsLog; 36 UsageStatsLogger(HealthFitnessStatsLog statsLog)37 UsageStatsLogger(HealthFitnessStatsLog statsLog) { 38 mStatsLog = statsLog; 39 } 40 41 /** Write Health Connect usage stats to statsd. */ log(UsageStatsCollector usageStatsCollector)42 void log(UsageStatsCollector usageStatsCollector) { 43 usageStatsCollector.upsertLastAccessLogTimeStamp(); 44 Map<String, List<String>> packageNameToPermissionsGranted = 45 usageStatsCollector.getPackagesHoldingHealthPermissions(); 46 int numberOfConnectedApps = packageNameToPermissionsGranted.size(); 47 int numberOfAvailableApps = 48 usageStatsCollector.getNumberOfAppsCompatibleWithHealthConnect(); 49 boolean isUserMonthlyActive = usageStatsCollector.isUserMonthlyActive(); 50 51 // If this condition is true then the user does not uses HC and we should not collect data. 52 // This will reduce the load on logging service otherwise we will get daily data from 53 // billions of Android devices. 54 if (numberOfConnectedApps == 0 && numberOfAvailableApps == 0 && !isUserMonthlyActive) { 55 return; 56 } 57 58 logExportImportStats(usageStatsCollector); 59 logPermissionStats(packageNameToPermissionsGranted); 60 logPhrStats(usageStatsCollector); 61 62 mStatsLog.write( 63 HealthFitnessStatsLog.HEALTH_CONNECT_USAGE_STATS, 64 numberOfConnectedApps, 65 numberOfAvailableApps, 66 isUserMonthlyActive); 67 } 68 logPhrStats(UsageStatsCollector usageStatsCollector)69 private void logPhrStats(UsageStatsCollector usageStatsCollector) { 70 if (!personalHealthRecordTelemetry()) { 71 return; 72 } 73 74 int medicalDataSourcesCount = usageStatsCollector.getMedicalDataSourcesCount(); 75 int medicalResourcesCount = usageStatsCollector.getMedicalResourcesCount(); 76 mStatsLog.write( 77 HealthFitnessStatsLog.HEALTH_CONNECT_PHR_USAGE_STATS, 78 medicalDataSourcesCount, 79 medicalResourcesCount, 80 usageStatsCollector.isPhrMonthlyActiveUser(), 81 (int) usageStatsCollector.getGrantedPhrAppsCount()); 82 } 83 logExportImportStats(UsageStatsCollector usageStatsCollector)84 void logExportImportStats(UsageStatsCollector usageStatsCollector) { 85 int exportFrequency = usageStatsCollector.getExportFrequency(); 86 mStatsLog.write( 87 HealthFitnessStatsLog.HEALTH_CONNECT_EXPORT_IMPORT_STATS_REPORTED, exportFrequency); 88 } 89 logPermissionStats(Map<String, List<String>> packageNameToPermissionsGranted)90 void logPermissionStats(Map<String, List<String>> packageNameToPermissionsGranted) { 91 for (Map.Entry<String, List<String>> connectedAppToPermissionsGranted : 92 packageNameToPermissionsGranted.entrySet()) { 93 94 List<String> grantedPermissions = connectedAppToPermissionsGranted.getValue(); 95 96 // This is done to remove the common prefix android.permission.health from all 97 // permissions 98 String[] grantedPermissionsShortened = new String[grantedPermissions.size()]; 99 for (int permissionIndex = 0; 100 permissionIndex < grantedPermissions.size(); 101 permissionIndex++) { 102 String grantedPermission = grantedPermissions.get(permissionIndex); 103 grantedPermissionsShortened[permissionIndex] = 104 grantedPermission.substring(grantedPermission.lastIndexOf('.') + 1); 105 } 106 107 mStatsLog.write( 108 HEALTH_CONNECT_PERMISSION_STATS, 109 connectedAppToPermissionsGranted.getKey(), 110 grantedPermissionsShortened); 111 } 112 } 113 } 114