Skip to content

Commit 6fac7c3

Browse files
committed
Review: Update isHighContrastModeEnabled() context extension with the use of a new available API
1 parent 9e9d006 commit 6fac7c3

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

core/src/main/java/com/orange/ouds/core/extensions/ContextExt.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,29 @@ package com.orange.ouds.core.extensions
1414

1515
import android.annotation.SuppressLint
1616
import android.content.Context
17+
import android.os.Build
1718
import android.view.accessibility.AccessibilityManager
1819
import com.orange.ouds.foundation.extensions.orElse
1920
import 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")
2526
internal 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

Comments
 (0)