1 | import { Application } from '../application';
|
2 | import { Observable } from '../data/observable';
|
3 | import { Trace } from '../trace';
|
4 | import * as Utils from '../utils';
|
5 | import { SDK_VERSION } from '../utils/constants';
|
6 | import { CommonA11YServiceEnabledObservable, SharedA11YObservable } from './accessibility-service-common';
|
7 | export function getAndroidAccessibilityManager() {
|
8 | const context = Utils.ad.getApplicationContext();
|
9 | if (!context) {
|
10 | return null;
|
11 | }
|
12 | return context.getSystemService(android.content.Context.ACCESSIBILITY_SERVICE);
|
13 | }
|
14 | const accessibilityStateEnabledPropName = 'accessibilityStateEnabled';
|
15 | const touchExplorationStateEnabledPropName = 'touchExplorationStateEnabled';
|
16 | class AndroidSharedA11YObservable extends SharedA11YObservable {
|
17 |
|
18 | get accessibilityServiceEnabled() {
|
19 | return !!this[accessibilityStateEnabledPropName] && !!this[touchExplorationStateEnabledPropName];
|
20 | }
|
21 | set accessibilityServiceEnabled(v) {
|
22 | return;
|
23 | }
|
24 | }
|
25 | let accessibilityStateChangeListener;
|
26 | let touchExplorationStateChangeListener;
|
27 | let sharedA11YObservable;
|
28 | function updateAccessibilityState() {
|
29 | const accessibilityManager = getAndroidAccessibilityManager();
|
30 | if (!accessibilityManager) {
|
31 | sharedA11YObservable.set(accessibilityStateEnabledPropName, false);
|
32 | sharedA11YObservable.set(touchExplorationStateEnabledPropName, false);
|
33 | return;
|
34 | }
|
35 | sharedA11YObservable.set(accessibilityStateEnabledPropName, !!accessibilityManager.isEnabled());
|
36 | sharedA11YObservable.set(touchExplorationStateEnabledPropName, !!accessibilityManager.isTouchExplorationEnabled());
|
37 | }
|
38 | function ensureStateListener() {
|
39 | if (sharedA11YObservable) {
|
40 | return sharedA11YObservable;
|
41 | }
|
42 | const accessibilityManager = getAndroidAccessibilityManager();
|
43 | sharedA11YObservable = new AndroidSharedA11YObservable();
|
44 | if (!accessibilityManager) {
|
45 | sharedA11YObservable.set(accessibilityStateEnabledPropName, false);
|
46 | sharedA11YObservable.set(touchExplorationStateEnabledPropName, false);
|
47 | return sharedA11YObservable;
|
48 | }
|
49 | accessibilityStateChangeListener = new android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener({
|
50 | onAccessibilityStateChanged(enabled) {
|
51 | updateAccessibilityState();
|
52 | if (Trace.isEnabled()) {
|
53 | Trace.write(`AccessibilityStateChangeListener state changed to: ${!!enabled}`, Trace.categories.Accessibility);
|
54 | }
|
55 | },
|
56 | });
|
57 | accessibilityManager.addAccessibilityStateChangeListener(accessibilityStateChangeListener);
|
58 | if (SDK_VERSION >= 19) {
|
59 | touchExplorationStateChangeListener = new android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener({
|
60 | onTouchExplorationStateChanged(enabled) {
|
61 | updateAccessibilityState();
|
62 | if (Trace.isEnabled()) {
|
63 | Trace.write(`TouchExplorationStateChangeListener state changed to: ${!!enabled}`, Trace.categories.Accessibility);
|
64 | }
|
65 | },
|
66 | });
|
67 | accessibilityManager.addTouchExplorationStateChangeListener(touchExplorationStateChangeListener);
|
68 | }
|
69 | updateAccessibilityState();
|
70 | Application.on(Application.resumeEvent, updateAccessibilityState);
|
71 | Application.on(Application.exitEvent, (args) => {
|
72 | const activity = args.android;
|
73 | if (activity && !activity.isFinishing()) {
|
74 | return;
|
75 | }
|
76 | const accessibilityManager = getAndroidAccessibilityManager();
|
77 | if (accessibilityManager) {
|
78 | if (accessibilityStateChangeListener) {
|
79 | accessibilityManager.removeAccessibilityStateChangeListener(accessibilityStateChangeListener);
|
80 | }
|
81 | if (touchExplorationStateChangeListener) {
|
82 | accessibilityManager.removeTouchExplorationStateChangeListener(touchExplorationStateChangeListener);
|
83 | }
|
84 | }
|
85 | accessibilityStateChangeListener = null;
|
86 | touchExplorationStateChangeListener = null;
|
87 | if (sharedA11YObservable) {
|
88 | sharedA11YObservable.removeEventListener(Observable.propertyChangeEvent);
|
89 | sharedA11YObservable = null;
|
90 | }
|
91 | Application.off(Application.resumeEvent, updateAccessibilityState);
|
92 | });
|
93 | return sharedA11YObservable;
|
94 | }
|
95 | export function isAccessibilityServiceEnabled() {
|
96 | return ensureStateListener().accessibilityServiceEnabled;
|
97 | }
|
98 | export class AccessibilityServiceEnabledObservable extends CommonA11YServiceEnabledObservable {
|
99 | constructor() {
|
100 | super(ensureStateListener());
|
101 | }
|
102 | }
|
103 |
|
\ | No newline at end of file |