Repository navigation
Add AccessibilityInfo.getContrastLevel and contrastLevelChanged on Android - #59181
camilopires wants to merge 11 commits into
Conversation
|
Hi @camilopires! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Warning JavaScript API change detected This PR commits an update to
This change was flagged as: |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D126038114. |
…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>
17016d3 to
93ae838
Compare
Visual regressionNo visual differences at android: no capture |
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>
|
Pushed a few follow-ups from a review pass (no API changes):
|
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>
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 asisDarkerSystemColorsEnabled(). The existing Android method,isHighTextContrastEnabled(), reads a different setting that only changes how text is drawn.This adds
AccessibilityInfo.getContrastLevel(): Promise<number>and a numericcontrastLevelChangedevent, for Android only.ContrastChangeListeneris registered while the app is resumed, and removed on pause and ininvalidate().0.addEventListeneroverload, 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()andcontrastLevelChanged(Android 14+ contrast level)Test Plan:
yarn jest packages/react-native/Libraries/Components/AccessibilityInfo: all tests passyarn flow-check: 0 errorsyarn lint: passesyarn build-types --validate: API snapshot up to dateyarn test-generated-typescriptandyarn test-typescript-legacy: passyarn format-check-kotlin: passes./gradlew :packages:rn-tester:android:app:assembleDebugRNTester on an Android 16 (API 36) emulator:
adb shell settings put secure contrast_levelwith 0, 0.5, 1.0 and -0.5 gave "Contrast level is 0." / "0.5." / "1." / "-0.5."contrastLevelChangedwith exactly 0.25, -0.75 and 0, all in the same process.RNTester on an Android 10 (API 29) emulator: "Contrast level is 0.", with no crashes across launch and resume.