// Updates all existing logical displays given the current set of display devices.
  // Removes invalid logical displays.
  // Sends notifications if needed.
  private boolean updateLogicalDisplaysLocked() {
    boolean changed = false;
    for (int i = mLogicalDisplays.size(); i-- > 0; ) {
      final int displayId = mLogicalDisplays.keyAt(i);
      LogicalDisplay display = mLogicalDisplays.valueAt(i);

      mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
      display.updateLocked(mDisplayDevices);
      if (!display.isValidLocked()) {
        mLogicalDisplays.removeAt(i);
        sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
        changed = true;
      } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
        sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
        changed = true;
      }
    }
    return changed;
  }
  // Adds a new logical display based on the given display device.
  // Sends notifications if needed.
  private void addLogicalDisplayLocked(DisplayDevice device) {
    DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
    boolean isDefault = (deviceInfo.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
    if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
      Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
      isDefault = false;
    }

    if (!isDefault && mSingleDisplayDemoMode) {
      Slog.i(
          TAG,
          "Not creating a logical display for a secondary display "
              + " because single display demo mode is enabled: "
              + deviceInfo);
      return;
    }

    final int displayId = assignDisplayIdLocked(isDefault);
    final int layerStack = assignLayerStackLocked(displayId);

    LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
    display.updateLocked(mDisplayDevices);
    if (!display.isValidLocked()) {
      // This should never happen currently.
      Slog.w(
          TAG,
          "Ignoring display device because the logical display "
              + "created from it was not considered valid: "
              + deviceInfo);
      return;
    }

    mLogicalDisplays.put(displayId, display);

    // Wake up waitForDefaultDisplay.
    if (isDefault) {
      mSyncRoot.notifyAll();
    }

    sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
  }