问题描述
系统打开自动转屏,
app中设置 android:screenOrientation="portrait"为0度(竖屏)
实测 app仍然随着手机转动而转动
问题分析
通过观察 此手机 的独特性是其屏幕尺寸 长宽相等 是个square方屏。
问题解决
代码中的问题 记录如下
frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java
private boolean mIgnoreRotationForApps;
void configureDisplayPolicy() {
final int width = mBaseDisplayWidth;
final int height = mBaseDisplayHeight;
final int shortSize;
final int longSize;
if (width > height) {
shortSize = height;
longSize = width;
} else {
shortSize = width;
longSize = height;
}
final int shortSizeDp = shortSize * DENSITY_DEFAULT / mBaseDisplayDensity;
final int longSizeDp = longSize * DENSITY_DEFAULT / mBaseDisplayDensity;
mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
mIgnoreRotationForApps = isNonDecorDisplayCloseToSquare(Surface.ROTATION_0, width, height);
}
private boolean isNonDecorDisplayCloseToSquare(int rotation, int width, int height) {
final DisplayCutout displayCutout =
calculateDisplayCutoutForRotation(rotation).getDisplayCutout();
final int uiMode = mWmService.mPolicy.getUiMode();
final int w = mDisplayPolicy.getNonDecorDisplayWidth(
width, height, rotation, uiMode, displayCutout);
final int h = mDisplayPolicy.getNonDecorDisplayHeight(
width, height, rotation, uiMode, displayCutout);
final float aspectRatio = Math.max(w, h) / (float) Math.min(w, h);
return aspectRatio <= mCloseToSquareMaxAspectRatio;
}
@Override
int getOrientation() {
final WindowManagerPolicy policy = mWmService.mPolicy;
if (mIgnoreRotationForApps) {
return SCREEN_ORIENTATION_USER;
}
if (mWmService.mDisplayFrozen) {
if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
+ " is frozen, return " + mLastWindowForcedOrientation);
return mLastWindowForcedOrientation;
} else if (policy.isKeyguardLocked()) {
if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
+ " is frozen while keyguard locked, return " + mLastOrientation);
return mLastOrientation;
}
} else {
final int orientation = mAboveAppWindowsContainers.getOrientation();
if (orientation != SCREEN_ORIENTATION_UNSET) {
return orientation;
}
}
return mTaskStackContainers.getOrientation();
}
总结
如果屏幕是接近方形。 将忽略app的rotation设置
|