Snap for 6320329 from 0ada5b161bd38469790d17d30670c28f0444007a to qt-qpr3-release

Change-Id: Id9f01b12d7da6dd68c146142da127b88c474036c
diff --git a/src/com/android/car/companiondevicesupport/api/internal/trust/ITrustedDeviceManager.aidl b/src/com/android/car/companiondevicesupport/api/internal/trust/ITrustedDeviceManager.aidl
index 7695a35..0bc3b30 100644
--- a/src/com/android/car/companiondevicesupport/api/internal/trust/ITrustedDeviceManager.aidl
+++ b/src/com/android/car/companiondevicesupport/api/internal/trust/ITrustedDeviceManager.aidl
@@ -48,7 +48,10 @@
     void unregisterTrustedDeviceEnrollmentCallback(in ITrustedDeviceEnrollmentCallback callback);
 
     /** Set a delegate for TrustAgent operation calls. */
-    void setTrustedDeviceAgentDelegate(in @nullable ITrustedDeviceAgentDelegate trustAgentDelegate);
+    void setTrustedDeviceAgentDelegate(in ITrustedDeviceAgentDelegate trustAgentDelegate);
+
+    /** Remove a prevoiusly set delegate. */
+    void clearTrustedDeviceAgentDelegate(in ITrustedDeviceAgentDelegate trustAgentDelegate);
 
     /** Returns a list of trusted devices for user. */
     List<TrustedDevice> getTrustedDevicesForActiveUser();
diff --git a/src/com/android/car/companiondevicesupport/feature/trust/TrustedDeviceAgentService.java b/src/com/android/car/companiondevicesupport/feature/trust/TrustedDeviceAgentService.java
index e63d0b7..71c8db7 100644
--- a/src/com/android/car/companiondevicesupport/feature/trust/TrustedDeviceAgentService.java
+++ b/src/com/android/car/companiondevicesupport/feature/trust/TrustedDeviceAgentService.java
@@ -53,8 +53,9 @@
 
     @Override
     public void onDestroy() {
+        logd(TAG, "Destroying trust agent service.");
         try {
-            mTrustedDeviceManager.setTrustedDeviceAgentDelegate(null);
+            mTrustedDeviceManager.clearTrustedDeviceAgentDelegate(mTrustedDeviceAgentDelegate);
         } catch (RemoteException e) {
             loge(TAG, "Error while disconnecting from TrustedDeviceManager.");
         }
@@ -130,6 +131,5 @@
             logd(TAG, "Removing escrow token for user " + userId + ".");
             TrustedDeviceAgentService.this.removeEscrowToken(handle, UserHandle.of(userId));
         }
-
     };
 }
diff --git a/src/com/android/car/companiondevicesupport/feature/trust/TrustedDeviceManager.java b/src/com/android/car/companiondevicesupport/feature/trust/TrustedDeviceManager.java
index 060bb13..bf2176d 100644
--- a/src/com/android/car/companiondevicesupport/feature/trust/TrustedDeviceManager.java
+++ b/src/com/android/car/companiondevicesupport/feature/trust/TrustedDeviceManager.java
@@ -371,13 +371,9 @@
 
     @Override
     public void setTrustedDeviceAgentDelegate(ITrustedDeviceAgentDelegate trustAgentDelegate) {
-
+        logd(TAG, "Set trusted device agent delegate: " + trustAgentDelegate + ".");
         mTrustAgentDelegate = trustAgentDelegate;
 
-        if (trustAgentDelegate == null) {
-            return;
-        }
-
         // Add pending token if present.
         if (mPendingToken != null) {
             try {
@@ -395,6 +391,18 @@
         }
     }
 
+    @Override
+    public void clearTrustedDeviceAgentDelegate(ITrustedDeviceAgentDelegate trustAgentDelegate) {
+        if (trustAgentDelegate.asBinder() != mTrustAgentDelegate.asBinder()) {
+            logd(TAG, "TrustedDeviceAgentDelegate " + trustAgentDelegate + " doesn't match the " +
+                    "current TrustedDeviceAgentDelegate: " + mTrustAgentDelegate +
+                    ". Ignoring call to clear.");
+            return;
+        }
+        logd(TAG, "Clear current TrustedDeviceAgentDelegate: " + trustAgentDelegate + ".");
+        mTrustAgentDelegate = null;
+    }
+
     private boolean areCredentialsValid(@Nullable PhoneCredentials credentials) {
         return credentials != null && credentials.getEscrowToken() != null
                 && credentials.getHandle() != null;