-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
135 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 86 additions & 18 deletions
104
lib/android/app/src/main/java/com/reactnativenavigation/utils/StatusBarUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,107 @@ | ||
package com.reactnativenavigation.utils | ||
|
||
import android.content.Context | ||
import android.app.Activity | ||
import android.graphics.Rect | ||
import android.os.Build | ||
import android.view.View | ||
import android.view.Window | ||
import android.view.WindowManager | ||
import com.reactnativenavigation.utils.StatusBarUtils | ||
import androidx.core.view.WindowCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
import androidx.core.view.WindowInsetsControllerCompat | ||
import kotlin.math.abs | ||
|
||
object StatusBarUtils { | ||
private const val STATUS_BAR_HEIGHT_M = 24 | ||
private const val STATUS_BAR_HEIGHT_L = 25 | ||
private var statusBarHeight = -1 | ||
|
||
@JvmStatic | ||
fun getStatusBarHeight(activity: Activity?): Int { | ||
val res= if (statusBarHeight > 0) { | ||
statusBarHeight | ||
} else { | ||
statusBarHeight = activity?.let { | ||
val rectangle = Rect() | ||
val window: Window = activity.window | ||
window.decorView.getWindowVisibleDisplayFrame(rectangle) | ||
val statusBarHeight: Int = rectangle.top | ||
val contentViewTop = window.findViewById<View>(Window.ID_ANDROID_CONTENT).top | ||
abs(contentViewTop - statusBarHeight) | ||
} ?: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) STATUS_BAR_HEIGHT_M else STATUS_BAR_HEIGHT_L | ||
statusBarHeight | ||
} | ||
logd("StatusBarHeight $res","STATUSXXXX") | ||
return res | ||
} | ||
|
||
@JvmStatic | ||
fun saveStatusBarHeight(height: Int) { | ||
statusBarHeight = height | ||
} | ||
|
||
fun getStatusBarHeight(context: Context): Int { | ||
if (statusBarHeight > 0) { | ||
return statusBarHeight | ||
@JvmStatic | ||
fun getStatusBarHeightDp(activity: Activity?): Int { | ||
return UiUtils.pxToDp(activity, getStatusBarHeight(activity).toFloat()) | ||
.toInt() | ||
} | ||
|
||
@JvmStatic | ||
fun isTranslucent(window: Window?): Boolean { | ||
return window?.let { | ||
val lp = window.attributes | ||
return lp != null && lp.flags and WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | ||
} ?: false | ||
} | ||
|
||
fun hideSystemUi(window: Window?, view: View) { | ||
window?.let { | ||
WindowCompat.setDecorFitsSystemWindows(window, false) | ||
WindowInsetsControllerCompat(window, view).let { controller -> | ||
controller.hide(WindowInsetsCompat.Type.systemBars()) | ||
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE | ||
} | ||
} | ||
val resources = context.resources | ||
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android") | ||
statusBarHeight = if (resourceId > 0) resources.getDimensionPixelSize(resourceId) else UiUtils.dpToPx( | ||
context, | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) STATUS_BAR_HEIGHT_M else STATUS_BAR_HEIGHT_L | ||
) | ||
return statusBarHeight | ||
} | ||
|
||
fun getStatusBarHeightDp(context: Context): Int { | ||
return UiUtils.pxToDp(context, getStatusBarHeight(context).toFloat()) | ||
.toInt() | ||
fun showSystemUi(window: Window?, view: View) { | ||
window?.let { | ||
WindowCompat.setDecorFitsSystemWindows(window, true) | ||
WindowInsetsControllerCompat(window, view).show(WindowInsetsCompat.Type.navigationBars()) | ||
} | ||
} | ||
|
||
fun isTranslucent(window: Window): Boolean { | ||
val lp = window.attributes | ||
return lp != null && lp.flags and WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | ||
fun hideNavigationBar(window: Window?, view: View) { | ||
window?.let { | ||
WindowCompat.setDecorFitsSystemWindows(window, false) | ||
WindowInsetsControllerCompat(window, view).let { controller -> | ||
controller.hide(WindowInsetsCompat.Type.navigationBars()) | ||
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE | ||
} | ||
} | ||
} | ||
|
||
fun showNavigationBar(window: Window?, view: View) { | ||
window?.let { | ||
WindowCompat.setDecorFitsSystemWindows(window, true) | ||
WindowInsetsControllerCompat(window, view).show(WindowInsetsCompat.Type.navigationBars()) | ||
} | ||
} | ||
|
||
fun hideStatusBar(window: Window?, view: View) { | ||
window?.let { | ||
WindowCompat.setDecorFitsSystemWindows(window, true) | ||
WindowInsetsControllerCompat(window, view).let { controller -> | ||
controller.hide(WindowInsetsCompat.Type.statusBars()) | ||
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE | ||
} | ||
} | ||
} | ||
|
||
fun showStatusBar(window: Window?, view: View) { | ||
window?.let { | ||
WindowCompat.setDecorFitsSystemWindows(window, true) | ||
WindowInsetsControllerCompat(window, view).show(WindowInsetsCompat.Type.statusBars()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters