Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate window dimensions change from orientation #15181

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
Expand Down Expand Up @@ -357,6 +358,8 @@ private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLay

private int mKeyboardHeight = 0;
private int mDeviceRotation = 0;
private DisplayMetrics mWindowMetrics = new DisplayMetrics();
private DisplayMetrics mScreenMetrics = new DisplayMetrics();

/* package */ CustomGlobalLayoutListener() {
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(getContext().getApplicationContext());
Expand All @@ -372,6 +375,7 @@ public void onGlobalLayout() {
}
checkForKeyboardEvents();
checkForDeviceOrientationChanges();
checkForDeviceDimensionsChanges();
}

private void checkForKeyboardEvents() {
Expand Down Expand Up @@ -404,13 +408,37 @@ private void checkForDeviceOrientationChanges() {
return;
}
mDeviceRotation = rotation;
// It's important to repopulate DisplayMetrics and export them before emitting the
// orientation change event, so that the Dimensions object returns the correct new values.
DisplayMetricsHolder.initDisplayMetrics(getContext());
emitUpdateDimensionsEvent();
emitOrientationChanged(rotation);
}

private void checkForDeviceDimensionsChanges() {
// Get current display metrics.
DisplayMetricsHolder.initDisplayMetrics(getContext());
// Check changes to both window and screen display metrics since they may not update at the same time.
if (!areMetricsEqual(mWindowMetrics, DisplayMetricsHolder.getWindowDisplayMetrics()) ||
!areMetricsEqual(mScreenMetrics, DisplayMetricsHolder.getScreenDisplayMetrics())) {
mWindowMetrics.setTo(DisplayMetricsHolder.getWindowDisplayMetrics());
mScreenMetrics.setTo(DisplayMetricsHolder.getScreenDisplayMetrics());
emitUpdateDimensionsEvent();
}
}

private boolean areMetricsEqual(DisplayMetrics displayMetrics, DisplayMetrics otherMetrics) {
if (Build.VERSION.SDK_INT >= 17) {
return displayMetrics.equals(otherMetrics);
} else {
// DisplayMetrics didn't have an equals method before API 17.
// Check all public fields manually.
return displayMetrics.widthPixels == otherMetrics.widthPixels &&
displayMetrics.heightPixels == otherMetrics.heightPixels &&
displayMetrics.density == otherMetrics.density &&
displayMetrics.densityDpi == otherMetrics.densityDpi &&
displayMetrics.scaledDensity == otherMetrics.scaledDensity &&
displayMetrics.xdpi == otherMetrics.xdpi &&
displayMetrics.ydpi == otherMetrics.ydpi;
}
}

private void emitOrientationChanged(final int newRotation) {
String name;
double rotationDegrees;
Expand Down