Incorrect displayCutout in landscape orientation when you hold your device at some angle

耗尽温柔 提交于 2021-02-05 11:23:11

问题


So we can get for example safe inset top value (useful for devices which have notch/cutout):

override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        val safeInsetTop = window.decorView.rootWindowInsets?.displayCutout?.safeInsetTop
        if (DEBUG) Timber.d("onAttachedToWindow, safeInsetTop: $safeInsetTop")
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (DEBUG) Timber.d("onCreate")

    if (prefAlwaysLandscape) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        if (resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE) {
            return // activity will be recreated
        }
    }
    ...
}

When it works normally (when you hold your phone straight - no angle):

Logs

onAttachedToWindow, safeInsetTop: 0
onCreate

When it works bad (when you hold your phone at some angle):

Logs

onAttachedToWindow, safeInsetTop: 112
onCreate

You can see there is 112 value instead of 0, which is stupid, in landscape orientation top safe value can't be more than 0, there is no notch, it's only possible for portrait orientation

So when the phone at angle and activity starts it thinks that it's in portrait orientation, but later configuration changes (to landscape) but I'm stuck with old 112 value, which is incorrect for landscape orientation

I don't know the solution for this issue

window.decorView.rootWindowInsets?.displayCutout?.safeInsetTop can be called only from onAttachedToWindow to return non null value

in onCreate, onStart or onResume it will return null...

Update

I found this windowManager.defaultDisplay.cutout?.safeInsetTop from WindowInsets.getDisplayCutout is NULL everywhere except within onAttachedToWindow in fullscreen Java app

I can call it in onCreate but still value is 112 when phone at angle

So it doesn't matter where you get it...

来源:https://stackoverflow.com/questions/65620959/incorrect-displaycutout-in-landscape-orientation-when-you-hold-your-device-at-so

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!