From a1fc18d0362202dee81392f9adb03820424aa9c1 Mon Sep 17 00:00:00 2001 From: Johan Reitan Date: Fri, 9 Apr 2021 11:29:21 +0200 Subject: [PATCH] Add isNavigationBarContrastEnforced API --- .../systemuicontroller/SystemUiControllerTest.kt | 11 +++++++++-- .../systemuicontroller/SystemUiController.kt | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/systemuicontroller/src/androidTest/java/com/google/accompanist/systemuicontroller/SystemUiControllerTest.kt b/systemuicontroller/src/androidTest/java/com/google/accompanist/systemuicontroller/SystemUiControllerTest.kt index 634e72328..817b19fb1 100644 --- a/systemuicontroller/src/androidTest/java/com/google/accompanist/systemuicontroller/SystemUiControllerTest.kt +++ b/systemuicontroller/src/androidTest/java/com/google/accompanist/systemuicontroller/SystemUiControllerTest.kt @@ -191,9 +191,13 @@ class SystemUiControllerTest { val view = composeTestRule.contentView val window = composeTestRule.activity.window - // Now create an AndroidSystemUiController() and set the navigation bar with dark icons - // and enforce contrast + // Now create an AndroidSystemUiController() val controller = AndroidSystemUiController(view) + + // Assert that the contrast is not enforced initially + assertThat(controller.isNavigationBarContrastEnforced()).isFalse() + + // and set the navigation bar with dark icons and enforce contrast controller.setNavigationBarColor( Color.Transparent, darkIcons = true, @@ -209,6 +213,9 @@ class SystemUiControllerTest { // Assert that the system applied the contrast enforced property assertThat(window.isNavigationBarContrastEnforced).isTrue() + + // Assert that the controller reflects that the contrast is enforced + assertThat(controller.isNavigationBarContrastEnforced()).isTrue() } } diff --git a/systemuicontroller/src/main/java/com/google/accompanist/systemuicontroller/SystemUiController.kt b/systemuicontroller/src/main/java/com/google/accompanist/systemuicontroller/SystemUiController.kt index 453aaa5ce..cfa7d7856 100644 --- a/systemuicontroller/src/main/java/com/google/accompanist/systemuicontroller/SystemUiController.kt +++ b/systemuicontroller/src/main/java/com/google/accompanist/systemuicontroller/SystemUiController.kt @@ -97,6 +97,14 @@ interface SystemUiController { transformColorForLightContent ) } + + /** + * Returns whether the system is ensuring that the navigation bar has enough contrast when a + * fully transparent background is requested. + * + * @return true, if API is 29+ and the system is ensuring contrast, false otherwise. + */ + fun isNavigationBarContrastEnforced(): Boolean = false } /** @@ -160,6 +168,11 @@ class AndroidSystemUiController(view: View) : SystemUiController { } } + override fun isNavigationBarContrastEnforced(): Boolean { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q + && window?.isNavigationBarContrastEnforced == true + } + private fun Context.findWindow(): Window? { var context = this while (context is ContextWrapper) {