Ignore orientation of finishing activity

There is a special case that the invisible activity can still
report orientation for who uses orientation_behind.
e.g.
 Top activity: orientation_behind
 The occluded invisible activity behind: orientation_sensor
Then it will pick sensor for top activity.

But if the top activity is already finishing, it should stop
to search the behind activities and keep current orientation.
Otherwise an intermediate orientation may be chosen which
may cause unnecessary orientation change.

The case may happen when launching orientation_behind activity
with clearTaskOnLaunch or intent flag CLEAR_TASK that will finish
the original activity at the same time.

Fix: 317680642
Test: ActivityRecordTests#testGetOrientation
Change-Id: I3c288ebd79cfe5b780d1f6427432d3c0bb0e568b
diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java
index ca8afe1..1e003da 100644
--- a/services/core/java/com/android/server/wm/ActivityRecord.java
+++ b/services/core/java/com/android/server/wm/ActivityRecord.java
@@ -8065,7 +8065,7 @@
      */
     @Override
     int getOrientation(int candidate) {
-        if (shouldIgnoreOrientationRequests()) {
+        if (finishing || shouldIgnoreOrientationRequests()) {
             return SCREEN_ORIENTATION_UNSET;
         }
 
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
index e88a00b..f049b33 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
@@ -2640,6 +2640,9 @@
         // Can specify orientation if the current orientation candidate is orientation behind.
         assertEquals(SCREEN_ORIENTATION_LANDSCAPE,
                 activity.getOrientation(SCREEN_ORIENTATION_BEHIND));
+        activity.makeFinishingLocked();
+        assertEquals("Finishing activity must not report orientation",
+                SCREEN_ORIENTATION_UNSET, activity.getOrientation(SCREEN_ORIENTATION_BEHIND));
 
         final ActivityRecord translucentActivity = new ActivityBuilder(mAtm)
                 .setActivityTheme(android.R.style.Theme_Translucent)