Merge "Improve InsetsPolicy#adjustVisibilityForIme when switching apps" into sc-v2-dev
diff --git a/core/java/android/app/servertransaction/ActivityTransactionItem.java b/core/java/android/app/servertransaction/ActivityTransactionItem.java
index a539812..186f25d 100644
--- a/core/java/android/app/servertransaction/ActivityTransactionItem.java
+++ b/core/java/android/app/servertransaction/ActivityTransactionItem.java
@@ -59,36 +59,37 @@
     }
 
     /**
-     * Get the {@link ActivityClientRecord} instance that corresponds to the provided token.
+     * Gets the {@link ActivityClientRecord} instance that corresponds to the provided token.
      * @param client Target client handler.
      * @param token Target activity token.
-     * @param includeLaunching Indicate to also find the {@link ActivityClientRecord} in launching
-     *                         activity list. It should be noted that there is no activity in
+     * @param includeLaunching Indicate to find the {@link ActivityClientRecord} in launching
+     *                         activity list.
+     *                         <p>Note that there is no {@link android.app.Activity} instance in
      *                         {@link ActivityClientRecord} from the launching activity list.
      * @return The {@link ActivityClientRecord} instance that corresponds to the provided token.
      */
     @NonNull ActivityClientRecord getActivityClientRecord(
             @NonNull ClientTransactionHandler client, IBinder token, boolean includeLaunching) {
-        ActivityClientRecord r = client.getActivityClient(token);
-        if (r != null) {
-            if (client.getActivity(token) == null) {
+        ActivityClientRecord r = null;
+        // Check launching Activity first to prevent race condition that activity instance has not
+        // yet set to ActivityClientRecord.
+        if (includeLaunching) {
+            r = client.getLaunchingActivity(token);
+        }
+        // Then if we don't want to find launching Activity or the ActivityClientRecord doesn't
+        // exist in launching Activity list. The ActivityClientRecord should have been initialized
+        // and put in the Activity list.
+        if (r == null) {
+            r = client.getActivityClient(token);
+            if (r != null && client.getActivity(token) == null) {
                 throw new IllegalArgumentException("Activity must not be null to execute "
                         + "transaction item");
             }
-            return r;
-        }
-        // The activity may not be launched yet. Fallback to check launching activity.
-        if (includeLaunching) {
-            r = client.getLaunchingActivity(token);
         }
         if (r == null) {
             throw new IllegalArgumentException("Activity client record must not be null to execute "
                     + "transaction item");
         }
-
-        // We don't need to check the activity of launching activity client records because they
-        // have not been launched yet.
-
         return r;
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelViewController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelViewController.java
index 07508a6..39dc540 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelViewController.java
@@ -652,7 +652,7 @@
 
     private KeyguardMediaController mKeyguardMediaController;
 
-    private boolean mStatusViewCentered = false;
+    private boolean mStatusViewCentered = true;
 
     private View.AccessibilityDelegate mAccessibilityDelegate = new View.AccessibilityDelegate() {
         @Override
@@ -1082,7 +1082,7 @@
         constraintSet.applyTo(mNotificationContainerParent);
         mNotificationContainerParent.setSplitShadeEnabled(mShouldUseSplitNotificationShade);
 
-        updateKeyguardStatusViewAlignment(false /* animate */);
+        updateKeyguardStatusViewAlignment(/* animate= */false);
         mLockscreenSmartspaceController.onSplitShadeChanged(mShouldUseSplitNotificationShade);
         mKeyguardMediaController.refreshMediaPosition();
     }
@@ -1129,6 +1129,9 @@
         keyguardStatusView = (KeyguardStatusView) mLayoutInflater.inflate(
                 R.layout.keyguard_status_view, mNotificationContainerParent, false);
         mNotificationContainerParent.addView(keyguardStatusView, statusIndex);
+        // When it's reinflated, this is centered by default. If it shouldn't be, this will update
+        // below when resources are updated.
+        mStatusViewCentered = true;
         attachSplitShadeMediaPlayerContainer(
                 keyguardStatusView.findViewById(R.id.status_view_media_container));