1# AccessibilityExtensionContext (System API) 2 3The **AccessibilityExtensionContext** module, inherited from **ExtensionContext**, provides context for **AccessibilityExtensionAbility**. 4 5You can use the APIs of this module to configure the concerned information, obtain root information, and inject gestures. 6 7> **NOTE** 8> 9> - The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10> - The current page contains only the system APIs of the current module. For details about other public APIs, see [AccessibilityExtensionContext](js-apis-inner-application-accessibilityExtensionContext.md). 11 12## How to Use 13 14Before using the **AccessibilityExtensionContext** module, you must define a child class that inherits from **AccessibilityExtensionAbility**. 15 16```ts 17import { AccessibilityExtensionAbility } from '@kit.AccessibilityKit'; 18 19class EntryAbility extends AccessibilityExtensionAbility { 20 onConnect(): void { 21 let axContext = this.context; 22 } 23} 24``` 25 26### enableScreenCurtain<sup>12+</sup> 27 28enableScreenCurtain(isEnable: boolean): void; 29 30Enables or disables the screen curtain. 31 32**System capability**: SystemCapability.BarrierFree.Accessibility.Core 33 34**Parameters** 35 36| Name | Type | Mandatory | Description | 37| ----------- | ---------------------------------------- | ---- | -------------- | 38| isEnable | boolean | Yes | The value **true** indicates enabled; **false** indicates disabled.| 39 40**Error codes** 41 42For details about the error codes, see [Accessibility Error Codes](errorcode-accessibility.md). 43 44| ID | Error Message | 45| ------- | ---------------------------------------- | 46| 202 | Permission verification failed. A non-system application calls a system API. | 47| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 48| 9300003 | No accessibility permission to perform the operation. | 49 50**Example** 51 52```ts 53import { AccessibilityElement } from '@kit.AccessibilityKit'; 54import { BusinessError } from '@kit.BasicServicesKit'; 55 56let rootElement: AccessibilityElement; 57 58axContext.getWindowRootElement().then((data: AccessibilityElement) => { 59 rootElement = data; 60 console.log(`Succeeded in get root element of the window, ${JSON.stringify(data)}`); 61 await rootElement.enableScreenCurtain(true); 62 console.log(`Succeeded in enableScreenCurtain}`); 63}).catch((err: BusinessError) => { 64 console.error(`failed to enableScreenCurtain, Code is ${err.code}, message is ${err.message}`); 65}); 66``` 67 68### findElement('elementId')<sup>12+</sup> 69 70findElement(type: 'elementId', condition: number): Promise\<AccessibilityElement>; 71 72Queries the node elements in the current active window based on the **elementId**. This API uses a promise to return the result. 73 74**System capability**: SystemCapability.BarrierFree.Accessibility.Core 75 76**Parameters** 77 78| Name | Type | Mandatory | Description | 79| --------- | --------------------------------- | ---- | ---------------------------------------- | 80| type | string | Yes | Type of element finding. The value is fixed at **'elementId'**.| 81| condition | number | Yes | **elementId** of the node element. | 82 83**Return value** 84 85| Type | Description | 86| ----------------------------------- | -------------------------------- | 87| Promise<[AccessibilityElement](js-apis-inner-application-accessibilityExtensionContext.md#accessibilityelement9)> | Promise used to return the result.| 88 89**Error codes** 90 91For details about the error codes, see [Accessibility Error Codes](errorcode-accessibility.md). 92 93| ID | Error Message | 94| ------- | ----------------------------- | 95| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 96 97**Example** 98 99```ts 100import { BusinessError } from '@kit.BasicServicesKit'; 101 102// elementId is 10. 103let condition = 10; 104 105// rootElement is an instance of AccessibilityElement. 106rootElement.findElement('elementId', condition).then((data: AccessibilityElement) => { 107 console.log(`Succeeded in find element, ${JSON.stringify(data)}`); 108}).catch((err: BusinessError) => { 109 console.error(`failed to find element, Code is ${err.code}, message is ${err.message}`); 110}); 111``` 112 113### findElement('textType')<sup>12+</sup> 114 115findElement(type: 'textType', condition: string): Promise\<Array\<AccessibilityElement>>; 116 117Queries all node elements based on the **accessibilityTextHint** text type configured for a node. This API uses a promise to return the result. 118 119**System capability**: SystemCapability.BarrierFree.Accessibility.Core 120 121**Parameters** 122 123| Name | Type | Mandatory | Description | 124| --------- | ------ | ---- | ----------------------------- | 125| type | string | Yes | Type of element finding. The value is fixed at **'textType'**.| 126| condition | string | Yes | Search criteria. | 127 128**Return value** 129 130| Type | Description | 131| ---------------------------------------- | ----------------------------- | 132| Promise<Array<[AccessibilityElement](js-apis-inner-application-accessibilityExtensionContext.md#accessibilityelement9)>> | Promise used to return the result.| 133 134**Error codes** 135 136For details about the error codes, see [Accessibility Error Codes](errorcode-accessibility.md). 137 138| ID | Error Message | 139| ------- | ----------------------------- | 140| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 141 142**Example** 143 144```ts 145import { BusinessError } from '@kit.BasicServicesKit'; 146 147// The content of condition must be the same as the type value in the accessibilityTextHint attribute of the target component. 148let condition = 'location'; 149 150// rootElement is an instance of AccessibilityElement. 151rootElement.findElement('textType', condition).then((data: AccessibilityElement[]) => { 152 console.log(`Succeeded in find element, ${JSON.stringify(data)}`); 153}).catch((err: BusinessError) => { 154 console.error(`failed to find element, Code is ${err.code}, message is ${err.message}`); 155}); 156``` 157 158### getCursorPosition<sup>12+</sup> 159 160getCursorPosition(): Promise\<number>; 161 162Obtains the cursor position in the **Text** component. This API uses a promise to return the result. 163 164**System capability**: SystemCapability.BarrierFree.Accessibility.Core 165 166**Return value** 167 168| Type | Description | 169| ------------------- | ---------------- | 170| Promise<number> | Promise used to return the result.| 171 172**Example** 173 174```ts 175import { BusinessError } from '@kit.BasicServicesKit'; 176 177// rootElement is an instance of AccessibilityElement. 178rootElement.getCursorPosition().then((data: number) => { 179 console.info(`Succeeded in getCursorPosition, ${data}`); 180}).catch((err: BusinessError) => { 181 console.error(`failed to getCursorPosition, Code is ${err.code}, message is ${err.message}`); 182}); 183``` 184 185### getCursorPosition<sup>12+</sup> 186 187getCursorPosition(callback: AsyncCallback\<number>): void; 188 189Obtains the cursor position in the **Text** component. This API uses an asynchronous callback to return the result. 190 191**System capability**: SystemCapability.BarrierFree.Accessibility.Core 192 193**Parameters** 194 195| Name | Type | Mandatory | Description | 196| ----------- | ---------------------------------------- | ---- | -------------- | 197| callback | AsyncCallback<number> | Yes | Callback function used to return the result.| 198 199**Example** 200 201```ts 202import { BusinessError } from '@kit.BasicServicesKit'; 203 204// rootElement is an instance of AccessibilityElement. 205rootElement.getCursorPosition((err: BusinessError, data: number) => { 206 if (err && err.code) { 207 console.error(`failed to getCursorPosition, Code is ${err.code}, message is ${err.message}`); 208 return; 209 } 210 console.info(`Succeeded in getCursorPosition, ${data}`); 211}); 212``` 213 214### startAbility<sup>12+</sup> 215 216startAbility(want: Want): void; 217 218Starts the foreground page. 219 220**System capability**: SystemCapability.BarrierFree.Accessibility.Core 221 222**Parameters** 223 224| Name| Type| Mandatory| Description| 225| -------- | -------- | -------- | -------- | 226| want | [Want](../../reference/apis-ability-kit/js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.| 227 228**Error codes** 229 230For details about the error codes, see [Accessibility Error Codes](errorcode-accessibility.md). 231 232| ID | Error Message | 233| ------- | ---------------------------------------- | 234| 201 | Permission denied. Interface caller does not have permission. | 235| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 236 237**Example** 238 239```ts 240import { BusinessError } from '@kit.BasicServicesKit'; 241 242let want: Want = { 243 bundleName: 'com.huawei.hmos.photos' 244 abilityName: 'com.huawei.hmos.photos.MainAbility' 245} 246 247axContext.startAbility(want).then(() => { 248 console.info(`startAbility Succeeded enable ability`); 249}).catch((err: BusinessError) => { 250 console.error(`startAbility failed to enable ability, Code is ${err.code}, message is ${err.message}`); 251}); 252``` 253 254### getElements<sup>18+</sup> 255 256getElements(windowId: number, elementId?: number): Promise<Array<AccessibilityElement>>; 257 258Obtains node elements in batches. 259 260**System capability**: SystemCapability.BarrierFree.Accessibility.Core 261 262**Parameters** 263 264| Name| Type| Mandatory| Description| 265| -------- | -------- | -------- | -------- | 266| windowId | number | Yes| Window ID to be obtained.| 267| elementId | number | No| Element ID to be obtained. If this parameter is passed in, the list of all child nodes under the current node is obtained. Otherwise, all nodes in the window are obtained.| 268 269**Return value** 270| Type | Description | 271| ----------------------------------- | ---------------------- | 272| Promise<Array<AccessibilityElement>> | Promise used to return the result.| 273 274**Error codes** 275 276For details about the error codes, see [Accessibility Error Codes](errorcode-accessibility.md). 277 278| ID | Error Message | 279| ------- | ---------------------------------------- | 280| 201 | Permission verification failed. The application does not have the permission required to call the API. | 281| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 282| 9300003 | No accessibility permission to perform the operation. | 283 284**Example** 285 286```ts 287import { AccessibilityElement } from '@kit.AccessibilityKit'; 288import { BusinessError } from '@kit.BasicServicesKit'; 289 290let windowId: number = 10; 291let elementId: number = 10; 292 293axContext.getElements(windowId, elementId).then((data:AccessibilityElement[]) => { 294 console.log(`Succeeded in find element, ${JSON.stringify(data)}`); 295}).catch((err: BusinessError) => { 296 console.error(`failed to find element, Code is ${err.code}, message is ${err.message}`); 297}); 298``` 299 300### getDefaultFocusedElementIds<sup>18+</sup> 301 302getDefaultFocusedElementIds(windowId: number): Promise<Array<number>>; 303 304Obtains the custom default focuses of an application. 305 306**System capability**: SystemCapability.BarrierFree.Accessibility.Core 307 308**Parameters** 309 310| Name| Type| Mandatory| Description| 311| -------- | -------- | -------- | -------- | 312| windowId | number | Yes| Window ID to be obtained.| 313 314**Return value** 315| Type | Description | 316| ----------------------------------- | ---------------------- | 317| Promise<Array<number>> | Promise used to return the result.| 318 319**Error codes** 320 321For details about the error codes, see [Accessibility Error Codes](errorcode-accessibility.md). 322 323| ID | Error Message | 324| ------- | ---------------------------------------- | 325| 202 | Permission verification failed. A non-system application calls a system API. | 326| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 327| 9300003 | No accessibility permission to perform the operation. | 328 329**Example** 330 331```ts 332import { AccessibilityElement } from '@kit.AccessibilityKit'; 333import { BusinessError } from '@kit.BasicServicesKit'; 334 335let windowId: number = 10; 336 337axContext.getDefaultFocusedElementIds(windowId).then((data: number[]) => { 338 console.log(`Succeeded in get default focus, ${JSON.stringify(data)}`); 339}).catch((err: BusinessError) => { 340 console.error(`failed to get default focus, Code is ${err.code}, message is ${err.message}`); 341}); 342``` 343