Skip to content

Commit

Permalink
Add isNavigationBarContrastEnforced API
Browse files Browse the repository at this point in the history
  • Loading branch information
joharei committed Apr 9, 2021
1 parent 97d98ef commit a1fc18d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a1fc18d

Please sign in to comment.