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.settingslib.bluetooth;
18 
19 import android.util.Log;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.internal.util.FrameworkStatsLog;
23 
24 import java.util.HashMap;
25 
26 /** Utils class to report hearing aid metrics to statsd */
27 public final class HearingAidStatsLogUtils {
28 
29     private static final String TAG = "HearingAidStatsLogUtils";
30     private static final HashMap<String, Integer> sDeviceAddressToBondEntryMap = new HashMap<>();
31 
32     /**
33      * Sets the mapping from hearing aid device to the bond entry where this device starts it's
34      * bonding(connecting) process.
35      *
36      * @param bondEntry The entry page id where the bonding process starts
37      * @param device The bonding(connecting) hearing aid device
38      */
setBondEntryForDevice(int bondEntry, CachedBluetoothDevice device)39     public static void setBondEntryForDevice(int bondEntry, CachedBluetoothDevice device) {
40         sDeviceAddressToBondEntryMap.put(device.getAddress(), bondEntry);
41     }
42 
43     /**
44      * Logs hearing aid device information to statsd, including device mode, device side, and entry
45      * page id where the binding(connecting) process starts.
46      *
47      * Only logs the info once after hearing aid is bonded(connected). Clears the map entry of this
48      * device when logging is completed.
49      *
50      * @param device The bonded(connected) hearing aid device
51      */
logHearingAidInfo(CachedBluetoothDevice device)52     public static void logHearingAidInfo(CachedBluetoothDevice device) {
53         final String deviceAddress = device.getAddress();
54         if (sDeviceAddressToBondEntryMap.containsKey(deviceAddress)) {
55             final int bondEntry = sDeviceAddressToBondEntryMap.getOrDefault(deviceAddress, -1);
56             final int deviceMode = device.getDeviceMode();
57             final int deviceSide = device.getDeviceSide();
58             FrameworkStatsLog.write(FrameworkStatsLog.HEARING_AID_INFO_REPORTED, deviceMode,
59                     deviceSide, bondEntry);
60 
61             sDeviceAddressToBondEntryMap.remove(deviceAddress);
62         } else {
63             Log.w(TAG, "The device address was not found. Hearing aid device info is not logged.");
64         }
65     }
66 
67     @VisibleForTesting
getDeviceAddressToBondEntryMap()68     static HashMap<String, Integer> getDeviceAddressToBondEntryMap() {
69         return sDeviceAddressToBondEntryMap;
70     }
71 
HearingAidStatsLogUtils()72     private HearingAidStatsLogUtils() {}
73 }
74