-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Changes from 1 commit
da60ec7
80b5396
e99afe2
e5a2ae2
3dd2a80
9738aa1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -357,6 +357,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()); | ||
|
@@ -372,6 +374,7 @@ public void onGlobalLayout() { | |
} | ||
checkForKeyboardEvents(); | ||
checkForDeviceOrientationChanges(); | ||
checkForDeviceDimensionsChanges(); | ||
} | ||
|
||
private void checkForKeyboardEvents() { | ||
|
@@ -404,13 +407,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant we have to put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Yes that seems to be the pattern. I'll fix that. Indentation for the if condition did not match the current style either as per line 147. |
||
&& 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; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make the code style consistent with existing code.