• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# AccessibilityExtensionAbility Development
2
3The **AccessibilityExtensionAbility** module provides accessibility extension capabilities based on the **ExtensionAbility** framework. You can develop your accessibility applications by applying the **AccessibilityExtensionAbility** template to enhance usability.
4
5> **Environment Requirements**
6>
7> IDE: DevEco Studio 3.0 Beta3 (3.0.0.900) or later
8>
9> SDK: API version 9 or later
10>
11> Model: stage
12
13This document is organized as follows:
14
15- [Creating an AccessibilityExtAbility File](#creating-an-accessibility-extension-service)
16- [Processing an Accessibility Event](#processing-an-accessibility-event)
17- [Declaring Capabilities of Accessibility Extension Services](#declaring-capabilities-of-accessibility-extension-services)
18- [Enabling a Custom Accessibility Extension Service](#enabling-a-custom-accessibility-extension-service)
19
20## Creating an Accessibility Extension Service
21
22You can create an accessibility extension service by creating a project from scratch or adding the service to an existing project.
23
24### Creating a Project
25
26Perform the following steps in DevEco Studio:
271. From the upper left corner of DevEco Studio, choose **File** > **New** > **Create Project**.
282. By following the project creation wizard, click the **OpenHarmony** tab, select the **Empty Ability** template, and then click **Next**.
293. Set **Project type** to **Application**, **Compile API** (or **Compile SDK**, depending on the version used) to **9**, and **Model** to **Stage**, and then click **Finish**.
30
31### Creating an AccessibilityExtAbility File
32
33To add an accessibility extension service to a project, create the **AccessibilityExtAbility** folder in the **ets** folder of the project, create the **AccessibilityExtAbility.ts** file in the new folder, and add the following code to the new file:
34
35```typescript
36import AccessibilityExtensionAbility from '@ohos.application.AccessibilityExtensionAbility';
37
38class AccessibilityExtAbility extends AccessibilityExtensionAbility {
39    onConnect() {
40        console.log('AccessibilityExtAbility onConnect');
41    }
42
43    onDisconnect() {
44        console.log('AccessibilityExtAbility onDisconnect');
45    }
46
47    onAccessibilityEvent(accessibilityEvent) {
48        console.log('AccessibilityExtAbility onAccessibilityEvent: ' + JSON.stringify(accessibilityEvent));
49    }
50}
51
52export default AccessibilityExtAbility;
53```
54
55The APIs defined in the file are as follows.
56
57| API| Description|
58| ---- | ---- |
59| onConnect(): void | Called when a connection with the extension service is set up.|
60| onDisconnect(): void | Called when the connection with the extension service is severed.|
61| onAccessibilityEvent(event: AccessibilityEvent): void | Called when an accessibility event occurs|
62
63## Processing an Accessibility Event
64
65You can process the service logic for accessibility events in the **onAccessibilityEvent()** API. For details about the events, see [AccessibilityEvent](../reference/apis/js-apis-application-accessibilityExtensionAbility.md#accessibilityevent). The following code snippet uses the **pageStateUpdate** event as an example.
66
67```typescript
68onAccessibilityEvent(accessibilityEvent) {
69    console.log('AccessibilityExtAbility onAccessibilityEvent: ' + JSON.stringify(accessibilityEvent));
70    if (accessibilityEvent.eventType === 'pageStateUpdate') {
71        console.log('AccessibilityExtAbility onAccessibilityEvent: pageStateUpdate');
72        // TODO: Develop custom logic.
73    }
74}
75```
76For an accessibility event, you can use the APIs of the [AccessibilityExtensionContext](../reference/apis/js-apis-inner-application-accessibilityExtensionContext.md) module to configure the concerned information, obtain root information, and inject gestures.
77
78You can also process physical key events in the accessibility extension service. For details, see [onKeyEvent](../reference/apis/js-apis-application-accessibilityExtensionAbility.md#accessibilityextensionabilityonkeyevent).
79
80## Declaring Capabilities of Accessibility Extension Services
81
82After developing the custom logic for an accessibility extension service, you must add the configuration information of the service to the corresponding module-level **module.json5** file in the project directory. In the file, the **srcEntrance** tag indicates the path to the accessibility extension service. Make sure the value of the **type** tag is fixed at **accessibility**. Otherwise, the connection to the service will fail.
83
84```json
85"extensionAbilities": [
86  {
87    "name": "AccessibilityExtAbility",
88    "srcEntrance": "./ets/AccessibilityExtAbility/AccessibilityExtAbility.ts",
89    "label": "$string:MainAbility_label",
90    "description": "$string:MainAbility_desc",
91    "type": "accessibility",
92    "metadata": [
93      {
94        "name": "ohos.accessibleability",
95        "resource": "$profile:accessibility_config"
96      }
97    ]
98  }
99]
100```
101**accessibility_config** is the specific configuration of the accessibility extension service. You need to create the **accessibility_config.json** file in **resources/base/profile/** and declare the [capabilities](../reference/apis/js-apis-accessibility.md#capability) of the service in the file.
102```json
103{
104  "accessibilityCapabilities": [
105    "retrieve",
106    "gesture"
107  ]
108}
109```
110## Enabling a Custom Accessibility Extension Service
111
112To enable or disable an accessibility extension service, run the following command:
113- To enable the service: **accessibility enable -a AccessibilityExtAbility -b com.example.demo -c rg**
114- To disable the service: **accessibility disable -a AccessibilityExtAbility -b com.example.demo**
115
116In the preceding commands, **AccessibilityExtAbility** indicates the name of the accessibility extension service, **com.example.demo** indicates the bundle name, and **rg** indicates the capabilities (**r** is short for retrieve).
117
118If the service is enabled or disabled successfully, the message "enable ability successfully" or "disable ability successfully" is displayed.
119