Prevent crash when display is already removed by the time we get displayAdded

Bug: 165165662
Change-Id: I5486b17a0a7235c922c4b5ebfa2cdc39303a8243
diff --git a/src/com/android/launcher3/util/DisplayController.java b/src/com/android/launcher3/util/DisplayController.java
index e5c8441..355c949 100644
--- a/src/com/android/launcher3/util/DisplayController.java
+++ b/src/com/android/launcher3/util/DisplayController.java
@@ -50,7 +50,7 @@
     private final ArrayList<DisplayListChangeListener> mListListeners = new ArrayList<>();
 
     private DisplayController(Context context) {
-        mDefaultDisplay = new DisplayHolder(context, DEFAULT_DISPLAY);
+        mDefaultDisplay = DisplayHolder.create(context, DEFAULT_DISPLAY);
 
         DisplayManager dm = context.getSystemService(DisplayManager.class);
         dm.registerDisplayListener(this, UI_HELPER_EXECUTOR.getHandler());
@@ -58,7 +58,11 @@
 
     @Override
     public final void onDisplayAdded(int displayId) {
-        DisplayHolder holder = new DisplayHolder(mDefaultDisplay.mDisplayContext, displayId);
+        DisplayHolder holder = DisplayHolder.create(mDefaultDisplay.mDisplayContext, displayId);
+        if (holder == null) {
+            // Display is already removed by the time we dot this.
+            return;
+        }
         synchronized (mOtherDisplays) {
             mOtherDisplays.put(displayId, holder);
         }
@@ -153,12 +157,8 @@
         private final ArrayList<DisplayInfoChangeListener> mListeners = new ArrayList<>();
         private DisplayController.Info mInfo;
 
-        public DisplayHolder(Context context, int id) {
-            DisplayManager dm = context.getSystemService(DisplayManager.class);
-            // Use application context to create display context so that it can have its own
-            // Resources.
-            mDisplayContext = context.getApplicationContext()
-                    .createDisplayContext(dm.getDisplay(id));
+        private DisplayHolder(Context displayContext) {
+            mDisplayContext = displayContext;
             // Note that the Display object must be obtained from DisplayManager which is
             // associated to the display context, so the Display is isolated from Activity and
             // Application to provide the actual state of device that excludes the additional
@@ -207,6 +207,17 @@
             }
         }
 
+        private static DisplayHolder create(Context context, int id) {
+            DisplayManager dm = context.getSystemService(DisplayManager.class);
+            Display display = dm.getDisplay(id);
+            if (display == null) {
+                return null;
+            }
+            // Use application context to create display context so that it can have its own
+            // Resources.
+            Context displayContext = context.getApplicationContext().createDisplayContext(display);
+            return new DisplayHolder(displayContext);
+        }
     }
 
     public static class Info {