Isolate mAnimationCompleteLock from mSync.

Sometimes the test fail because activity starts for more than 5
seconds, and it seems only happen when calling startActivitySync
and running for split screen tests.

If there are two activities A and B are running in the same
process. Consider the following sequence:

1. Start ActivityA then move it to primary split screen.
2. Start ActivityB with startActivitySync, it would launched on
second split screen and wait for enter animation complete.
3. Resume ActivityA naturally, but since ActivityB is blocking mSync
in waitForEnterAnimationComplete, performResume for ActivityA would
waiting for mSync on main thread.
4. Animation complete, scheduleEnterAnimationComplete for ActivityB,
however ENTER_ANIMATION_COMPLETE cannot be executed due to main
thread was blocked.
The deadlock occurs until mAnimationCompleteLock timeout.

Note: There is also another case where the first activity is started
more than 5 seconds at the beginning of the test.

Bug: 127741025
Test: atest ActivityThreadTest
Test: atest android.server.wm.lifecycle
Change-Id: I8b4fe44f962dd31d130ee6b38dabf6e0c90b4060
diff --git a/core/java/android/app/Instrumentation.java b/core/java/android/app/Instrumentation.java
index 588acee..690e956 100644
--- a/core/java/android/app/Instrumentation.java
+++ b/core/java/android/app/Instrumentation.java
@@ -493,6 +493,7 @@
     public Activity startActivitySync(@NonNull Intent intent, @Nullable Bundle options) {
         validateNotAppThread();
 
+        final Activity activity;
         synchronized (mSync) {
             intent = new Intent(intent);
 
@@ -527,16 +528,18 @@
                 } catch (InterruptedException e) {
                 }
             } while (mWaitingActivities.contains(aw));
-
-            waitForEnterAnimationComplete(aw.activity);
-
-            // Apply an empty transaction to ensure SF has a chance to update before
-            // the Activity is ready (b/138263890).
-            try (SurfaceControl.Transaction t = new SurfaceControl.Transaction()) {
-                t.apply(true);
-            }
-            return aw.activity;
+            activity = aw.activity;
         }
+
+        // Do not call this method within mSync, lest it could block the main thread.
+        waitForEnterAnimationComplete(activity);
+
+        // Apply an empty transaction to ensure SF has a chance to update before
+        // the Activity is ready (b/138263890).
+        try (SurfaceControl.Transaction t = new SurfaceControl.Transaction()) {
+            t.apply(true);
+        }
+        return activity;
     }
 
     /**