Skip to content

Add AccessibilityInfo.getContrastLevel and contrastLevelChanged on Android - #59181

Open
camilopires wants to merge 11 commits into
react:mainfrom
camilopires:a11y/android-contrast-level
Open

camilopires wants to merge 11 commits into
react:mainfrom
camilopires:a11y/android-contrast-level

Conversation

@camilopires

Copy link
Copy Markdown
Contributor

Summary:

Android 14 (API 34) added a system contrast level (Settings → Accessibility → Color and motion → Contrast), from -1 to 1, read with UiModeManager.getContrast(). It is Android's equivalent of iOS Increase Contrast, which React Native already exposes as isDarkerSystemColorsEnabled(). The existing Android method, isHighTextContrastEnabled(), reads a different setting that only changes how text is drawn.

This adds AccessibilityInfo.getContrastLevel(): Promise<number> and a numeric contrastLevelChanged event, for Android only.

  • A ContrastChangeListener is registered while the app is resumed, and removed on pause and in invalidate().
  • The value is read again on resume, because registering doesn't report the current value.
  • The listener is created lazily, so older Android versions never load the API 34 interface.
  • On Android before 14 and on iOS, the method resolves to 0.
  • The TypeScript declarations add an addEventListener overload, because this event's payload is a number rather than a boolean.

RNTester gets a small example that shows the value and the last event's payload.

Discussion: react-native-community/discussions-and-proposals#1026

Changelog:

[ANDROID] [ADDED] - AccessibilityInfo.getContrastLevel() and contrastLevelChanged (Android 14+ contrast level)

Test Plan:

  • yarn jest packages/react-native/Libraries/Components/AccessibilityInfo: all tests pass
  • yarn flow-check: 0 errors
  • yarn lint: passes
  • yarn build-types --validate: API snapshot up to date
  • yarn test-generated-typescript and yarn test-typescript-legacy: pass
  • yarn format-check-kotlin: passes
  • RNTester builds: ./gradlew :packages:rn-tester:android:app:assembleDebug

RNTester on an Android 16 (API 36) emulator:

  • Getter: adb shell settings put secure contrast_level with 0, 0.5, 1.0 and -0.5 gave "Contrast level is 0." / "0.5." / "1." / "-0.5."
  • Events: Android recreates the activity when the contrast changes, so I logged the listener temporarily, locally and not in this PR. Setting 0.25, -0.75 and 0.0 logged contrastLevelChanged with exactly 0.25, -0.75 and 0, all in the same process.
  • Changed while in the background: set 0.75, then brought the app back. The row showed "Contrast level is 0.75."

RNTester on an Android 10 (API 29) emulator: "Contrast level is 0.", with no crashes across launch and resume.

@meta-cla

meta-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

Hi @camilopires!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@github-actions

Copy link
Copy Markdown

Warning

JavaScript API change detected

This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API.

  • Please include a clear changelog message.
  • This change will be subject to additional review.

This change was flagged as: POTENTIALLY_BREAKING

@meta-cla

meta-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 18, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 18, 2026
@meta-codesync

meta-codesync Bot commented Oct 19, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D126038114.

camilopires and others added 3 commits October 24, 2026 00:52
…droid

Android 14 (API 34) exposes the system contrast level, from -1 to 1, through
UiModeManager.getContrast(). It is the Android equivalent of Increase
Contrast on iOS; isHighTextContrastEnabled reads a different setting that
only changes how text is drawn. Register a ContrastChangeListener while the
app is resumed and read the value again on resume. Earlier versions and iOS
resolve to 0.

Adds Jest tests for the getter and the numeric event, and an RNTester
example that shows the value and the last event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Android NativeAccessibilityInfo spec's getContrastLevel now appears in
the generated spec, so it shows up in the Apple snapshots. Generated with
Doxygen 1.16.1 and Python 3.12, as in validate-cxx-api-snapshots.yml.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Robolectric doesn't call contrast listeners, so on API 34 the test serves
a mock UiModeManager and calls the listener the module registers. It
checks that a change sends contrastLevelDidChange once (including a
negative level), resuming twice registers one listener, pausing and
invalidating remove it, and a change made while paused is sent on
resume. On API 33 it uses Robolectric's UiModeManager and checks the
level stays 0 with no event. Removing the listener registration makes
five of the seven tests fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@camilopires
camilopires force-pushed the a11y/android-contrast-level branch from 17016d3 to 93ae838 Compare October 24, 2026 00:08
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at 544a52180048.

android: no capture

camilopires and others added 7 commits October 24, 2026 14:40
The Android contrast listener now lives in a per-module
ContrastListenerApi34 helper, and the value Android passes to it is used
instead of being read again. Every path (start-up, resume, listener)
goes through one normaliser that maps NaN to 0 and clamps to -1..1.

invalidate() removes the lifecycle listener first, sets an invalidated
flag that resume and queued contrast callbacks check, and unregisters
the listener on the UI thread, posting when called from another thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The docs now say that iOS always resolves 0 and points to
isDarkerSystemColorsEnabled, that this is the closest Android
counterpart to Increase Contrast, that 0 also means Android before 14
or no UiModeManager, and that a missing native method rejects. The
d.ts event overload notes API 34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The row shows 'loading' until the starting read finishes, and
'Unavailable: <message>' if it rejects. A starting read that finishes
after an event no longer overwrites the newer value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A NaN from the contrast listener or the read on resume no longer sends
contrastLevelChanged(0); the module keeps its last level and sends
nothing. NaN is still the standard contrast, 0, at construction, where
there is no earlier value. NaN and out-of-range values each warn once
on their own. invalidate() no longer posts the listener removal when
there is no listener, and its warning names the module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test now emits both contrastLevelDidChange and contrastLevelChanged,
so mapping the event to either name on iOS would call the handler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@camilopires

Copy link
Copy Markdown
Contributor Author

Pushed a few follow-ups from a review pass (no API changes):

  • The contrast level is clamped to -1…1 on every path. NaN maps to 0 only at start-up; after that, a NaN keeps the last good value instead of reporting standard contrast.
  • The listener now lives in a per-module @RequiresApi(UPSIDE_DOWN_CAKE) helper instead of an Any? field, uses the value the platform passes in, and is torn down on the UI thread in invalidate(), with queued callbacks ignored afterwards.
  • The docs say plainly that iOS always resolves 0 (use isDarkerSystemColorsEnabled there), and that a missing native module or method rejects.
  • Robolectric tests cover the listener, clamping, NaN, pause/resume and teardown (21 tests).

CI compiles tests with -PenableWarningsAsErrors=true, and
BridgeReactContext is deprecated, so the test failed to compile there.
Suppress the deprecation for the file, as DeviceInfoModuleTest and the
other tests that use BridgeReactContext do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant