Skip to content

Commit

Permalink
Update navigationBarHeight calculation logic to handle different navi…
Browse files Browse the repository at this point in the history
…gation bar states correctly
  • Loading branch information
vshkl committed Jul 27, 2021
1 parent d47a334 commit 4c76a0e
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions alerter/src/main/java/com/tapadoo/alerter/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.tapadoo.alerter
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.Context
import android.content.res.Resources
import android.graphics.*
import android.graphics.drawable.Drawable
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.util.Log
import android.view.*
import android.view.animation.Animation
Expand Down Expand Up @@ -118,9 +120,36 @@ class Alert @JvmOverloads constructor(context: Context,

val layoutContainer: View? by lazy { findViewById<View>(R.id.vAlertContentContainer) }

private val currentDisplay: Display? by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.display
} else {
@Suppress("DEPRECATION")
(context.applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
}
}

private val usableScreenHeight: Int
get() = Resources.getSystem().displayMetrics.heightPixels

private val physicalScreenHeight: Int
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics().also { currentDisplay?.getRealMetrics(it) }.heightPixels
} else {
usableScreenHeight
}

private val cutoutsHeight: Int
get() = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ->
currentDisplay?.cutout?.run { safeInsetTop + safeInsetBottom } ?: 0
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P ->
rootWindowInsets?.displayCutout?.run { safeInsetTop + safeInsetBottom } ?: 0
else -> 0
}

private val navigationBarHeight by lazy {
val dimenId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
resources.getDimensionPixelSize(dimenId)
physicalScreenHeight - usableScreenHeight - cutoutsHeight
}

init {
Expand Down

0 comments on commit 4c76a0e

Please sign in to comment.