Should keep updating BT devices event history after use is categorized

We want to run a long term survey for these users. The event history
should be keep updated to reflect what category the users are recently.

Bug: 325699522
Test: atest CachedBluetoothDeviceTest
Test: atest HearingAidStatsLogUtilsTest
Change-Id: I388be0b1fabcd0e141242aa8c31006234dd6bf44
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index bcdb64d..61c3ce7 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -300,37 +300,7 @@
                 mLocalNapRoleConnected = false;
             }
 
-            if (!HearingAidStatsLogUtils.isUserCategorized(mContext)) {
-                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);
-                    }
-                }
-            }
+            HearingAidStatsLogUtils.updateHistoryIfNeeded(mContext, this, profile, newProfileState);
         }
 
         fetchActiveDevices();
@@ -987,11 +957,9 @@
                 connect();
             }
 
-            if (!HearingAidStatsLogUtils.isUserCategorized(mContext)) {
-                // 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());
-            }
+            // 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 97b94da..200f49f 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidStatsLogUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidStatsLogUtils.java
@@ -16,6 +16,7 @@
 
 package com.android.settingslib.bluetooth;
 
+import android.bluetooth.BluetoothProfile;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.icu.text.SimpleDateFormat;
@@ -126,16 +127,43 @@
     }
 
     /**
-     * Indicates if user is categorized as one of {@link #CATEGORY_HEARING_AIDS},
-     * {@link #CATEGORY_NEW_HEARING_AIDS}, {@link #CATEGORY_HEARING_DEVICES}, and
-     * {@link #CATEGORY_NEW_HEARING_DEVICES}.
+     * Updates corresponding history if we found the device is a hearing device after profile state
+     * changed.
      *
      * @param context the request context
-     * @return true if user is already categorized as one of interested group
+     * @param cachedDevice the remote device
+     * @param profile the profile that has a state changed
+     * @param profileState the new profile state
      */
-    public static boolean isUserCategorized(Context context) {
-        String userCategory = getSharedPreferences(context).getString(BT_HEARING_USER_CATEGORY, "");
-        return !userCategory.isEmpty();
+    public static void updateHistoryIfNeeded(Context context, CachedBluetoothDevice cachedDevice,
+            LocalBluetoothProfile profile, int profileState) {
+
+        if (isJustBonded(cachedDevice.getAddress())) {
+            // Saves bonded timestamp as the source for judging whether to display
+            // the survey
+            if (cachedDevice.getProfiles().stream().anyMatch(
+                    p -> (p instanceof HearingAidProfile || p instanceof HapClientProfile))) {
+                HearingAidStatsLogUtils.addCurrentTimeToHistory(context,
+                        HearingAidStatsLogUtils.HistoryType.TYPE_HEARING_AIDS_PAIRED);
+            } else if (cachedDevice.getProfiles().stream().anyMatch(
+                    p -> (p instanceof A2dpSinkProfile || p instanceof HeadsetProfile))) {
+                HearingAidStatsLogUtils.addCurrentTimeToHistory(context,
+                        HearingAidStatsLogUtils.HistoryType.TYPE_HEARING_DEVICES_PAIRED);
+            }
+            removeFromJustBonded(cachedDevice.getAddress());
+        }
+
+        // Saves connected timestamp as the source for judging whether to display
+        // the survey
+        if (profileState == BluetoothProfile.STATE_CONNECTED) {
+            if (profile instanceof HearingAidProfile || profile instanceof HapClientProfile) {
+                HearingAidStatsLogUtils.addCurrentTimeToHistory(context,
+                        HearingAidStatsLogUtils.HistoryType.TYPE_HEARING_AIDS_CONNECTED);
+            } else if (profile instanceof A2dpSinkProfile || profile instanceof HeadsetProfile) {
+                HearingAidStatsLogUtils.addCurrentTimeToHistory(context,
+                        HearingAidStatsLogUtils.HistoryType.TYPE_HEARING_DEVICES_CONNECTED);
+            }
+        }
     }
 
     /**
@@ -186,14 +214,6 @@
                 userCategory = CATEGORY_HEARING_DEVICES;
             }
         }
-
-        if (!userCategory.isEmpty()) {
-            // History become useless once user is categorized. Clear all history.
-            SharedPreferences.Editor editor = getSharedPreferences(context).edit();
-            editor.putString(BT_HEARING_USER_CATEGORY, userCategory).apply();
-            clearHistory(context);
-            sJustBondedDeviceAddressSet.clear();
-        }
         return userCategory;
     }
 
@@ -211,7 +231,7 @@
      * Removes the device address from the just bonded list.
      * @param address the device address
      */
-    public static void removeFromJustBonded(String address) {
+    private static void removeFromJustBonded(String address) {
         sJustBondedDeviceAddressSet.remove(address);
     }
 
@@ -220,24 +240,11 @@
      * @param address the device address
      * @return true if the device address is in the just bonded list
      */
-    public static boolean isJustBonded(String address) {
+    private 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
-     */
-    private static synchronized void clearHistory(Context context) {
-        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
-        editor.remove(BT_HEARING_AIDS_PAIRED_HISTORY)
-                .remove(BT_HEARING_AIDS_CONNECTED_HISTORY)
-                .remove(BT_HEARING_DEVICES_PAIRED_HISTORY)
-                .remove(BT_HEARING_DEVICES_CONNECTED_HISTORY)
-                .apply();
-    }
-
-    /**
      * Adds current timestamp into BT hearing devices related history.
      * @param context the request context
      * @param type the type of history to store the data. See {@link HistoryType}.