[uwb] Use handler for calls to UwbConfigStore

Bug: 197341285
Test: Verified manually
Change-Id: I377d216598b4d2fe2c7daa2a73ea9a059c94e1b8
diff --git a/service/java/com/android/server/uwb/UwbServiceCore.java b/service/java/com/android/server/uwb/UwbServiceCore.java
index c199cd5..d1554b2 100644
--- a/service/java/com/android/server/uwb/UwbServiceCore.java
+++ b/service/java/com/android/server/uwb/UwbServiceCore.java
@@ -98,6 +98,7 @@
     private /* @UwbManager.AdapterStateCallback.State */ int mState;
     private @StateChangeReason int mLastStateChangedReason;
     private  IUwbVendorUciCallback mCallBack = null;
+    private final Handler mHandler;
 
     public UwbServiceCore(Context uwbApplicationContext, NativeUwbManager nativeUwbManager,
             UwbMetrics uwbMetrics, UwbCountryCode uwbCountryCode,
@@ -124,6 +125,11 @@
         updateState(AdapterStateCallback.STATE_DISABLED, StateChangeReason.SYSTEM_BOOT);
 
         mEnableDisableTask = new EnableDisableTask(serviceLooper);
+        mHandler = new Handler(serviceLooper);
+    }
+
+    public Handler getHandler() {
+        return mHandler;
     }
 
     private void updateState(int state, int reason) {
diff --git a/service/java/com/android/server/uwb/UwbServiceImpl.java b/service/java/com/android/server/uwb/UwbServiceImpl.java
index bbc4567..cc139f6 100644
--- a/service/java/com/android/server/uwb/UwbServiceImpl.java
+++ b/service/java/com/android/server/uwb/UwbServiceImpl.java
@@ -269,7 +269,6 @@
     public PersistableBundle addServiceProfile(@NonNull PersistableBundle parameters) {
         enforceUwbPrivilegedPermission();
         ServiceProfile serviceProfile = ServiceProfile.fromBundle(parameters);
-        //TODO Use handler to post the events
         Optional<UUID> serviceInstanceID = mUwbInjector
                 .getProfileManager()
                 .addServiceProfile(serviceProfile.getServiceID());
@@ -377,14 +376,16 @@
     }
 
     public void handleUserSwitch(int userId) {
-        //TODO : Fix threading model here, handle race condition
-        Log.d(TAG, "Handle user switch " + userId);
-        mUwbInjector.getUwbConfigStore().handleUserSwitch(userId);
+        mUwbServiceCore.getHandler().post(() -> {
+            Log.d(TAG, "Handle user switch " + userId);
+            mUwbInjector.getUwbConfigStore().handleUserSwitch(userId);
+        });
     }
 
     public void handleUserUnlock(int userId) {
-        //TODO : Fix threading model here, handle race condition
-        Log.d(TAG, "Handle user unlock " + userId);
-        mUwbInjector.getUwbConfigStore().handleUserUnlock(userId);
+        mUwbServiceCore.getHandler().post(() -> {
+            Log.d(TAG, "Handle user unlock " + userId);
+            mUwbInjector.getUwbConfigStore().handleUserUnlock(userId);
+        });
     }
 }
diff --git a/service/java/com/android/server/uwb/pm/ProfileManager.java b/service/java/com/android/server/uwb/pm/ProfileManager.java
index 5c324de..ffddee6 100644
--- a/service/java/com/android/server/uwb/pm/ProfileManager.java
+++ b/service/java/com/android/server/uwb/pm/ProfileManager.java
@@ -117,7 +117,7 @@
             mAppServiceProfileMap.put(app_uid, appServiceProfileList);
         }
         mHasNewDataToSerialize = true;
-        mUwbConfigStore.saveToStore(true);
+        mHandler.post(() -> mUwbConfigStore.saveToStore(true));
         return Optional.of(serviceInstanceID);
     }
 
@@ -138,7 +138,7 @@
                 }
             }
         }
-        mUwbConfigStore.saveToStore(true);
+        mHandler.post(() -> mUwbConfigStore.saveToStore(true));
     }
 
     public void loadServiceProfile(Map<UUID, ServiceProfileInfo> serviceProfileDataMap) {