@@ -14,25 +14,29 @@ package com.orange.ouds.core.extensions
1414
1515import android.annotation.SuppressLint
1616import android.content.Context
17+ import android.os.Build
1718import android.view.accessibility.AccessibilityManager
1819import com.orange.ouds.foundation.extensions.orElse
1920import com.orange.ouds.foundation.extensions.tryOrNull
2021
2122/* *
2223 * @return true if the high contrast mode is enabled on the device, false otherwise
2324 */
24- @SuppressLint(" DiscouragedPrivateApi " , " PrivateApi" )
25+ @SuppressLint(" PrivateApi" )
2526internal fun Context.isHighContrastModeEnabled (): Boolean {
2627 val accessibilityManager = getSystemService(Context .ACCESSIBILITY_SERVICE ) as AccessibilityManager
2728
28- return with (accessibilityManager) {
29- // Workaround to access mIsHighTextContrastEnabled field because it is hidden with a @UnsupportedAppUsage annotation
30- // On some OS versions this field is named mIsHighContrastTextEnabled
31- // TODO Use high text contrast APIs once API level 36 is available
32- tryOrNull { javaClass.getDeclaredField(" mIsHighTextContrastEnabled" ) }
33- .orElse { tryOrNull { javaClass.getDeclaredField(" mIsHighContrastTextEnabled" ) } }
34- ?.also { it.isAccessible = true }
35- ?.getBoolean(this )
36- .orElse { false }
29+ return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .BAKLAVA ) {
30+ accessibilityManager.isHighContrastTextEnabled
31+ } else {
32+ with (accessibilityManager) {
33+ // Workaround to access mIsHighTextContrastEnabled field because it is hidden with a @UnsupportedAppUsage annotation
34+ // On some OS versions this field is named mIsHighContrastTextEnabled
35+ tryOrNull { javaClass.getDeclaredField(" mIsHighTextContrastEnabled" ) }
36+ .orElse { tryOrNull { javaClass.getDeclaredField(" mIsHighContrastTextEnabled" ) } }
37+ ?.also { it.isAccessible = true }
38+ ?.getBoolean(this )
39+ .orElse { false }
40+ }
3741 }
3842}
0 commit comments