9

I would like to know if Android has any flag to be added into configChanges in an Activity attribute in the AndroidManifest for modifications in the Invert Colors option of the device.

The android doc shows the following flags: - "mcc"
- "mnc"
- "locale"
- "touchscreen"
- "keyboard"
- "keyboardHidden"
- "navigation"
- "screenLayout"
- "fontScale"
- "uiMode" // this one is for the dark mode
- "orientation"
- "density"
- "screenSize"
- "smallestScreenSize"

But none of them deal with it.


Invert colors option:

enter image description here

Augusto Carmo
  • 4,386
  • 2
  • 28
  • 61
  • 1
    I do not see "invert colors" in Settings on Android 10 (Pixel 3) or Android 9 (Galaxy S9). Where are you seeing this option? – CommonsWare Jan 28 '20 at 14:58
  • @CommonsWare, I've just updated my question with an image :) – Augusto Carmo Jan 28 '20 at 15:54
  • I was goingto say `"uiMode"` and if that doesn't work, then "invert colors" is something that device manufacturer has added by customising Android, you will need to go read their custom SDK docs rather than vanilla Android docs. What Make Model & Version of Android have you screenshotted that off? – Blundell Jan 28 '20 at 15:59
  • I think that isn't an Android customization. "Invert colors" is available on Android 10 on Pixel 3 XL – Ayer Ribeiro de Souza Netto Jan 28 '20 at 16:08
  • 2
    Thanks for pointing that out! Playing with it, I suspect that is purely a GPU thing and is not actually a configuration change, but that's just a guess. Does your activity undergo a configuration change when this is toggled? – CommonsWare Jan 28 '20 at 17:33
  • @CommonsWare, I just tested it again and unfortunately the `onConfigurationChanged(Configuration)` is not fired when an inversion of colors modification occurs =/. It would be nice if it were, as it's directly related to the ui. – Augusto Carmo Jan 29 '20 at 09:09
  • 1
    AFAIK, `onConfigurationChanged()` would only get fired if you had the right `android:configChanges` attribute and opted out of the default configuration change behavior. Is your activity destroyed and recreated after switching the "invert colors" toggle? – CommonsWare Jan 29 '20 at 12:06
  • @CommonsWare, it is. – Augusto Carmo Jan 29 '20 at 12:24
  • 1
    OK, so it's definitely a configuration change. Unless there's some system message in Logcat, I don't know how to determine what sort of configuration change it is and how you would opt out of it via `android:configChanges`. If you tried all the documented ones without success, then either the `android:configChanges` value is undocumented or does not exist (i.e., you can't opt out of it)... and neither of those are great options. :-( – CommonsWare Jan 29 '20 at 12:48
  • 1
    @CommonsWare I'm afraid Android does not have a flag for the color inversion modification. I tested with all the flags available in the docs (`mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|fontScale|uiMode|orientation|density|screenSize|smallestScreenSize`) and none made the `onConfigurationChanged` callback be fired =/. Anyways, I would like to thank you a bunch for your attention, time and willing to help \o/ – Augusto Carmo Jan 29 '20 at 14:21

1 Answers1

1

If you need to check the state of inverted colors I see just two possible solutions.

Manual check. Taken from this question:
Get enable/disable status and of accessibility Colour inversion mode

fun isInversionModeEnabled(): Boolean {
        var isInversionEnabled = false
        val accessibilityEnabled = try {
            Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED)
        } catch (e: Settings.SettingNotFoundException) {
            Log.d(TAG, "Error finding setting ACCESSIBILITY_DISPLAY_INVERSION_ENABLED: " + e.message)
            Log.d(TAG, "Checking negative color enabled status")
            val SEM_HIGH_CONTRAST = "high_contrast"
            Settings.System.getInt(contentResolver, SEM_HIGH_CONTRAST, 0)
        }
        if (accessibilityEnabled == 1) {
            Log.d(TAG, "inversion  or negative colour is enabled")
            isInversionEnabled = true
        } else {
            Log.d(TAG, "inversion  or negative colour is disabled")
        }
        return isInversionEnabled
    }

And also you can use AccessibilityService.
On inversion color changed i've got such event:

EventType: TYPE_VIEW_CLICKED; EventTime: 170718119; PackageName: com.android.systemui; 
MovementGranularity: 0; Action: 0; ContentChangeTypes: []; WindowChangeTypes: [] [
ClassName: android.widget.Switch; Text: [Invert colors]; ContentDescription: On;

So i can check the current state somehow like this:

override fun onAccessibilityEvent(event: AccessibilityEvent) {
        val isInvertedColorsChanged = event.text.contains("Invert colors")
        if (isInvertedColorsChanged) {
            val isEnabled = event.contentDescription == "On"
            //do what you need
        }
    }

I'm not sure it will work for every device.
I've never done it before, so maybe there is a better solution.

Merov
  • 1,028
  • 1
  • 14
  • 29