1# @ohos.arkui.uiExtension (uiExtension) 2 3The **uiExtension** module provides APIs for the EmbeddedUIExtensionAbility (or UIExtensionAbility) to obtain the host application window information or the information about the corresponding **EmbeddedComponent**<!--Del--> (or **UIExtensionComponent**)<!--DelEnd--> component. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 12. Updates will be marked with a superscript to indicate their earliest API version. 8> 9 10## Modules to Import 11 12``` 13import { uiExtension } from '@kit.ArkUI' 14``` 15 16## WindowProxy 17 18### getWindowAvoidArea 19 20getWindowAvoidArea(type: window.AvoidAreaType): window.AvoidArea 21 22Obtains the area where this window cannot be displayed, for example, the system bar area, notch, gesture area, and soft keyboard area. 23 24**Atomic service API**: This API can be used in atomic services since API version 12. 25 26**System capability**: SystemCapability.ArkUI.ArkUI.Full 27 28| Name| Type| Mandatory| Description| 29| -------- | -------- | -------- | -------- | 30| type |[window.AvoidAreaType](./js-apis-window.md#avoidareatype7) | Yes| Type of the area.| 31 32**Return value** 33 34| Type| Description| 35| -------- | -------- | 36|[window.AvoidArea](./js-apis-window.md#avoidarea7) | Area where the window cannot be displayed.| 37 38**Error codes** 39 40For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 41 42| ID| Error Message| 43| ------- | -------- | 44| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 45 46**Example** 47 48```ts 49// ExtensionProvider.ts 50import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 51import { window } from '@kit.ArkUI'; 52 53export default class EntryAbility extends EmbeddedUIExtensionAbility { 54 onSessionCreate(want: Want, session: UIExtensionContentSession) { 55 const extensionWindow = session.getUIExtensionWindowProxy(); 56 // Obtain the information about the area where the window cannot be displayed. 57 let avoidArea: window.AvoidArea | undefined = extensionWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); 58 console.info(`avoidArea: ${JSON.stringify(avoidArea)}`); 59 } 60} 61``` 62 63### on('avoidAreaChange') 64 65on(type: 'avoidAreaChange', callback: Callback<AvoidAreaInfo>): void 66 67Subscribes to the event indicating changes to the area where the window cannot be displayed. 68 69**Atomic service API**: This API can be used in atomic services since API version 12. 70 71**System capability**: SystemCapability.ArkUI.ArkUI.Full 72 73| Name| Type| Mandatory| Description| 74| ------ | ---- | ---- | ---- | 75| type | string | Yes| Event type. The value is fixed at **'avoidAreaChange'**, indicating the event of changes to the area where the window cannot be displayed.| 76| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[AvoidAreaInfo](#avoidareainfo)> | Yes| Callback used to return the area information.| 77 78**Error codes** 79 80For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 81 82| ID| Error Message| 83| ------- | -------- | 84| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 85 86**Example** 87 88```ts 89// ExtensionProvider.ts 90import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 91import { uiExtension } from '@kit.ArkUI'; 92 93export default class EntryAbility extends EmbeddedUIExtensionAbility { 94 onSessionCreate(want: Want, session: UIExtensionContentSession) { 95 const extensionWindow = session.getUIExtensionWindowProxy(); 96 // Subscribe to the event indicating changes to the area where the window cannot be displayed. 97 extensionWindow.on('avoidAreaChange', (info: uiExtension.AvoidAreaInfo) => { 98 console.info(`The avoid area of the host window is: ${JSON.stringify(info.area)}.`); 99 }); 100 } 101} 102``` 103 104### off('avoidAreaChange') 105 106off(type: 'avoidAreaChange', callback?: Callback<AvoidAreaInfo>): void 107 108Unsubscribes from the event indicating changes to the area where the window cannot be displayed. 109 110**Atomic service API**: This API can be used in atomic services since API version 12. 111 112**System capability**: SystemCapability.ArkUI.ArkUI.Full 113 114| Name | Type| Mandatory| Description| 115| -------- | ---- | ---- | --- | 116| type | string | Yes| Event type. The value is fixed at **'avoidAreaChange'**, indicating the event of changes to the area where the window cannot be displayed.| 117| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[AvoidAreaInfo](#avoidareainfo)> | No| Callback used for unsubscription. If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled.| 118 119**Error codes** 120 121For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 122 123| ID| Error Message | 124| -------- | ------------------------------------------------------------ | 125| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 126 127**Example** 128 129```ts 130// ExtensionProvider.ts 131import { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit'; 132 133export default class EntryAbility extends EmbeddedUIExtensionAbility { 134 onSessionDestroy(session: UIExtensionContentSession) { 135 const extensionWindow = session.getUIExtensionWindowProxy(); 136 // Cancel all subscriptions to the event indicating changes to the area where the window cannot be displayed. 137 extensionWindow.off('avoidAreaChange'); 138 } 139} 140``` 141 142### on('windowSizeChange') 143 144on(type: 'windowSizeChange', callback: Callback<window.Size>): void 145 146Subscribes to the window size change event of the host application. 147 148**Atomic service API**: This API can be used in atomic services since API version 12. 149 150**System capability**: SystemCapability.ArkUI.ArkUI.Full 151 152| Name | Type | Mandatory| Description | 153| -------- | --------------------- | ---- | ---------------------- | 154| type | string | Yes | Event type. The value is fixed at **'windowSizeChange'**, indicating the window size change event.| 155| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[window.Size](js-apis-window.md#size7)> | Yes | Callback used to return the window size.| 156 157**Error codes** 158 159For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 160 161| ID| Error Message| 162| ------- | -------- | 163| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 164 165**Example** 166 167```ts 168// ExtensionProvider.ts 169import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 170import { window } from '@kit.ArkUI'; 171 172export default class EntryAbility extends EmbeddedUIExtensionAbility { 173 onSessionCreate(want: Want, session: UIExtensionContentSession) { 174 const extensionWindow = session.getUIExtensionWindowProxy(); 175 // Subscribe to the window size change event of the host application. 176 extensionWindow.on('windowSizeChange', (size: window.Size) => { 177 console.info(`The avoid area of the host window is: ${JSON.stringify(size)}.`); 178 }); 179 } 180} 181``` 182 183### off('windowSizeChange') 184 185off(type: 'windowSizeChange', callback?: Callback<window.Size>): void 186 187Unsubscribes from the window size change event of the host application. 188 189**Atomic service API**: This API can be used in atomic services since API version 12. 190 191**System capability**: SystemCapability.ArkUI.ArkUI.Full 192 193| Name | Type | Mandatory| Description | 194| -------- | --------------------- | ---- | ---------------------- | 195| type | string | Yes | Event type. The value is fixed at **'windowSizeChange'**, indicating the window size change event.| 196| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[window.Size](js-apis-window.md#size7)> | No | Callback used for unsubscription. If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled.| 197 198**Error codes** 199 200For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 201 202| ID| Error Message| 203| ------- | -------- | 204| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 205 206**Example** 207 208```ts 209// ExtensionProvider.ts 210import { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit'; 211 212export default class EntryAbility extends EmbeddedUIExtensionAbility { 213 onSessionDestroy(session: UIExtensionContentSession) { 214 const extensionWindow = session.getUIExtensionWindowProxy(); 215 // Unsubscribe from the window size change event of the host application. 216 extensionWindow.off('windowSizeChange'); 217 } 218} 219``` 220 221### on('rectChange')<sup>14+</sup> 222 223on(type: 'rectChange', reasons: number, callback: Callback<RectChangeOptions>): void 224 225Subscribes to changes in the position and size of the component (**EmbeddedComponent** or **UIExtensionComponent**). This API can be used only on 2-in-1 devices. 226 227**System capability**: SystemCapability.ArkUI.ArkUI.Full 228 229**Atomic service API**: This API can be used in atomic services since API version 14. 230 231**Parameters** 232 233| Name | Type | Mandatory| Description | 234| -------- | ------------------------------ | ---- | -------------------------------------------------------- | 235| type | string | Yes | Event type. The value is fixed at **'rectChange'**, indicating the rectangle change event for the component (**EmbeddedComponent** or **UIExtensionComponent**).| 236| reasons | number | Yes | Reason for the position and size change of the component (**EmbeddedComponent** or **UIExtensionComponent**). 237| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[RectChangeOptions](#rectchangeoptions14)> | Yes| Callback used to return the current rectangle change values and the reason for the change of the component (**EmbeddedComponent** or **UIExtensionComponent**).| 238 239**Error codes** 240 241For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 242 243| ID| Error Message| 244| ------- | -------------------------------------------- | 245| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 246| 801 | Capability not supported. Failed to call the API due to limited device capabilities. | 247 248**Example:** 249 250```ts 251// ExtensionProvider.ts 252import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 253import { uiExtension } from '@kit.ArkUI'; 254 255export default class EntryAbility extends EmbeddedUIExtensionAbility { 256 onSessionCreate(want: Want, session: UIExtensionContentSession) { 257 const extensionWindow = session.getUIExtensionWindowProxy(); 258 // Subscribe to changes in the position and size of the component (EmbeddedComponent or UIExtensionComponent). 259 extensionWindow.on('rectChange', uiExtension.RectChangeReason.HOST_WINDOW_RECT_CHANGE, (data: uiExtension.RectChangeOptions) => { 260 console.info('Succeeded window rect changes. Data: ' + JSON.stringify(data)); 261 }); 262 } 263} 264``` 265 266### off('rectChange')<sup>14+</sup> 267 268off(type: 'rectChange', callback?: Callback<RectChangeOptions>): void 269 270Unsubscribes from changes in the position and size of the component (**EmbeddedComponent** or **UIExtensionComponent**). This API can be used only on 2-in-1 devices. 271 272**System capability**: SystemCapability.ArkUI.ArkUI.Full 273 274**Atomic service API**: This API can be used in atomic services since API version 14. 275 276**Parameters** 277 278| Name | Type | Mandatory| Description | 279| -------- | ------------------------------ | ---- | ------------------------------------------------------------ | 280| type | string | Yes | Event type. The value is fixed at **'rectChange'**, indicating the rectangle change event for the component (**EmbeddedComponent** or **UIExtensionComponent**).| 281| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[RectChangeOptions](#rectchangeoptions14)> | No | Callback used to return the current rectangle change values and the reason for the change of the component (**EmbeddedComponent** or **UIExtensionComponent**). If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled.| 282 283**Error codes** 284 285For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 286 287| ID| Error Message| 288| ------- | -------------------------------------------- | 289| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 290| 801 | Capability not supported. Failed to call the API due to limited device capabilities. | 291 292**Example:** 293 294```ts 295// ExtensionProvider.ts 296import { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit'; 297 298export default class EntryAbility extends EmbeddedUIExtensionAbility { 299 onSessionDestroy(session: UIExtensionContentSession) { 300 const extensionWindow = session.getUIExtensionWindowProxy(); 301 // Unsubscribe from changes in the position and size of the component (EmbeddedComponent or UIExtensionComponent). 302 extensionWindow.off('rectChange'); 303 } 304} 305``` 306 307### properties<sup>14+</sup> 308 309properties: WindowProxyProperties 310 311Provides information about the host application window and the component (**EmbeddedComponent** or **UIExtensionComponent**). 312 313**System capability**: SystemCapability.ArkUI.ArkUI.Full 314 315**Atomic service API**: This API can be used in atomic services since API version 14. 316 317| Name | Type | Description | 318| ---------- | ------------------------------------ | -------------------------------- | 319| properties | [WindowProxyProperties](#windowproxyproperties14) | Information about the host application window and the component (**EmbeddedComponent** or **UIExtensionComponent**).| 320 321**Example** 322 323```ts 324// ExtensionProvider.ts 325import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 326 327export default class EntryAbility extends EmbeddedUIExtensionAbility { 328 onSessionCreate(want: Want, session: UIExtensionContentSession) { 329 const extensionWindow = session.getUIExtensionWindowProxy(); 330 // Obtain the position and size information of the component (EmbeddedComponent or UIExtensionComponent). 331 const rect = extensionWindow.properties.uiExtensionHostWindowProxyRect; 332 console.log(`Rect Info: ${JSON.stringify(rect)}`); 333 } 334} 335``` 336 337### createSubWindowWithOptions 338 339createSubWindowWithOptions(name: string, subWindowOptions: window.SubWindowOptions): Promise<window.Window> 340 341Creates a subwindow for this window proxy. This API uses a promise to return the result. 342 343**System capability**: SystemCapability.ArkUI.ArkUI.Full 344 345**Atomic service API**: This API can be used in atomic services since API version 12. 346 347**Model restriction**: This API can be used only in the stage model. 348 349**Parameters** 350 351| Name| Type | Mandatory| Description | 352| ------ | ------ | ---- | -------------- | 353| name | string | Yes | Name of the subwindow.| 354| subWindowOptions | [window.SubWindowOptions](js-apis-window.md#subwindowoptions11) | Yes | Parameters used for creating the subwindow. | 355 356**Return value** 357 358| Type | Description | 359| -------------------------------- | ------------------------------------------------ | 360| Promise<[window.Window](js-apis-window.md#window)> | Promise used to return the subwindow created.| 361 362**Error codes** 363 364For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). and [Window Error Codes](errorcode-window.md). 365 366| ID| Error Message| 367| ------- | ------------------------------ | 368| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 369| 801 | Capability not supported. Failed to call the API due to limited device capabilities. | 370| 1300002 | This window state is abnormal. | 371| 1300005 | This window proxy is abnormal. | 372 373**Example:** 374 375```ts 376// ExtensionProvider.ts 377import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 378import { window } from '@kit.ArkUI'; 379import { BusinessError } from '@kit.BasicServicesKit'; 380 381export default class EntryAbility extends EmbeddedUIExtensionAbility { 382 onSessionCreate(want: Want, session: UIExtensionContentSession) { 383 const extensionWindow = session.getUIExtensionWindowProxy(); 384 const subWindowOpts: window.SubWindowOptions = { 385 title: 'This is a subwindow', 386 decorEnabled: true 387 }; 388 // Create a subwindow. 389 extensionWindow.createSubWindowWithOptions('subWindowForHost', subWindowOpts) 390 .then((subWindow: window.Window) => { 391 subWindow.setUIContent('pages/Index', (err, data) =>{ 392 if (err && err.code != 0) { 393 return; 394 } 395 subWindow?.resize(300, 300, (err, data)=>{ 396 if (err && err.code != 0) { 397 return; 398 } 399 subWindow?.moveWindowTo(100, 100, (err, data)=>{ 400 if (err && err.code != 0) { 401 return; 402 } 403 subWindow?.showWindow((err, data) => { 404 if (err && err.code == 0) { 405 console.info(`The subwindow has been shown!`); 406 } else { 407 console.error(`Failed to show the subwindow!`); 408 } 409 }); 410 }); 411 }); 412 }); 413 }).catch((error: BusinessError) => { 414 console.error(`Create subwindow failed: ${JSON.stringify(error)}`); 415 }) 416 } 417} 418``` 419 420### occupyEvents<sup>16+</sup> 421 422occupyEvents(eventFlags: number): Promise<void> 423 424Sets the events that the component (**EmbeddedComponent** or **UIExtensionComponent**) will occupy, preventing the host from responding to these events within the component's area. 425 426**System capability**: SystemCapability.ArkUI.ArkUI.Full 427 428**Atomic service API**: This API can be used in atomic services since API version 16. 429 430**Parameters** 431 432| Name| Type | Mandatory| Description | 433| ------ | ------ | ---- | -------------- | 434| eventFlags | [EventFlag](js-apis-arkui-uiExtension.md#eventflag16) | Yes| Type of events to be occupied by the component.| 435 436**Return value** 437 438| Type | Description | 439| ------------------- | ------------------------ | 440| Promise<void> | Promise that returns no value.| 441 442**Error codes** 443 444For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). and [Window Error Codes](errorcode-window.md). 445 446| ID| Error Message| 447| -------- | ------------------------------ | 448| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 449| 1300002 | This window state is abnormal. | 450| 1300003 | This window manager service works abnormally. | 451 452**Example:** 453 454```ts 455// ExtensionProvider.ts 456import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 457import { window } from '@kit.ArkUI'; 458 459export default class EntryAbility extends EmbeddedUIExtensionAbility { 460 onSessionCreate(want: Want, session: UIExtensionContentSession) { 461 const extensionWindow = session.getUIExtensionWindowProxy(); 462 // Occupy events. 463 try { 464 let promise = extensionWindow.occupyEvents(uiExtension.EventFlag.EVENT_CLICK | uiExtension.EventFlag.EVENT_LONG_PRESS); 465 promise.then(() => { 466 console.info('Succeeded in occupy events'); 467 }).catch((err: BusinessError) => { 468 console.error(`Failed to occupy events. Cause code: ${err.code}, message: ${err.message}`); 469 }); 470 } catch (e) { 471 console.error(`Occupy events got exception`); 472 } 473 } 474} 475``` 476 477## EventFlag<sup>16+</sup> 478 479Enumerates event types. 480 481**System capability**: SystemCapability.ArkUI.ArkUI.Full 482 483| Name | Value | Description | 484|-----------------------------| --------------- |----------------| 485| EVENT_NONE | 0x00000000 | None. | 486| EVENT_PAN_GESTURE_LEFT | 0x00000001 | Swipe left event. | 487| EVENT_PAN_GESTURE_RIGHT | 0x00000002 | Swipe right event. | 488| EVENT_PAN_GESTURE_UP | 0x00000004 | Swipe up event. | 489| EVENT_PAN_GESTURE_DOWN | 0x00000008 | Swipe down event. | 490| EVENT_CLICK | 0x00000100 | Click event. | 491| EVENT_LONG_PRESS | 0x00000200 | Long press event. | 492 493## AvoidAreaInfo 494 495Describes the information about the area where the window cannot be displayed. 496 497**Atomic service API**: This API can be used in atomic services since API version 12. 498 499**System capability**: SystemCapability.ArkUI.ArkUI.Full 500 501| Name| Type | Mandatory| Description | 502| ------ | -------------------- | ------------------ | ------------------ | 503| type | [window.AvoidAreaType](js-apis-window.md#avoidareatype7) | Yes| Type of the area where the window cannot be displayed. | 504| area | [window.AvoidArea](js-apis-window.md#avoidarea7) | Yes| Area where the window cannot be displayed.| 505 506## WindowProxyProperties<sup>14+</sup> 507 508Provides information about the host application window and component (**EmbeddedComponent** or **UIExtensionComponent**). 509 510**System capability**: SystemCapability.ArkUI.ArkUI.Full 511 512**Atomic service API**: This API can be used in atomic services since API version 14. 513 514| Name | Type | Mandatory | Description | 515| ------------------------------ | ----------- | -------------------------------- | -------------------------------- | 516| uiExtensionHostWindowProxyRect | [window.Rect](js-apis-window.md#rect7) | Yes| Position and size of the component (**EmbeddedComponent** or **UIExtensionComponent**).| 517 518## RectChangeReason<sup>14+</sup> 519 520Enumerates the reasons for changes in the rectangle (position and size) of the component (**EmbeddedComponent** or **UIExtensionComponent**). 521 522**System capability**: SystemCapability.ArkUI.ArkUI.Full 523 524**Atomic service API**: This API can be used in atomic services since API version 14. 525 526| Name | Value | Description | 527| ----------------------- | ---- | ------------------------------------------------------------ | 528| HOST_WINDOW_RECT_CHANGE | 1 | The rectangle of the host window containing the component changes.| 529 530## RectChangeOptions<sup>14+</sup> 531 532Provides the values and reasons returned when the rectangle (position and size) of the component (**EmbeddedComponent** or **UIExtensionComponent**) changes. 533 534**System capability**: SystemCapability.ArkUI.ArkUI.Full 535 536**Atomic service API**: This API can be used in atomic services since API version 14. 537 538| Name | Type | Readable| Writable| Description | 539| ---------- | ------------- | ---- | ---- | ------------------ | 540| rect | [window.Rect](js-apis-window.md#rect7) | Yes | Yes | New values of the rectangle of the component after the change.| 541| reason | [RectChangeReason](#rectchangereason14) | Yes | Yes | Reason for the rectangle change.| 542 543## Example 544 545This example shows how to use all the available APIs in the EmbeddedUIExtensionAbility. The bundle name of the sample application is **com.example.embeddeddemo**, and the EmbeddedUIExtensionAbility to start is **ExampleEmbeddedAbility**. 546 547- The EntryAbility (UIAbility) of the sample application loads the **pages/Index.ets** file, whose content is as follows: 548 549 ```ts 550 // The UIAbility loads pages/Index.ets when started. 551 import { Want } from '@kit.AbilityKit'; 552 553 @Entry 554 @Component 555 struct Index { 556 @State message: string = 'Message: ' 557 private want: Want = { 558 bundleName: "com.example.embeddeddemo", 559 abilityName: "ExampleEmbeddedAbility", 560 } 561 562 build() { 563 Row() { 564 Column() { 565 Text(this.message).fontSize(30) 566 EmbeddedComponent(this.want, EmbeddedType.EMBEDDED_UI_EXTENSION) 567 .width('100%') 568 .height('90%') 569 .onTerminated((info)=>{ 570 this.message = 'Termination: code = ' + info.code + ', want = ' + JSON.stringify(info.want); 571 }) 572 .onError((error)=>{ 573 this.message = 'Error: code = ' + error.code; 574 }) 575 } 576 .width('100%') 577 } 578 .height('100%') 579 } 580 } 581 ``` 582 583- The EmbeddedUIExtensionAbility to start by the **EmbeddedComponent** is implemented in the **ets/extensionAbility/ExampleEmbeddedAbility** file. The file content is as follows: 584 585 ```ts 586 import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 587 588 const TAG: string = '[ExampleEmbeddedAbility]' 589 export default class ExampleEmbeddedAbility extends EmbeddedUIExtensionAbility { 590 591 onCreate() { 592 console.info(TAG, `onCreate`); 593 } 594 595 onForeground() { 596 console.info(TAG, `onForeground`); 597 } 598 599 onBackground() { 600 console.info(TAG, `onBackground`); 601 } 602 603 onDestroy() { 604 console.info(TAG, `onDestroy`); 605 } 606 607 onSessionCreate(want: Want, session: UIExtensionContentSession) { 608 console.info(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 609 let param: Record<string, UIExtensionContentSession> = { 610 'session': session 611 }; 612 let storage: LocalStorage = new LocalStorage(param); 613 session.loadContent('pages/extension', storage); 614 } 615 } 616 ``` 617 618- The entry page file of the EmbeddedUIExtensionAbility is **pages/extension.ets**, whose content is as follows: 619 620 ```ts 621 import { UIExtensionContentSession } from '@kit.AbilityKit'; 622 import { uiExtension, window } from '@kit.ArkUI'; 623 import { BusinessError } from '@kit.BasicServicesKit'; 624 let storage = LocalStorage.getShared() 625 626 @Entry(storage) 627 @Component 628 struct Extension { 629 @State message: string = 'EmbeddedUIExtensionAbility Index'; 630 private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 631 private extensionWindow: uiExtension.WindowProxy | undefined = this.session?.getUIExtensionWindowProxy(); 632 private subWindow: window.Window | undefined = undefined; 633 634 aboutToAppear(): void { 635 this.extensionWindow?.on('windowSizeChange', (size: window.Size) => { 636 console.info(`size = ${JSON.stringify(size)}`); 637 }); 638 this.extensionWindow?.on('rectChange', uiExtension.RectChangeReason.HOST_WINDOW_RECT_CHANGE, (data: uiExtension.RectChangeOptions) => { 639 console.info('Succeeded window rect changes. Data: ' + JSON.stringify(data)); 640 }); 641 this.extensionWindow?.on('avoidAreaChange', (info: uiExtension.AvoidAreaInfo) => { 642 console.info(`type = ${JSON.stringify(info.type)}, area = ${JSON.stringify(info.area)}`); 643 }); 644 } 645 646 aboutToDisappear(): void { 647 this.extensionWindow?.off('windowSizeChange'); 648 this.extensionWindow?.off('rectChange'); 649 this.extensionWindow?.off('avoidAreaChange'); 650 } 651 652 build() { 653 Column() { 654 Text(this.message) 655 .fontSize(20) 656 .fontWeight(FontWeight.Bold) 657 Button("Obtain Component Size").width('90%').margin({top: 5, bottom: 5}).fontSize(16).onClick(() => { 658 let rect = this.extensionWindow?.properties.uiExtensionHostWindowProxyRect; 659 console.info(`EmbeddedComponent position and size: ${JSON.stringify(rect)}`); 660 }) 661 Button("Obtain Avoid Area Info").width('90%').margin({top: 5, bottom: 5}).fontSize(16).onClick(() => { 662 let avoidArea: window.AvoidArea | undefined = this.extensionWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); 663 console.info(`System avoid area: ${JSON.stringify(avoidArea)}`); 664 }) 665 Button("Create Subwindow").width('90%').margin({top: 5, bottom: 5}).fontSize(16).onClick(() => { 666 let subWindowOpts: window.SubWindowOptions = { 667 'title': 'This is a subwindow', 668 decorEnabled: true 669 }; 670 this.extensionWindow?.createSubWindowWithOptions('subWindowForHost', subWindowOpts) 671 .then((subWindow: window.Window) => { 672 this.subWindow = subWindow; 673 this.subWindow.loadContent('pages/Index', storage, (err, data) =>{ 674 if (err && err.code != 0) { 675 return; 676 } 677 this.subWindow?.resize(300, 300, (err, data)=>{ 678 if (err && err.code != 0) { 679 return; 680 } 681 this.subWindow?.moveWindowTo(100, 100, (err, data)=>{ 682 if (err && err.code != 0) { 683 return; 684 } 685 this.subWindow?.showWindow((err, data) => { 686 if (err && err.code == 0) { 687 console.info(`The subwindow has been shown!`); 688 } else { 689 console.error(`Failed to show the subwindow!`); 690 } 691 }); 692 }); 693 }); 694 }); 695 }).catch((error: BusinessError) => { 696 console.error(`Create subwindow failed: ${JSON.stringify(error)}`); 697 }) 698 }) 699 }.width('100%').height('100%') 700 } 701 } 702 ``` 703 704- Add an item to **extensionAbilities** in the **module.json5** file of the sample application. The details are as follows: 705 ```json 706 { 707 "name": "ExampleEmbeddedAbility", 708 "srcEntry": "./ets/extensionAbility/ExampleEmbeddedAbility.ets", 709 "type": "embeddedUI" 710 } 711 ``` 712