• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { BusinessError, AsyncCallback } from '@ohos.base';
17
18native function sendAccessibilityEventSync(event: accessibility.EventInfo): void;
19
20export namespace accessibility {
21    loadLibrary("accessibility_ani");
22
23    export type EventType = 'accessibilityFocus' | 'accessibilityFocusClear' |
24        'click' | 'longClick' | 'focus' | 'select' | 'hoverEnter' | 'hoverExit' |
25        'textUpdate' | 'textSelectionUpdate' | 'scroll' | 'requestFocusForAccessibility' |
26        'announceForAccessibility';
27
28    export type Action = 'accessibilityFocus' | 'clearAccessibilityFocus' | 'focus' | 'clearFocus' | 'clearSelection' |
29        'click' | 'longClick' | 'cut' | 'copy' | 'paste' | 'select' | 'setText' | 'delete' |
30        'scrollForward' | 'scrollBackward' | 'setSelection' | 'setCursorPosition' | 'home' |
31        'back' | 'recentTask' | 'notificationCenter' | 'controlCenter' | 'common';
32
33    export type WindowUpdateType = 'add' | 'remove' | 'bounds' | 'active' | 'focus';
34
35    export type TextMoveUnit = 'char' | 'word' | 'line' | 'page' | 'paragraph';
36
37    export type observerType = 'accessibilityStateChange' | 'touchGuideStateChange' | 'screenReaderStateChange' |
38        'touchModeChange';
39
40    export native function isOpenTouchGuideSync(): boolean;
41
42    export native function isOpenAccessibilitySync(): boolean;
43
44    export native function on(type: observerType, callback: ((parameter: boolean) => void)): void;
45
46    native function offObserver(type: observerType, callback: ((parameter: boolean) => void)): void;
47
48    native function offAll(type: observerType): void;
49
50    export function off(type: observerType, callback?: ((parameter: boolean) => void)): void {
51        if (callback === undefined) {
52            offAll(type);
53        } else {
54            let callbackFunc: ((parameter: boolean) => void) = callback as ((parameter: boolean) => void);
55            offObserver(type, callbackFunc);
56        }
57    }
58
59    export class EventInfo {
60        constructor(type: EventType, bundleName: string, triggerAction: Action) {
61            this.type = type;
62            this.bundleName = bundleName;
63            this.triggerAction = triggerAction;
64        }
65        type: EventType;
66        windowUpdateType?: WindowUpdateType;
67        bundleName: string;
68        componentType?: string;
69        pageId?: number;
70        description?: string;
71        triggerAction: Action;
72        textMoveUnit?: TextMoveUnit;
73        contents?: Array<string>;
74        lastContent?: string;
75        beginIndex?: number;
76        currentIndex?: number;
77        endIndex?: number;
78        itemCount?: number;
79        elementId?: number;
80        textAnnouncedForAccessibility?: string;
81        customId?: string;
82    }
83
84    export function sendAccessibilityEvent(event: EventInfo, callback: AsyncCallback<void>): void {
85        taskpool.execute((): void => {
86            return sendAccessibilityEventSync(event);
87        })
88        .then((ret: NullishType): void => {
89            callback(new BusinessError(), ret as undefined);
90        })
91        .catch((ret: NullishType): void => {
92            callback(ret as BusinessError, undefined);
93        });
94    }
95
96    export function sendAccessibilityEvent(event: EventInfo): Promise<void> {
97        return new Promise<void>((resolve, reject): void => {
98            taskpool.execute((): void => {
99                return sendAccessibilityEventSync(event);
100            })
101            .then((ret: NullishType): void => {
102                resolve(ret as undefined);
103            })
104            .catch((ret: NullishType): void => {
105                reject(ret as BusinessError);
106            });
107        });
108    }
109}