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 android.annotation.NonNull; 20 import android.content.Context; 21 import android.health.HealthFitnessStatsLog; 22 import android.os.UserHandle; 23 24 import java.util.Objects; 25 26 /** 27 * Logs Health Connect usage stats. 28 * 29 * @hide 30 */ 31 final class UsageStatsLogger { 32 33 /** Write Health Connect usage stats to statsd. */ log(@onNull Context context, @NonNull UserHandle userHandle)34 static void log(@NonNull Context context, @NonNull UserHandle userHandle) { 35 Objects.requireNonNull(userHandle); 36 Objects.requireNonNull(context); 37 38 UsageStatsCollector usageStatsCollector = new UsageStatsCollector(context, userHandle); 39 usageStatsCollector.upsertLastAccessLogTimeStamp(); 40 int numberOfConnectedApps = usageStatsCollector.getPackagesHoldingHealthPermissions(); 41 int numberOfAvailableApps = 42 usageStatsCollector.getNumberOfAppsCompatibleWithHealthConnect(); 43 boolean isUserMonthlyActive = usageStatsCollector.isUserMonthlyActive(); 44 45 // If this condition is true then the user does not uses HC and we should not collect data. 46 // This will reduce the load on logging service otherwise we will get daily data from 47 // billions of Android devices. 48 if (numberOfConnectedApps == 0 && numberOfAvailableApps == 0 && !isUserMonthlyActive) { 49 return; 50 } 51 52 HealthFitnessStatsLog.write( 53 HealthFitnessStatsLog.HEALTH_CONNECT_USAGE_STATS, 54 numberOfConnectedApps, 55 numberOfAvailableApps, 56 isUserMonthlyActive); 57 } 58 } 59