About the impact of setting high-contrast text on the mobile phone on the app and the solution

In the accessibility settings of Android phones, there is a high-contrast copy setting, which aims to improve the readability of the page, which is to make it easier for users to see the text on the screen. However, different mobile phones implement this function differently, which leads to the phenomenon that the text disappears when this function is turned on.

After this setting is turned on, all colorful text will turn into white or black according to the depth of the color. In addition to this, some mobile phones will add a black stroke to all white text, and a white stroke to all black text. This kind of processing is better, and the text under the white background is not also white.

The problem is that some of Huawei's mobile phones will only change the font color to either white or black, and the light gray text on the white background will become white text, and nothing can be seen.

There are two ways to solve this problem.

The first is to optimize the UI design scheme. Don’t design text colors that are too light. This requires you to know what color will be turned into white and what color will be turned into black when the high-contrast text function is enabled.

The second option is to directly prohibit the app from responding to high-contrast text settings. From the perspective of user interaction, this is unfriendly and inappropriate. After all, people who enable this function must not be able to read well enough. The original barrier-free function is to help. If it is prohibited, it is too unethical. But if you really want to prohibit it, you need to determine whether the implementation plan is feasible.

Option One

Conclusion: By comparing Xiaomi mobile phones and Huawei mobile phones, it is found that the dividing points of black and white values ​​​​for different mobile phones are inconsistent. The approximate range can be determined, but the demarcation point cannot be given accurately.

First test by setting the font color to pure black and white. In the screenshot below, the background color is the original color of the font, and the font color is the modified color of the font when high-contrast text is turned on.

for (i in 16..255 step 1) {
    val v = TextView(this)
    v.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
    v.gravity = Gravity.CENTER
    val six1 = i.toString(16)
    val str = "#${six1}${six1}${six1}"
    v.text = str
    v.setTextColor(Color.parseColor(str))
    v.setBackgroundColor(Color.parseColor(str))

    parentLl.addView(v)
}

Mi 10s 11 system phone: the cut-off point is #7F7F7F

Huawei mobile phone P40 mobile phone: the dividing point is #C5C5C5

Modify the code, set the font color to red, and check the cut-off point of the color value

for (i in 16..255 step 1) {
    val v = TextView(this)
    v.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
    v.gravity = Gravity.CENTER
    val six1 = i.toString(16)
    val str = "#FF${six1}${six1}"
    v.text = str
    v.setTextColor(Color.parseColor(str))
    v.setBackgroundColor(Color.parseColor(str))

    parentLl.addView(v)
}

Mi 10s 11 system mobile phone: the dividing point is #FF4040

Huawei P40 phones: The cutoff is #FFA9A9

Through comparison, it is found that the color of Xiaomi mobile phones will be processed into a larger range of white, but it will add a white stroke, which will not affect reading. The color area of ​​Huawei mobile phones processed into white is smaller, which is basically the upper left corner of the color picker A small part, so it is good to avoid this part when UI design is required.

The range of suggestions we can give about the UI is:

1. The gray value reference point, don't give a gray that is lighter than #C5C5C5

2. No matter what font color, avoid the color value at the upper left corner of the color picker, which is about the red box

It is impossible to find out the specific black-and-white change logic. It can also be seen through comparison that different mobile phones have different calculation logics for high-contrast ratios. Therefore, we can only get a general understanding of high-contrast text through the changes in grayscale and red on the two mobile phones. basic principles.

Option II:

Conclusion: Logically feasible, but banned by the system.

When searching for information, I found a blog about the new auxiliary functions of Android 5.0 (high-contrast text/color correction/color inversion) learning_Momo9518's blog-CSDN blog_android high contrast

According to the timing diagram, it is found that the app's processing of high-contrast text is in the red box.

Finally find the key logic in ViewRootImpl

final AccessibilityManager mAccessibilityManager;

mHighContrastTextManager = new HighContrastTextManager();
mAccessibilityManager.addHighTextContrastStateChangeListener(
        mHighContrastTextManager, mHandler);

Among them, the key class is AccessibilityManager, which can be obtained through getSystemService in the app, so it is assumed that this object can be obtained, and an empty implementation listener can be added through the addHighTextContrastStateChangeListener method, so as to realize the high-contrast text adjustment of the mobile phone by app shielding set up.

Because the addHighTextContrastStateChangeListener method is annotated by @hide, it is called by reflection:

val a: AccessibilityManager =
    getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
a.addAccessibilityStateChangeListener {
    Toast.makeText(this, "hello", Toast.LENGTH_SHORT)
    Log.i("xiaxiao", "hello aaaaa")
}
val classes = a.javaClass.declaredClasses
var highTextContrastChangeListenerClass: Class<*>? = null
classes.forEach {
    if (it.simpleName.contains("HighTextContrastChangeListener")) {
        highTextContrastChangeListenerClass = it
    }
}
val m: Method = a.javaClass.getMethod(
    "addHighTextContrastStateChangeListener",
    highTextContrastChangeListenerClass,
    Handler::class.java
)
m.invoke(a, null, Handler(mainLooper))

But NoSuchMethodException is reported.

Later, it was found that after android9.0, the reflection of the hide method was restricted, and some classes and methods could not be reflected after loading the blacklist. Reference: https://developer.android.com/guide/app-compatibility/restrictions-non-sdk-interfaces#determine-list

Look for addHighTextContrastStateChangeListener in the table, and it is found that it has been added to the blacklist and cannot be reflected, so the app cannot block the high-contrast text function by setting an empty listener.

This concludes this basic exploration of high-contrast text, and hopefully gave you a general idea.

Guess you like

Origin blog.csdn.net/xx23x/article/details/124317711