Save BT hearing device event history for HaTS surveys

We'll run surveys on hearing adis and general hearing devices users to
know the satisfaction difference between different kinds of devices.
This can help us prioritize to improve the parts which are less satisfying.

getProfiles() are not availabe right after the bonded event. We need to wait until the device is connected to see if it's a hearing device or not. Delay
the saving time of hearing devices bonded events until the profiles are connected.

Bug: 294627726
Test: atest HearingAidStatsLogUtilsTest
Test: atest CachedBluetoothDeviceTest
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:ea207bdb0a87dccd7026cc133b407f3081c7c823)

Change-Id: I70dc1680bea2f1002325c2ba1497dfccb5cf2b48
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index f03ff00..fabaebe 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -277,6 +277,32 @@
                 mRemovedProfiles.add(profile);
                 mLocalNapRoleConnected = false;
             }
+
+            if (HearingAidStatsLogUtils.isJustBonded(getAddress())) {
+                // Saves bonded timestamp as the source for judging whether to display the survey
+                if (getProfiles().stream().anyMatch(
+                        p -> (p instanceof HearingAidProfile || p instanceof HapClientProfile))) {
+                    HearingAidStatsLogUtils.addCurrentTimeToHistory(mContext,
+                            HearingAidStatsLogUtils.HistoryType.TYPE_HEARING_AIDS_PAIRED);
+                } else if (getProfiles().stream().anyMatch(
+                        p -> (p instanceof A2dpSinkProfile || p instanceof HeadsetProfile))) {
+                    HearingAidStatsLogUtils.addCurrentTimeToHistory(mContext,
+                            HearingAidStatsLogUtils.HistoryType.TYPE_HEARING_DEVICES_PAIRED);
+                }
+                HearingAidStatsLogUtils.removeFromJustBonded(getAddress());
+            }
+
+            // Saves connected timestamp as the source for judging whether to display the survey
+            if (newProfileState == BluetoothProfile.STATE_CONNECTED) {
+                if (profile instanceof HearingAidProfile || profile instanceof HapClientProfile) {
+                    HearingAidStatsLogUtils.addCurrentTimeToHistory(mContext,
+                            HearingAidStatsLogUtils.HistoryType.TYPE_HEARING_AIDS_CONNECTED);
+                } else if (profile instanceof A2dpSinkProfile
+                        || profile instanceof HeadsetProfile) {
+                    HearingAidStatsLogUtils.addCurrentTimeToHistory(mContext,
+                            HearingAidStatsLogUtils.HistoryType.TYPE_HEARING_DEVICES_CONNECTED);
+                }
+            }
         }
 
         fetchActiveDevices();
@@ -899,6 +925,10 @@
             if (mDevice.isBondingInitiatedLocally()) {
                 connect();
             }
+
+            // Saves this device as just bonded and checks if it's an hearing device after profiles
+            // are connected. This is for judging whether to display the survey.
+            HearingAidStatsLogUtils.addToJustBonded(getAddress());
         }
     }
 
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidStatsLogUtils.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidStatsLogUtils.java
index eb2df03..3b84053 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidStatsLogUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidStatsLogUtils.java
@@ -31,8 +31,10 @@
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.Locale;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
@@ -70,6 +72,7 @@
     }
 
     private static final HashMap<String, Integer> sDeviceAddressToBondEntryMap = new HashMap<>();
+    private static final Set<String> sJustBondedDeviceAddressSet = new HashSet<>();
 
     /**
      * Sets the mapping from hearing aid device to the bond entry where this device starts it's
@@ -112,6 +115,33 @@
     }
 
     /**
+     * Maintains a temporarily list of just bonded device address. After the device profiles are
+     * connected, {@link HearingAidStatsLogUtils#removeFromJustBonded} will be called to remove the
+     * address.
+     * @param address the device address
+     */
+    public static void addToJustBonded(String address) {
+        sJustBondedDeviceAddressSet.add(address);
+    }
+
+    /**
+     * Removes the device address from the just bonded list.
+     * @param address the device address
+     */
+    public static void removeFromJustBonded(String address) {
+        sJustBondedDeviceAddressSet.remove(address);
+    }
+
+    /**
+     * Checks whether the device address is in the just bonded list.
+     * @param address the device address
+     * @return true if the device address is in the just bonded list
+     */
+    public static boolean isJustBonded(String address) {
+        return sJustBondedDeviceAddressSet.contains(address);
+    }
+
+    /**
      * Clears all BT hearing devices related history stored in shared preference.
      * @param context the request context
      */