Update deviceId from server process when activity is destroyed

When an activity is removed from WindowProcessController,
non UI Contexts, such as application context should be updated
with the default deviceId.

There were two issues in WindowProcessController preventing this
to happen:
- mTopActivityDeviceId should be updated whenever the deviceId changed
  even if it is not dispatched to the client. This way if the activity
  is later removed, we can detect a change in the topActivityDeviceId
  and report it back to the client. The only case when
  mTopActivityDeviceId is updated and not dispatched is when activity
  is started, but the deviceId is passed to the client via other
  channels.
- Getting the topActivityDeviceId should return the DEVICE_ID_DEFAULT
  if there are no activities.

Bug: 264530057
Test: atest DeviceAssociationTest
Change-Id: I712ea9a68fdc55074b61349b5c70ba12946d5f0a
diff --git a/services/core/java/com/android/server/wm/WindowProcessController.java b/services/core/java/com/android/server/wm/WindowProcessController.java
index 7c0318d..d1d0a4f 100644
--- a/services/core/java/com/android/server/wm/WindowProcessController.java
+++ b/services/core/java/com/android/server/wm/WindowProcessController.java
@@ -210,7 +210,7 @@
     /** Whether {@link #mLastReportedConfiguration} is deferred by the cached state. */
     private volatile boolean mHasCachedConfiguration;
 
-    private int mTopActivityDeviceId = Context.DEVICE_ID_DEFAULT;
+    private int mLastTopActivityDeviceId = Context.DEVICE_ID_DEFAULT;
     /**
      * Registered {@link DisplayArea} as a listener to override config changes. {@code null} if not
      * registered.
@@ -1411,8 +1411,9 @@
         // If deviceId for the top-activity changed, schedule passing it to the app process.
         boolean topActivityDeviceChanged = false;
         int deviceId = getTopActivityDeviceId();
-        if (deviceId != mTopActivityDeviceId) {
+        if (deviceId != mLastTopActivityDeviceId) {
             topActivityDeviceChanged = true;
+            mLastTopActivityDeviceId = deviceId;
         }
 
         final Configuration config = getConfiguration();
@@ -1431,15 +1432,13 @@
             return;
         }
 
-        // TODO(b/263402938): Add tests that capture the deviceId dispatch to the client.
-        mTopActivityDeviceId = deviceId;
-        dispatchConfiguration(config, topActivityDeviceChanged ? mTopActivityDeviceId
+        dispatchConfiguration(config, topActivityDeviceChanged ? mLastTopActivityDeviceId
                 : Context.DEVICE_ID_INVALID);
     }
 
     private int getTopActivityDeviceId() {
         ActivityRecord topActivity = getTopNonFinishingActivity();
-        int updatedDeviceId = mTopActivityDeviceId;
+        int updatedDeviceId = Context.DEVICE_ID_DEFAULT;
         if (topActivity != null && topActivity.mDisplayContent != null) {
             updatedDeviceId = mAtm.mTaskSupervisor.getDeviceIdForDisplayId(
                     topActivity.mDisplayContent.mDisplayId);