private void getMetricsWithSize(
      DisplayMetrics outMetrics,
      CompatibilityInfo compatInfo,
      Configuration configuration,
      int width,
      int height) {
    outMetrics.densityDpi = outMetrics.noncompatDensityDpi = logicalDensityDpi;
    outMetrics.density =
        outMetrics.noncompatDensity = logicalDensityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
    outMetrics.scaledDensity = outMetrics.noncompatScaledDensity = outMetrics.density;
    outMetrics.xdpi = outMetrics.noncompatXdpi = physicalXDpi;
    outMetrics.ydpi = outMetrics.noncompatYdpi = physicalYDpi;

    width =
        (configuration != null
                && configuration.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED)
            ? (int) ((configuration.screenWidthDp * outMetrics.density) + 0.5f)
            : width;
    height =
        (configuration != null
                && configuration.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED)
            ? (int) ((configuration.screenHeightDp * outMetrics.density) + 0.5f)
            : height;

    outMetrics.noncompatWidthPixels = outMetrics.widthPixels = width;
    outMetrics.noncompatHeightPixels = outMetrics.heightPixels = height;

    if (!compatInfo.equals(CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO)) {
      compatInfo.applyToDisplayMetrics(outMetrics);
    }
  }
  public void updateLayoutParams(boolean inDialog) {
    LayoutParams layoutParams = getLayoutParams();
    DisplayMetrics metrics = getDisplayMetrics(getContext());
    log.debug("Maximum display resolution: {} X {}\n", metrics.widthPixels, metrics.heightPixels);
    if (inDialog) {
      metrics.widthPixels -= 40;
      metrics.heightPixels -= 40;
    }
    int[] params = getDisplayParameters(metrics.widthPixels, metrics.heightPixels);
    layoutParams.width = params[0];
    layoutParams.height = params[1];
    setLayoutParams(layoutParams);

    if (showing)
      nativeResize(metrics.widthPixels, metrics.heightPixels, params[0], params[1], 0, 0);
    else initDrawer(metrics.widthPixels, metrics.heightPixels, params[0], params[1], 0, 0);
  }
  /**
   * Initializes and acquires the scene, creating various Android objects such as context, inflater,
   * and parser.
   *
   * @param timeout the time to wait if another rendering is happening.
   * @return whether the scene was prepared
   * @see #acquire(long)
   * @see #release()
   */
  public Result init(long timeout) {
    // acquire the lock. if the result is null, lock was just acquired, otherwise, return
    // the result.
    Result result = acquireLock(timeout);
    if (result != null) {
      return result;
    }

    // setup the ParserFactory
    ParserFactory.setParserFactory(mParams.getLayoutlibCallback().getParserFactory());

    HardwareConfig hardwareConfig = mParams.getHardwareConfig();

    // setup the display Metrics.
    DisplayMetrics metrics = new DisplayMetrics();
    metrics.densityDpi = metrics.noncompatDensityDpi = hardwareConfig.getDensity().getDpiValue();

    metrics.density =
        metrics.noncompatDensity = metrics.densityDpi / (float) DisplayMetrics.DENSITY_DEFAULT;

    metrics.scaledDensity = metrics.noncompatScaledDensity = metrics.density;

    metrics.widthPixels = metrics.noncompatWidthPixels = hardwareConfig.getScreenWidth();
    metrics.heightPixels = metrics.noncompatHeightPixels = hardwareConfig.getScreenHeight();
    metrics.xdpi = metrics.noncompatXdpi = hardwareConfig.getXdpi();
    metrics.ydpi = metrics.noncompatYdpi = hardwareConfig.getYdpi();

    RenderResources resources = mParams.getResources();

    // build the context
    mContext =
        new BridgeContext(
            mParams.getProjectKey(),
            metrics,
            resources,
            mParams.getAssets(),
            mParams.getLayoutlibCallback(),
            getConfiguration(),
            mParams.getTargetSdkVersion(),
            mParams.isRtlSupported());

    setUp();

    return SUCCESS.createResult();
  }
  private static void setDisplayMetrics(Context context) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    DisplayMetricsHolder.setWindowDisplayMetrics(displayMetrics);

    DisplayMetrics screenDisplayMetrics = new DisplayMetrics();
    screenDisplayMetrics.setTo(displayMetrics);
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    // Get the real display metrics if we are using API level 17 or higher.
    // The real metrics include system decor elements (e.g. soft menu bar).
    //
    // See:
    // http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
    if (Build.VERSION.SDK_INT >= 17) {
      display.getRealMetrics(screenDisplayMetrics);
    } else {
      // For 14 <= API level <= 16, we need to invoke getRawHeight and getRawWidth to get the real
      // dimensions.
      // Since react-native only supports API level 16+ we don't have to worry about other cases.
      //
      // Reflection exceptions are rethrown at runtime.
      //
      // See:
      // http://stackoverflow.com/questions/14341041/how-to-get-real-screen-height-and-width/23861333#23861333
      try {
        Method mGetRawH = Display.class.getMethod("getRawHeight");
        Method mGetRawW = Display.class.getMethod("getRawWidth");
        screenDisplayMetrics.widthPixels = (Integer) mGetRawW.invoke(display);
        screenDisplayMetrics.heightPixels = (Integer) mGetRawH.invoke(display);
      } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
        throw new RuntimeException("Error getting real dimensions for API level < 17", e);
      }
    }
    DisplayMetricsHolder.setScreenDisplayMetrics(screenDisplayMetrics);
  }