|
11 | 11 |
|
12 | 12 | import javax.annotation.Nullable; |
13 | 13 |
|
14 | | -import android.app.Activity; |
15 | 14 | import android.content.Context; |
16 | 15 | import android.graphics.Rect; |
17 | 16 | import android.os.Bundle; |
18 | 17 | import android.util.AttributeSet; |
| 18 | +import android.util.DisplayMetrics; |
19 | 19 | import android.view.MotionEvent; |
20 | 20 | import android.view.Surface; |
21 | 21 | import android.view.View; |
@@ -346,6 +346,10 @@ private void checkForDeviceOrientationChanges() { |
346 | 346 | return; |
347 | 347 | } |
348 | 348 | mDeviceRotation = rotation; |
| 349 | + // It's important to repopulate DisplayMetrics and export them before emitting the |
| 350 | + // orientation change event, so that the Dimensions object returns the correct new values. |
| 351 | + DisplayMetricsHolder.initDisplayMetrics(getContext()); |
| 352 | + emitUpdateDimensionsEvent(); |
349 | 353 | emitOrientationChanged(rotation); |
350 | 354 | } |
351 | 355 |
|
@@ -384,6 +388,30 @@ private void emitOrientationChanged(final int newRotation) { |
384 | 388 | sendEvent("namedOrientationDidChange", map); |
385 | 389 | } |
386 | 390 |
|
| 391 | + private void emitUpdateDimensionsEvent() { |
| 392 | + DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics(); |
| 393 | + DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics(); |
| 394 | + |
| 395 | + WritableMap windowDisplayMetricsMap = Arguments.createMap(); |
| 396 | + windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels); |
| 397 | + windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels); |
| 398 | + windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density); |
| 399 | + windowDisplayMetricsMap.putDouble("fontScale", windowDisplayMetrics.scaledDensity); |
| 400 | + windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi); |
| 401 | + |
| 402 | + WritableMap screenDisplayMetricsMap = Arguments.createMap(); |
| 403 | + screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels); |
| 404 | + screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels); |
| 405 | + screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density); |
| 406 | + screenDisplayMetricsMap.putDouble("fontScale", screenDisplayMetrics.scaledDensity); |
| 407 | + screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi); |
| 408 | + |
| 409 | + WritableMap dimensionsMap = Arguments.createMap(); |
| 410 | + dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap); |
| 411 | + dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap); |
| 412 | + sendEvent("didUpdateDimensions", dimensionsMap); |
| 413 | + } |
| 414 | + |
387 | 415 | private void sendEvent(String eventName, @Nullable WritableMap params) { |
388 | 416 | if (mReactInstanceManager != null) { |
389 | 417 | mReactInstanceManager.getCurrentReactContext() |
|
0 commit comments