AccessibilityInfo.android.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. 'use strict';
  11. const RCTDeviceEventEmitter = require('../../EventEmitter/RCTDeviceEventEmitter');
  12. const UIManager = require('../../ReactNative/UIManager');
  13. import NativeAccessibilityInfo from './NativeAccessibilityInfo';
  14. const REDUCE_MOTION_EVENT = 'reduceMotionDidChange';
  15. const TOUCH_EXPLORATION_EVENT = 'touchExplorationDidChange';
  16. type ChangeEventName = $Keys<{
  17. change: string,
  18. reduceMotionChanged: string,
  19. screenReaderChanged: string,
  20. ...
  21. }>;
  22. const _subscriptions = new Map();
  23. /**
  24. * Sometimes it's useful to know whether or not the device has a screen reader
  25. * that is currently active. The `AccessibilityInfo` API is designed for this
  26. * purpose. You can use it to query the current state of the screen reader as
  27. * well as to register to be notified when the state of the screen reader
  28. * changes.
  29. *
  30. * See https://reactnative.dev/docs/accessibilityinfo.html
  31. */
  32. const AccessibilityInfo = {
  33. /**
  34. * iOS only
  35. */
  36. isBoldTextEnabled: function(): Promise<boolean> {
  37. return Promise.resolve(false);
  38. },
  39. /**
  40. * iOS only
  41. */
  42. isGrayscaleEnabled: function(): Promise<boolean> {
  43. return Promise.resolve(false);
  44. },
  45. /**
  46. * iOS only
  47. */
  48. isInvertColorsEnabled: function(): Promise<boolean> {
  49. return Promise.resolve(false);
  50. },
  51. isReduceMotionEnabled: function(): Promise<boolean> {
  52. return new Promise((resolve, reject) => {
  53. if (NativeAccessibilityInfo) {
  54. NativeAccessibilityInfo.isReduceMotionEnabled(resolve);
  55. } else {
  56. reject(false);
  57. }
  58. });
  59. },
  60. /**
  61. * iOS only
  62. */
  63. isReduceTransparencyEnabled: function(): Promise<boolean> {
  64. return Promise.resolve(false);
  65. },
  66. isScreenReaderEnabled: function(): Promise<boolean> {
  67. return new Promise((resolve, reject) => {
  68. if (NativeAccessibilityInfo) {
  69. NativeAccessibilityInfo.isTouchExplorationEnabled(resolve);
  70. } else {
  71. reject(false);
  72. }
  73. });
  74. },
  75. /**
  76. * Deprecated
  77. *
  78. * Same as `isScreenReaderEnabled`
  79. */
  80. get fetch(): () => Promise<boolean> {
  81. console.warn(
  82. 'AccessibilityInfo.fetch is deprecated, call AccessibilityInfo.isScreenReaderEnabled instead',
  83. );
  84. return this.isScreenReaderEnabled;
  85. },
  86. addEventListener: function(
  87. eventName: ChangeEventName,
  88. handler: Function,
  89. ): void {
  90. let listener;
  91. if (eventName === 'change' || eventName === 'screenReaderChanged') {
  92. listener = RCTDeviceEventEmitter.addListener(
  93. TOUCH_EXPLORATION_EVENT,
  94. enabled => {
  95. handler(enabled);
  96. },
  97. );
  98. } else if (eventName === 'reduceMotionChanged') {
  99. listener = RCTDeviceEventEmitter.addListener(
  100. REDUCE_MOTION_EVENT,
  101. enabled => {
  102. handler(enabled);
  103. },
  104. );
  105. }
  106. _subscriptions.set(handler, listener);
  107. },
  108. removeEventListener: function(
  109. eventName: ChangeEventName,
  110. handler: Function,
  111. ): void {
  112. const listener = _subscriptions.get(handler);
  113. if (!listener) {
  114. return;
  115. }
  116. listener.remove();
  117. _subscriptions.delete(handler);
  118. },
  119. /**
  120. * Set accessibility focus to a react component.
  121. *
  122. * See https://reactnative.dev/docs/accessibilityinfo.html#setaccessibilityfocus
  123. */
  124. setAccessibilityFocus: function(reactTag: number): void {
  125. UIManager.sendAccessibilityEvent(
  126. reactTag,
  127. UIManager.getConstants().AccessibilityEventTypes.typeViewFocused,
  128. );
  129. },
  130. /**
  131. * Post a string to be announced by the screen reader.
  132. *
  133. * See https://reactnative.dev/docs/accessibilityinfo.html#announceforaccessibility
  134. */
  135. announceForAccessibility: function(announcement: string): void {
  136. if (NativeAccessibilityInfo) {
  137. NativeAccessibilityInfo.announceForAccessibility(announcement);
  138. }
  139. },
  140. };
  141. module.exports = AccessibilityInfo;