-
Notifications
You must be signed in to change notification settings - Fork 24.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add isAccessibilityServiceEnabled
#31396
Closed
grgr-dkrk
wants to merge
9
commits into
facebook:main
from
grgr-dkrk:feat/add_accessibility_state_listener
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5521d78
feat: add `isAccessibilityStateEnabled`
grgr-dkrk 1537588
accessibilityState -> accessibilityService
grgr-dkrk 4b4ec0e
change ACCESSIBILITY_STATE_EVENT_NAME to ACCESSIBILITY_SERVICE_EVENT_…
grgr-dkrk 6fe11f8
change type of `isAccessibilityServiceEnabled`
grgr-dkrk 4bb94c8
fix suggestion
grgr-dkrk ac05878
fix: suggestion
grgr-dkrk ccc99be
Merge branch 'main' into feat/add_accessibility_state_listener
grgr-dkrk 1e641c1
chore: fix accessibilityExample for flow check
grgr-dkrk ab66bd2
fix(accessibilityInfo): fix suggestions
grgr-dkrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,19 @@ public void onTouchExplorationStateChanged(boolean enabled) { | |
} | ||
} | ||
|
||
// Android can listen for accessibility service enable with `accessibilityStateChange`, but | ||
// `accessibilityState` conflicts with React Native props and confuses developers. Therefore, the | ||
// name `accessibilityServiceChange` is used here instead. | ||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
private class ReactAccessibilityServiceChangeListener | ||
implements AccessibilityManager.AccessibilityStateChangeListener { | ||
|
||
@Override | ||
public void onAccessibilityStateChanged(boolean enabled) { | ||
updateAndSendAccessibilityServiceChangeEvent(enabled); | ||
} | ||
} | ||
|
||
// Listener that is notified when the global TRANSITION_ANIMATION_SCALE. | ||
private final ContentObserver animationScaleObserver = | ||
new ContentObserver(new Handler(Looper.getMainLooper())) { | ||
|
@@ -64,13 +77,16 @@ public void onChange(boolean selfChange, Uri uri) { | |
|
||
private @Nullable AccessibilityManager mAccessibilityManager; | ||
private @Nullable ReactTouchExplorationStateChangeListener mTouchExplorationStateChangeListener; | ||
private @Nullable ReactAccessibilityServiceChangeListener mAccessibilityServiceChangeListener; | ||
private final ContentResolver mContentResolver; | ||
private boolean mReduceMotionEnabled = false; | ||
private boolean mTouchExplorationEnabled = false; | ||
private boolean mAccessibilityServiceEnabled = false; | ||
private int mRecommendedTimeout; | ||
|
||
private static final String REDUCE_MOTION_EVENT_NAME = "reduceMotionDidChange"; | ||
private static final String TOUCH_EXPLORATION_EVENT_NAME = "touchExplorationDidChange"; | ||
private static final String ACCESSIBILITY_SERVICE_EVENT_NAME = "accessibilityServiceDidChange"; | ||
|
||
public AccessibilityInfoModule(ReactApplicationContext context) { | ||
super(context); | ||
|
@@ -79,8 +95,10 @@ public AccessibilityInfoModule(ReactApplicationContext context) { | |
(AccessibilityManager) appContext.getSystemService(Context.ACCESSIBILITY_SERVICE); | ||
mContentResolver = getReactApplicationContext().getContentResolver(); | ||
mTouchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled(); | ||
mAccessibilityServiceEnabled = mAccessibilityManager.isEnabled(); | ||
mReduceMotionEnabled = this.getIsReduceMotionEnabledValue(); | ||
mTouchExplorationStateChangeListener = new ReactTouchExplorationStateChangeListener(); | ||
mAccessibilityServiceChangeListener = new ReactAccessibilityServiceChangeListener(); | ||
} | ||
|
||
@Override | ||
|
@@ -106,6 +124,11 @@ public void isTouchExplorationEnabled(Callback successCallback) { | |
successCallback.invoke(mTouchExplorationEnabled); | ||
} | ||
|
||
@Override | ||
public void isAccessibilityServiceEnabled(Callback successCallback) { | ||
successCallback.invoke(mAccessibilityServiceEnabled); | ||
} | ||
|
||
private void updateAndSendReduceMotionChangeEvent() { | ||
boolean isReduceMotionEnabled = this.getIsReduceMotionEnabledValue(); | ||
|
||
|
@@ -134,16 +157,31 @@ private void updateAndSendTouchExplorationChangeEvent(boolean enabled) { | |
} | ||
} | ||
|
||
private void updateAndSendAccessibilityServiceChangeEvent(boolean enabled) { | ||
if (mAccessibilityServiceEnabled != enabled) { | ||
mAccessibilityServiceEnabled = enabled; | ||
|
||
ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn(); | ||
if (reactApplicationContext != null) { | ||
getReactApplicationContext() | ||
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | ||
.emit(ACCESSIBILITY_SERVICE_EVENT_NAME, mAccessibilityServiceEnabled); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@@ -176,2 +178 @@
- mAccessibilityManager.addAccessibilityStateChangeListener(
- mAccessibilityServiceChangeListener);
+ mAccessibilityManager.addAccessibilityStateChangeListener(mAccessibilityServiceChangeListener);
|
||
public void onHostResume() { | ||
mAccessibilityManager.addTouchExplorationStateChangeListener( | ||
mTouchExplorationStateChangeListener); | ||
mAccessibilityManager.addAccessibilityStateChangeListener(mAccessibilityServiceChangeListener); | ||
|
||
Uri transitionUri = Settings.Global.getUriFor(Settings.Global.TRANSITION_ANIMATION_SCALE); | ||
mContentResolver.registerContentObserver(transitionUri, false, animationScaleObserver); | ||
|
||
updateAndSendTouchExplorationChangeEvent(mAccessibilityManager.isTouchExplorationEnabled()); | ||
updateAndSendAccessibilityServiceChangeEvent(mAccessibilityManager.isEnabled()); | ||
updateAndSendReduceMotionChangeEvent(); | ||
} | ||
|
||
|
@@ -152,6 +190,8 @@ public void onHostResume() { | |
public void onHostPause() { | ||
mAccessibilityManager.removeTouchExplorationStateChangeListener( | ||
mTouchExplorationStateChangeListener); | ||
mAccessibilityManager.removeAccessibilityStateChangeListener( | ||
mAccessibilityServiceChangeListener); | ||
|
||
mContentResolver.unregisterContentObserver(animationScaleObserver); | ||
} | ||
|
@@ -160,6 +200,7 @@ public void onHostPause() { | |
public void initialize() { | ||
getReactApplicationContext().addLifecycleEventListener(this); | ||
updateAndSendTouchExplorationChangeEvent(mAccessibilityManager.isTouchExplorationEnabled()); | ||
updateAndSendAccessibilityServiceChangeEvent(mAccessibilityManager.isEnabled()); | ||
updateAndSendReduceMotionChangeEvent(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Point by @philIip that this comment should also mention that it's an Android only feature