Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3f40c68

Browse files
authored
Fix AccessibilityFeatures.disableAnimations flag on Android 12+ (#53428)
This PR fixes the problem where Flutter would not respect the "remove animation" accessibility setting on Android 12+. Please see this issue for details: flutter/flutter#130976 As [mentioned](flutter/flutter#130976 (comment)) by [horsemankukka](https://github.com/horsemankukka), the problem has to do with reading `Settings.Global.TRANSITION_ANIMATION_SCALE` as a string instead of a float. Flutter would compare it to the string "0" to determine if animations should be disabled. Presumably, this worked because the settings app did indeed use the string "0" or "1" for this setting. But as of Android 12 it's instead written using float representation ("0.0" or "1.0"), at least on Samsung devices. [The documentation](https://developer.android.com/reference/android/provider/Settings.Global#TRANSITION_ANIMATION_SCALE) also states that this setting should be read as a float, which is what this PR does. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent d1506c1 commit 3f40c68

2 files changed

Lines changed: 80 additions & 5 deletions

File tree

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public class AccessibilityBridge extends AccessibilityNodeProvider {
126126
// Font weight adjustment for bold text. FontWeight.Bold - FontWeight.Normal = w700 - w400 = 300.
127127
private static final int BOLD_TEXT_WEIGHT_ADJUSTMENT = 300;
128128

129+
// Default transition animation scale (animations enabled)
130+
private static final float DEFAULT_TRANSITION_ANIMATION_SCALE = 1.0f;
131+
132+
// Transition animation scale when animations are disabled
133+
private static final float DISABLED_TRANSITION_ANIMATION_SCALE = 0.0f;
134+
129135
/// Value is derived from ACTION_TYPE_MASK in AccessibilityNodeInfo.java
130136
private static int FIRST_RESOURCE_ID = 267386881;
131137

@@ -399,11 +405,13 @@ public void onChange(boolean selfChange, Uri uri) {
399405
return;
400406
}
401407
// Retrieve the current value of TRANSITION_ANIMATION_SCALE from the OS.
402-
String value =
403-
Settings.Global.getString(
404-
contentResolver, Settings.Global.TRANSITION_ANIMATION_SCALE);
408+
float value =
409+
Settings.Global.getFloat(
410+
contentResolver,
411+
Settings.Global.TRANSITION_ANIMATION_SCALE,
412+
DEFAULT_TRANSITION_ANIMATION_SCALE);
405413

406-
boolean shouldAnimationsBeDisabled = value != null && value.equals("0");
414+
boolean shouldAnimationsBeDisabled = value == DISABLED_TRANSITION_ANIMATION_SCALE;
407415
if (shouldAnimationsBeDisabled) {
408416
accessibilityFeatureFlags |= AccessibilityFeature.DISABLE_ANIMATIONS.value;
409417
} else {
@@ -560,7 +568,7 @@ private void setBoldTextFlag() {
560568
if (shouldBold) {
561569
accessibilityFeatureFlags |= AccessibilityFeature.BOLD_TEXT.value;
562570
} else {
563-
accessibilityFeatureFlags &= AccessibilityFeature.BOLD_TEXT.value;
571+
accessibilityFeatureFlags &= ~AccessibilityFeature.BOLD_TEXT.value;
564572
}
565573
sendLatestAccessibilityFlagsToFlutter();
566574
}

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.mockito.ArgumentMatchers.eq;
1414
import static org.mockito.Mockito.any;
1515
import static org.mockito.Mockito.anyInt;
16+
import static org.mockito.Mockito.atLeastOnce;
1617
import static org.mockito.Mockito.doAnswer;
1718
import static org.mockito.Mockito.mock;
1819
import static org.mockito.Mockito.never;
@@ -28,8 +29,10 @@
2829
import android.content.Context;
2930
import android.content.res.Configuration;
3031
import android.content.res.Resources;
32+
import android.database.ContentObserver;
3133
import android.graphics.Rect;
3234
import android.os.Bundle;
35+
import android.provider.Settings;
3336
import android.text.SpannableString;
3437
import android.text.SpannedString;
3538
import android.text.style.LocaleSpan;
@@ -1346,6 +1349,31 @@ public void itSetsBoldTextFlagCorrectly() {
13461349
/*platformViewsAccessibilityDelegate=*/ null);
13471350

13481351
verify(mockChannel).setAccessibilityFeatures(1 << 3);
1352+
reset(mockChannel);
1353+
1354+
// Now verify that clearing the BOLD_TEXT flag doesn't touch any of the other flags.
1355+
// Ensure the DISABLE_ANIMATION flag will be set
1356+
Settings.Global.putFloat(null, "transition_animation_scale", 0.0f);
1357+
// Ensure the BOLD_TEXT flag will be cleared
1358+
config.fontWeightAdjustment = 0;
1359+
1360+
accessibilityBridge =
1361+
setUpBridge(
1362+
/*rootAccessibilityView=*/ mockRootView,
1363+
/*accessibilityChannel=*/ mockChannel,
1364+
/*accessibilityManager=*/ mockManager,
1365+
/*contentResolver=*/ null,
1366+
/*accessibilityViewEmbedder=*/ mockViewEmbedder,
1367+
/*platformViewsAccessibilityDelegate=*/ null);
1368+
1369+
// setAccessibilityFeatures() will be called multiple times from AccessibilityBridge's
1370+
// constructor, verify that the latest argument is correct
1371+
ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
1372+
verify(mockChannel, atLeastOnce()).setAccessibilityFeatures(captor.capture());
1373+
assertEquals(1 << 2 /* DISABLE_ANIMATION */, captor.getValue().intValue());
1374+
1375+
// Set back to default
1376+
Settings.Global.putFloat(null, "transition_animation_scale", 1.0f);
13491377
}
13501378

13511379
@Test
@@ -2012,6 +2040,45 @@ public void itProducesPlatformViewNodeForVirtualDisplay() {
20122040
verify(accessibilityViewEmbedder).getRootNode(eq(embeddedView), eq(0), any(Rect.class));
20132041
}
20142042

2043+
@Test
2044+
public void testItSetsDisableAnimationsFlagBasedOnTransitionAnimationScale() {
2045+
AccessibilityChannel mockChannel = mock(AccessibilityChannel.class);
2046+
ContentResolver mockContentResolver = mock(ContentResolver.class);
2047+
2048+
AccessibilityBridge accessibilityBridge =
2049+
setUpBridge(
2050+
/*rootAccessibilityView=*/ null,
2051+
/*accessibilityChannel=*/ mockChannel,
2052+
/*accessibilityManager=*/ null,
2053+
/*contentResolver=*/ mockContentResolver,
2054+
/*accessibilityViewEmbedder=*/ null,
2055+
/*platformViewsAccessibilityDelegate=*/ null);
2056+
2057+
// Capture the observer registered for Settings.Global.TRANSITION_ANIMATION_SCALE
2058+
ArgumentCaptor<ContentObserver> observerCaptor = ArgumentCaptor.forClass(ContentObserver.class);
2059+
verify(mockContentResolver)
2060+
.registerContentObserver(
2061+
eq(Settings.Global.getUriFor(Settings.Global.TRANSITION_ANIMATION_SCALE)),
2062+
eq(false),
2063+
observerCaptor.capture());
2064+
ContentObserver observer = observerCaptor.getValue();
2065+
2066+
// Initial state
2067+
verify(mockChannel).setAccessibilityFeatures(0);
2068+
reset(mockChannel);
2069+
2070+
// Animations are disabled
2071+
Settings.Global.putFloat(mockContentResolver, "transition_animation_scale", 0.0f);
2072+
observer.onChange(false);
2073+
verify(mockChannel).setAccessibilityFeatures(1 << 2);
2074+
reset(mockChannel);
2075+
2076+
// Animations are enabled
2077+
Settings.Global.putFloat(mockContentResolver, "transition_animation_scale", 1.0f);
2078+
observer.onChange(false);
2079+
verify(mockChannel).setAccessibilityFeatures(0);
2080+
}
2081+
20152082
@Test
20162083
public void releaseDropsChannelMessageHandler() {
20172084
AccessibilityChannel mockChannel = mock(AccessibilityChannel.class);

0 commit comments

Comments
 (0)