how to detect accessibility settings on android is enabled/disabled

前端 未结 5 1314
清歌不尽
清歌不尽 2021-01-28 12:34

I\'m particularly interested in high contrast text, color correction, and magnification settings. I did some research online, couldn\'t fi

相关标签:
5条回答
  • 2021-01-28 12:53

    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 }

    0 讨论(0)
  • 2021-01-28 12:54

    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
    }
    
    0 讨论(0)
  • 2021-01-28 12:57

    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.

    0 讨论(0)
  • 2021-01-28 12:59

    Maybe can be considered sloppy, using androidx.core:core-ktx's getSystemService extension,

    isHighTextContrastEnabled = try {
        context.getSystemService<AccessibilityManager>()?.run {
            (javaClass.getMethod("isHighTextContrastEnabled").invoke(this) as? Boolean)
        } ?: false
    } catch (e: Exception) {
        e.printStackTrace()
        false
    }
    
    0 讨论(0)
  • 2021-01-28 13:09
        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

    0 讨论(0)
提交回复
热议问题