Prevent potential NPE when updating state to bar manager

If the process of bar window is died, the associated window
will be set to null. It must be checked before using.

Fixes: 129240719
Test: manual - Play video in landscape mode and make systemui
      crash, e.g. adb shell kill -11 ${pid of systemui}. The
      result should be only the systemui is restarted.

Change-Id: If5b93e76cb856dd0dd80fb8523788f4587222c9c
(cherry picked from commit 52b5816d2ae7f5fd51d921d7f69a147c97afce4b)
diff --git a/services/core/java/com/android/server/wm/BarController.java b/services/core/java/com/android/server/wm/BarController.java
index 9bc8462..9c77c42 100644
--- a/services/core/java/com/android/server/wm/BarController.java
+++ b/services/core/java/com/android/server/wm/BarController.java
@@ -16,6 +16,8 @@
 
 package com.android.server.wm;
 
+import static android.view.Display.INVALID_DISPLAY;
+
 import static com.android.server.wm.BarControllerProto.STATE;
 import static com.android.server.wm.BarControllerProto.TRANSIENT_STATE;
 
@@ -90,6 +92,12 @@
         mWin = win;
     }
 
+    /** @return The display id of {@link #mWin}. It is used to be called from {@link #mHandler}. */
+    int getDisplayId() {
+        final WindowState win = mWin;
+        return win != null ? win.getDisplayId() : INVALID_DISPLAY;
+    }
+
     /**
      * Sets the frame within which the bar will display its content.
      *
@@ -228,8 +236,9 @@
                 @Override
                 public void run() {
                     StatusBarManagerInternal statusbar = getStatusBarInternal();
-                    if (statusbar != null) {
-                        statusbar.setWindowState(mWin.getDisplayId(), mStatusBarManagerId, state);
+                    int displayId = getDisplayId();
+                    if (statusbar != null && displayId != INVALID_DISPLAY) {
+                        statusbar.setWindowState(displayId, mStatusBarManagerId, state);
                     }
                 }
             });
diff --git a/services/core/java/com/android/server/wm/StatusBarController.java b/services/core/java/com/android/server/wm/StatusBarController.java
index 6db606d..64f1efd 100644
--- a/services/core/java/com/android/server/wm/StatusBarController.java
+++ b/services/core/java/com/android/server/wm/StatusBarController.java
@@ -16,6 +16,7 @@
 
 package com.android.server.wm;
 
+import static android.view.Display.INVALID_DISPLAY;
 import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
 import static android.view.WindowManager.LayoutParams.MATCH_PARENT;
 
@@ -36,22 +37,25 @@
 
         private Runnable mAppTransitionPending = () -> {
             StatusBarManagerInternal statusBar = getStatusBarInternal();
-            if (statusBar != null && mWin != null) {
-                statusBar.appTransitionPending(mWin.getDisplayId());
+            int displayId = getDisplayId();
+            if (statusBar != null && displayId != INVALID_DISPLAY) {
+                statusBar.appTransitionPending(displayId);
             }
         };
 
         private Runnable mAppTransitionCancelled = () -> {
             StatusBarManagerInternal statusBar = getStatusBarInternal();
-            if (statusBar != null && mWin != null) {
-                statusBar.appTransitionCancelled(mWin.getDisplayId());
+            int displayId = getDisplayId();
+            if (statusBar != null && displayId != INVALID_DISPLAY) {
+                statusBar.appTransitionCancelled(displayId);
             }
         };
 
         private Runnable mAppTransitionFinished = () -> {
             StatusBarManagerInternal statusBar = getStatusBarInternal();
-            if (statusBar != null && mWin != null) {
-                statusBar.appTransitionFinished(mWin.getDisplayId());
+            int displayId = getDisplayId();
+            if (statusBar != null && displayId != INVALID_DISPLAY) {
+                statusBar.appTransitionFinished(displayId);
             }
         };
 
@@ -65,8 +69,9 @@
                 long statusBarAnimationStartTime, long statusBarAnimationDuration) {
             mHandler.post(() -> {
                 StatusBarManagerInternal statusBar = getStatusBarInternal();
-                if (statusBar != null && mWin != null) {
-                    statusBar.appTransitionStarting(mWin.getDisplayId(),
+                int displayId = getDisplayId();
+                if (statusBar != null && displayId != INVALID_DISPLAY) {
+                    statusBar.appTransitionStarting(displayId,
                             statusBarAnimationStartTime, statusBarAnimationDuration);
                 }
             });