Skip to content

Commit 016b17b

Browse files
authored
[android] Isolate API-34 high contrast listener in AccessibilityBridge behind Api34Impl (flutter#192502)
Isolates API-34 `UiModeManager.ContrastChangeListener` and related contrast calls in `AccessibilityBridge` inside an `@RequiresApi(API_LEVELS.API_34)` static holder class (`Api34Impl`) with `@DoNotInline`. ### Background / Root Cause On Android 12 and 13 (API < 34), `AccessibilityBridge` previously evaluated a ternary merge point in `<init>` between a lambda cast to `(ContrastChangedListener & UiModeManager.ContrastChangeListener)` and a no-op fallback. D8 desugars this intersection cast into a synthetic class implementing `UiModeManager.ContrastChangeListener`. Because this ternary join occurs in `<init>`, the ART verifier attempts to resolve the synthetic class and its unimplemented interface when loading `AccessibilityBridge`, failing with `NoClassDefFoundError: Failed resolution of: Lt2;` (`ClassNotFoundException: android.app.UiModeManager$ContrastChangeListener`). By moving all references to `UiModeManager.ContrastChangeListener` into `Api34Impl` and holding the observer as an `Object` reference on `AccessibilityBridge`, ART never attempts to load or verify the API-34 types on devices with API < 34. Fixes flutter#192231 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant in-code documentation (doc comments with `///`). - [ ] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- *This pull request was generated with Gemini / AI assistance.*
1 parent a0b6113 commit 016b17b

2 files changed

Lines changed: 83 additions & 38 deletions

File tree

‎engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java‎

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,9 @@ public void onAccessibilityStateChanged(boolean accessibilityEnabled) {
418418
private final AccessibilityManager.TouchExplorationStateChangeListener
419419
touchExplorationStateChangeListener;
420420

421-
// Listener that is notified when the high contrast mode is turned on/off.
422-
private interface ContrastChangedListener {
423-
void onContrastChanged(float contrast);
424-
}
425-
426-
private final ContrastChangedListener highContrastObserver =
427-
(Build.VERSION.SDK_INT >= API_LEVELS.API_34)
428-
? (ContrastChangedListener & UiModeManager.ContrastChangeListener)
429-
contrast -> setHighContrastFlag()
430-
: contrast -> {
431-
/* no-op */
432-
};
421+
// Listener that is notified when the high contrast mode is turned on/off on API 34+.
422+
// Stored as an Object to avoid class-verification issues on pre-API 34 devices.
423+
private final Object highContrastObserver;
433424

434425
// Listener that is notified when the invert colors flag is turned on/off.
435426
private final AccessibilityFeatureObserver invertColorsObserver;
@@ -612,8 +603,11 @@ public void onTouchExplorationStateChanged(boolean isTouchExplorationEnabled) {
612603

613604
// Initialize and register contrast listener
614605
if (Build.VERSION.SDK_INT >= API_LEVELS.API_34) {
606+
highContrastObserver =
607+
Api34Impl.registerHighContrastObserver(rootAccessibilityView.getContext(), this);
615608
setHighContrastFlag();
616-
registerHighContrastObserver(rootAccessibilityView.getContext());
609+
} else {
610+
highContrastObserver = null;
617611
}
618612

619613
platformViewsAccessibilityDelegate.attachAccessibilityBridge(this);
@@ -651,8 +645,9 @@ public void release() {
651645
touchExplorationStateChangeListener);
652646
contentResolver.unregisterContentObserver(animationScaleObserver);
653647
contentResolver.unregisterContentObserver(invertColorsObserver);
654-
if (Build.VERSION.SDK_INT >= API_LEVELS.API_34) {
655-
unregisterHighContrastObserver(rootAccessibilityView.getContext());
648+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_34 && highContrastObserver != null) {
649+
Api34Impl.unregisterHighContrastObserver(
650+
rootAccessibilityView.getContext(), highContrastObserver);
656651
}
657652
accessibilityChannel.setAccessibilityMessageHandler(null);
658653
}
@@ -720,24 +715,6 @@ private void setBoldTextFlag() {
720715
updateAccessibilityFeature(AccessibilityFeature.BOLD_TEXT, shouldBold);
721716
}
722717

723-
@RequiresApi(API_LEVELS.API_34)
724-
private void registerHighContrastObserver(Context context) {
725-
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
726-
if (uiModeManager != null) {
727-
uiModeManager.addContrastChangeListener(
728-
context.getMainExecutor(), (UiModeManager.ContrastChangeListener) highContrastObserver);
729-
}
730-
}
731-
732-
@RequiresApi(API_LEVELS.API_34)
733-
private void unregisterHighContrastObserver(Context context) {
734-
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
735-
if (uiModeManager != null) {
736-
uiModeManager.removeContrastChangeListener(
737-
(UiModeManager.ContrastChangeListener) highContrastObserver);
738-
}
739-
}
740-
741718
@RequiresApi(API_LEVELS.API_34)
742719
private void setHighContrastFlag() {
743720
Context context = rootAccessibilityView.getContext();
@@ -748,13 +725,9 @@ private void setHighContrastFlag() {
748725
return;
749726
}
750727

751-
float uiContrast = uiModeManager.getContrast();
752728
// TODO(https://github.com/flutter/flutter/issues/182863): Move contrast value to a separate API
753729
// as Android supports a range from -1.0 to 1.0, not just a boolean state.
754-
755-
// 0.0 (standard), 0.5 (medium), 1.0 (high)
756-
// Any enhancement above standard is considered high contrast
757-
boolean isHighContrastEnabled = uiContrast > 0.0f;
730+
boolean isHighContrastEnabled = Api34Impl.isHighContrast(uiModeManager);
758731

759732
updateAccessibilityFeature(AccessibilityFeature.HIGH_CONTRAST, isHighContrastEnabled);
760733
}
@@ -2999,4 +2972,42 @@ static boolean isBoldText(Configuration configuration) {
29992972
&& fontWeightAdjustment >= BOLD_TEXT_WEIGHT_ADJUSTMENT;
30002973
}
30012974
}
2975+
2976+
/**
2977+
* Isolates API-34 references so that ART's class verifier does not attempt to resolve them when
2978+
* loading {@link AccessibilityBridge} on older API levels. Without this separation, the verifier
2979+
* attempts to resolve {@link UiModeManager.ContrastChangeListener} at class-load time, causing a
2980+
* {@link NoClassDefFoundError} crash on pre-API 34 devices (issue #192231).
2981+
*/
2982+
@RequiresApi(API_LEVELS.API_34)
2983+
private static class Api34Impl {
2984+
@DoNotInline
2985+
static Object registerHighContrastObserver(
2986+
@NonNull Context context, @NonNull AccessibilityBridge bridge) {
2987+
UiModeManager uiModeManager =
2988+
(UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
2989+
if (uiModeManager == null) {
2990+
return null;
2991+
}
2992+
UiModeManager.ContrastChangeListener listener = contrast -> bridge.setHighContrastFlag();
2993+
uiModeManager.addContrastChangeListener(context.getMainExecutor(), listener);
2994+
return listener;
2995+
}
2996+
2997+
@DoNotInline
2998+
static void unregisterHighContrastObserver(@NonNull Context context, @NonNull Object observer) {
2999+
UiModeManager uiModeManager =
3000+
(UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
3001+
if (uiModeManager != null && observer instanceof UiModeManager.ContrastChangeListener) {
3002+
uiModeManager.removeContrastChangeListener((UiModeManager.ContrastChangeListener) observer);
3003+
}
3004+
}
3005+
3006+
@DoNotInline
3007+
static boolean isHighContrast(@NonNull UiModeManager uiModeManager) {
3008+
// 0.0 (standard), 0.5 (medium), 1.0 (high)
3009+
// Any enhancement above standard is considered high contrast
3010+
return uiModeManager.getContrast() > 0.0f;
3011+
}
3012+
}
30023013
}

‎engine/src/flutter/shell/platform/android/test/io/flutter/view/AccessibilityBridgeTest.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,40 @@ public void itUpdatesHighContrastFlagOnChangeListener() {
15791579
assertEquals(ACCESSIBILITY_FEATURE_NO_ANNOUNCE, featuresCaptor.getValue().intValue());
15801580
}
15811581

1582+
@Config(sdk = API_LEVELS.API_34)
1583+
@TargetApi(API_LEVELS.API_34)
1584+
@Test
1585+
public void itUnregistersHighContrastObserverOnRelease() {
1586+
AccessibilityChannel mockChannel = mock(AccessibilityChannel.class);
1587+
AccessibilityManager mockManager = mock(AccessibilityManager.class);
1588+
View mockRootView = mock(View.class);
1589+
Context context = mock(Context.class);
1590+
UiModeManager mockUiModeManager = mock(UiModeManager.class);
1591+
1592+
when(mockRootView.getContext()).thenReturn(context);
1593+
when(context.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(mockUiModeManager);
1594+
when(context.getMainExecutor())
1595+
.thenReturn(RuntimeEnvironment.getApplication().getMainExecutor());
1596+
1597+
AccessibilityBridge accessibilityBridge =
1598+
setUpBridge(
1599+
/* rootAccessibilityView= */ mockRootView,
1600+
/* accessibilityChannel= */ mockChannel,
1601+
/* accessibilityManager= */ mockManager,
1602+
/* contentResolver= */ null,
1603+
/* accessibilityViewEmbedder= */ null,
1604+
/* platformViewsAccessibilityDelegate= */ null);
1605+
1606+
ArgumentCaptor<UiModeManager.ContrastChangeListener> listenerCaptor =
1607+
ArgumentCaptor.forClass(UiModeManager.ContrastChangeListener.class);
1608+
verify(mockUiModeManager).addContrastChangeListener(any(), listenerCaptor.capture());
1609+
UiModeManager.ContrastChangeListener listener = listenerCaptor.getValue();
1610+
1611+
accessibilityBridge.release();
1612+
1613+
verify(mockUiModeManager).removeContrastChangeListener(listener);
1614+
}
1615+
15821616
@Config(sdk = API_LEVELS.API_33)
15831617
@TargetApi(API_LEVELS.API_33)
15841618
@Test

0 commit comments

Comments
 (0)