From ab5bb48e6150248753fe3c50d62b80201da92768 Mon Sep 17 00:00:00 2001 From: kubasz Date: Sat, 24 Aug 2019 20:36:17 +0200 Subject: [PATCH] Fix navbar detection --- .../pl/szczodrzynski/navlib/SystemBarsUtil.kt | 11 ++--- .../java/pl/szczodrzynski/navlib/Utils.kt | 41 +++++++++++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/SystemBarsUtil.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/SystemBarsUtil.kt index 5867c8a..1a0cc7b 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/SystemBarsUtil.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/SystemBarsUtil.kt @@ -7,13 +7,16 @@ import android.graphics.Color import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION_CODES.* import android.util.Log -import android.view.* +import android.view.View import android.view.View.* +import android.view.Window +import android.view.WindowManager import androidx.core.graphics.ColorUtils import androidx.core.view.ViewCompat import com.mikepenz.materialize.util.KeyboardUtil -class SystemBarsUtil(val activity: Activity) { + +class SystemBarsUtil(private val activity: Activity) { companion object { private const val COLOR_TRANSPARENT = Color.TRANSPARENT /** @@ -310,9 +313,7 @@ class SystemBarsUtil(val activity: Activity) { var navigationBarSize = 0 - val hasMenuKey = ViewConfiguration.get(activity).hasPermanentMenuKey() - val hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK) - if (!hasMenuKey && !hasBackKey && targetAppFullscreen) { + if (hasNavigationBar(resources) && targetAppFullscreen) { val orientation = resources.configuration.orientation val navigationBarRes = when { diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/Utils.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/Utils.kt index 1767a69..40f7223 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/Utils.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/Utils.kt @@ -3,13 +3,17 @@ package pl.szczodrzynski.navlib import android.app.Activity import android.content.Context import android.content.res.Configuration -import android.graphics.Color +import android.content.res.Resources +import android.graphics.drawable.Drawable import android.os.Build import android.util.TypedValue import android.view.View import androidx.annotation.AttrRes -import androidx.annotation.ColorInt -import androidx.core.graphics.ColorUtils +import androidx.annotation.DrawableRes +import com.google.android.material.elevation.ElevationOverlayProvider +import com.mikepenz.iconics.IconicsColor +import com.mikepenz.iconics.IconicsDrawable + /*private val displayMetrics by lazy { context.resources.displayMetrics @@ -76,6 +80,37 @@ fun blendColors(background: Int, foreground: Int): Int { return (0xff000000 or (red.toLong() shl 16) or (green.toLong() shl 8) or (blue.toLong())).toInt() } +fun elevateSurface(context: Context, dp: Int): Int { + ElevationOverlayProvider(context).apply { + return compositeOverlay(themeSurfaceColor, dp * context.resources.displayMetrics.density) + } +} + fun isTablet(c: Context): Boolean { return (c.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE +} + +fun hasNavigationBar(resources: Resources): Boolean { + val id = resources.getIdentifier("config_showNavigationBar", "bool", "android") + var hasNavigationBar = id > 0 && resources.getBoolean(id) + + // Allow a system property to override this. Used by the emulator. + // See also hasNavigationBar(). + val navBarOverride = System.getProperty("qemu.hw.mainkeys") + if (navBarOverride == "1") + hasNavigationBar = true + else if (navBarOverride == "0") hasNavigationBar = false + + return hasNavigationBar +} + +fun IconicsDrawable.colorAttr(context: Context, @AttrRes attrRes: Int) = color(IconicsColor.colorInt(getColorFromAttr(context, attrRes))) + +fun Context.getDrawableFromRes(@DrawableRes id: Int): Drawable { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + resources.getDrawable(id, theme) + } + else { + resources.getDrawable(id) + } } \ No newline at end of file