简体   繁体   中英

how to detect accessibility settings on android is enabled/disabled

I'm particularly interested in high contrast text , color correction , and magnification settings. I did some research online, couldn't find what I want. I saw one answer about detecting high contrast text :

AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);
boolean isHighTextContrastEnabled = am.isHighTextContrastEnabled();

But somehow it gives me the error for isHighTextContrastEnabled() saying that it is undefined for the type AccessibilityManager.

Also didn't find solution for the other two settings detection.

    AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);

    Class clazz = am.getClass();
    Method m = null;
    try {
        m = clazz.getMethod("isHighTextContrastEnabled",null);
    } catch (NoSuchMethodException e) {
        Log.w("FAIL", "isHighTextContrastEnabled not found in AccessibilityManager");
    }


    Object result = null;
    try {
        result = m.invoke(am, null);
        if (result != null && result instanceof Boolean)  {
            Boolean b = (Boolean)result;
            Log.d("result", "b =" + b);
        }
    }  catch (Exception e) {
        android.util.Log.d("fail",  "isHighTextContrastEnabled invoked with an exception" + e.getMessage());
        return;
    }

and I do test, it return false, so it works

What a did was

private fun checkForAcessibility(): Boolean {
    try {
        val accessibilityManager = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
        val accessibilityManagerClass = accessibilityManager.javaClass
        val isHighTextContrastEnabledMethod = accessibilityManagerClass.getMethod("isHighTextContrastEnabled")
        val result: Any = isHighTextContrastEnabledMethod.invoke(accessibilityManager) ?: return AccessibilityEnabledValue.ERROR_QUERYING_VALUE

        if (result !is Boolean) {
            return AccessibilityEnabledValue.ERROR_QUERYING_VALUE
        }

        return if (result) {
            AccessibilityEnabledValue.TRUE
        } else {
            AccessibilityEnabledValue.FALSE
        }
    } catch (e: Exception) {
        return AccessibilityEnabledValue.ERROR_QUERYING_VALUE
    }
}




enum class AccessibilityEnabledValue(val value: String) {
    TRUE("true"),
    FALSE("false"),
    ERROR_QUERYING_VALUE("error_querying_value")
}

I've noticed that isHighTextContrastEnabled() method does not contain parameters.

use this for color correction:

int color_correction_enabled = 0;

try { color_correction_enabled = Settings.Secure.getInt(this.getContentResolver(), "accessibility_display_daltonizer_enabled");

} catch (Exception e) {

color_correction_enabled = 0; // means default false }

Though reflection solution helps with some devices I was unable to get "isHighTextContrastEnabled" via reflection on my OnePlus 5 device. So I ended up with another method when reflection fails. Just rendering text on a bitmap and then manually checking if there is a specific color (different from black and white) in the bitmap (cause in "High Contrast" mode it will be black and white).

Here is the code in Kotlin:

private fun isHighTextContrastEnabledWithBmp(): Boolean {
    val bmp = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bmp)
    val testColor = Color.GREEN

    val paint = Paint().apply {
        color = testColor
        textSize = 5f
    }

    canvas.drawText("1", 0f, 10f, paint)
    val pixels = IntArray(bmp.width * bmp.height)
    bmp.getPixels(
        pixels, 0, bmp.width,
        0, 0, bmp.height, bmp.width
    )
    val result = pixels.any { it == testColor }.not()

    return result
}

Using androidx.core:core-ktx 's getSystemService extension,

isHighTextContrastEnabled = runCatching {
    context.getSystemService<AccessibilityManager>()?.let {
        (it.javaClass.getMethod("isHighTextContrastEnabled").invoke(it) as? Boolean)
    }
}.onFailure { it.printStackTrace() }.getOrNull() ?: false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM