• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.form.FormExtensionAbility (FormExtensionAbility)
2
3The **FormExtensionAbility** module provides lifecycle callbacks invoked when a widget is created, destroyed, or updated.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> The APIs of this module can be used only in the stage model.
9
10## Modules to Import
11
12```ts
13import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
14```
15
16## Attributes
17
18**System capability**: SystemCapability.Ability.Form
19
20| Name   | Type                                                        | Readable| Writable| Description                                                        |
21| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
22| context | [FormExtensionContext](js-apis-inner-application-formExtensionContext.md) | Yes  | No  | Context of the FormExtensionAbility. This context is inherited from [ExtensionContext](js-apis-inner-application-extensionContext.md).|
23
24## onAddForm
25
26onAddForm(want: Want): formBindingData.FormBindingData
27
28Called to notify the widget provider that a **Form** instance (widget) is being created.
29
30**System capability**: SystemCapability.Ability.Form
31
32**Parameters**
33
34| Name| Type                                  | Mandatory| Description                                                        |
35| ------ | -------------------------------------- | ---- | ------------------------------------------------------------ |
36| want   | [Want](js-apis-app-ability-want.md) | Yes  | Want information related to the FormExtensionAbility, including the widget ID, name, and style. The information must be managed as persistent data to facilitate subsequent widget update and deletion.|
37
38**Return value**
39
40| Type                                                        | Description                                                       |
41| ------------------------------------------------------------ | ----------------------------------------------------------- |
42| [formBindingData.FormBindingData](js-apis-app-form-formBindingData.md#formbindingdata) | A **formBindingData.FormBindingData** object containing the data to be displayed on the widget.|
43
44**Example**
45
46```ts
47import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
48import formBindingData from '@ohos.app.form.formBindingData';
49import Want from '@ohos.app.ability.Want';
50
51export default class MyFormExtensionAbility extends FormExtensionAbility {
52  onAddForm(want: Want) {
53    console.log(`FormExtensionAbility onAddForm, want: ${want.abilityName}`);
54    let dataObj1 = new Map<string, string>();
55    dataObj1.set('temperature', '11c');
56    dataObj1.set('time', '11:00');
57
58    let obj1: formBindingData.FormBindingData = formBindingData.createFormBindingData(dataObj1);
59    return obj1;
60  }
61}
62```
63
64## onCastToNormalForm
65
66onCastToNormalForm(formId: string): void
67
68Called to notify the widget provider that a temporary widget is being converted to a normal one.
69
70**System capability**: SystemCapability.Ability.Form
71
72**Parameters**
73
74| Name| Type  | Mandatory| Description                    |
75| ------ | ------ | ---- | ------------------------ |
76| formId | string | Yes  | ID of the widget that requests to be converted to a normal one.|
77
78**Example**
79
80```ts
81import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
82
83export default class MyFormExtensionAbility extends FormExtensionAbility {
84  onCastToNormalForm(formId: string) {
85    console.log(`FormExtensionAbility onCastToNormalForm, formId: ${formId}`);
86  }
87};
88```
89
90## onUpdateForm
91
92onUpdateForm(formId: string): void
93
94Called to notify the widget provider that a widget is being updated. After obtaining the latest data, your application should call [updateForm](js-apis-app-form-formProvider.md#updateform) of **formProvider** to update the widget data.
95
96**System capability**: SystemCapability.Ability.Form
97
98**Parameters**
99
100| Name| Type  | Mandatory| Description              |
101| ------ | ------ | ---- | ------------------ |
102| formId | string | Yes  | ID of the widget that requests to be updated.|
103
104**Example**
105
106```ts
107import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
108import formBindingData from '@ohos.app.form.formBindingData';
109import formProvider from '@ohos.app.form.formProvider';
110import Base from '@ohos.base';
111
112export default class MyFormExtensionAbility extends FormExtensionAbility {
113  onUpdateForm(formId: string) {
114    console.log(`FormExtensionAbility onUpdateForm, formId: ${formId}`);
115    let param: Record<string, string> = {
116      'temperature': '22c',
117      'time': '22:00'
118    }
119    let obj2: formBindingData.FormBindingData = formBindingData.createFormBindingData(param);
120    formProvider.updateForm(formId, obj2).then(() => {
121      console.log(`FormExtensionAbility context updateForm`);
122    }).catch((error: Base.BusinessError) => {
123      console.error(`FormExtensionAbility context updateForm failed, data: ${error}`);
124    });
125  }
126};
127```
128
129## onChangeFormVisibility
130
131onChangeFormVisibility(newStatus: { [key: string]: number }): void
132
133Called to notify the widget provider that the widget visibility status is being changed.
134
135**System capability**: SystemCapability.Ability.Form
136
137**Parameters**
138
139| Name   | Type                     | Mandatory| Description                        |
140| --------- | ------------------------- | ---- | ---------------------------- |
141| newStatus | { [key: string]: number } | Yes  | ID and visibility status of the widget to be changed.|
142
143**Example**
144
145```ts
146import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
147import formBindingData from '@ohos.app.form.formBindingData';
148import formProvider from '@ohos.app.form.formProvider';
149import Base from '@ohos.base';
150
151// There is no Object.keys in ArkTS, and the for..in... syntax cannot be used.
152// If an error is reported, extract this API to a .ts file and expose it. Then import the API to the required .ets file.
153function getObjKeys(obj: Object): string[] {
154  let keys = Object.keys(obj);
155  return keys;
156}
157
158export default class MyFormExtensionAbility extends FormExtensionAbility {
159  onChangeFormVisibility(newStatus: Record<string, number>) {
160    console.log(`FormExtensionAbility onChangeFormVisibility, newStatus: ${newStatus}`);
161    let param: Record<string, string> = {
162      'temperature': '22c',
163      'time': '22:00'
164    }
165    let obj2: formBindingData.FormBindingData = formBindingData.createFormBindingData(param);
166
167    let keys: string[] = getObjKeys(newStatus);
168
169    for (let i: number = 0; i < keys.length; i++) {
170      console.log(`FormExtensionAbility onChangeFormVisibility, key: ${keys[i]}, value= ${newStatus[keys[i]]}`);
171      formProvider.updateForm(keys[i], obj2).then(() => {
172        console.log(`FormExtensionAbility context updateForm`);
173      }).catch((error: Base.BusinessError) => {
174        console.error(`Operation updateForm failed. Cause: ${JSON.stringify(error)}`);
175      });
176    }
177  }
178};
179```
180
181## onFormEvent
182
183onFormEvent(formId: string, message: string): void
184
185Called to instruct the widget provider to process the widget event.
186
187**System capability**: SystemCapability.Ability.Form
188
189**Parameters**
190
191| Name | Type  | Mandatory| Description                  |
192| ------- | ------ | ---- | ---------------------- |
193| formId  | string | Yes  | ID of the widget that requests the event.|
194| message | string | Yes  | Event message.            |
195
196**Example**
197
198```ts
199import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
200
201export default class MyFormExtensionAbility extends FormExtensionAbility {
202  onFormEvent(formId: string, message: string) {
203    console.log(`FormExtensionAbility onFormEvent, formId: ${formId}, message: ${message}`);
204  }
205};
206```
207
208## onRemoveForm
209
210onRemoveForm(formId: string): void
211
212Called to notify the widget provider that a **Form** instance (widget) is being destroyed.
213
214**System capability**: SystemCapability.Ability.Form
215
216**Parameters**
217
218| Name| Type  | Mandatory| Description              |
219| ------ | ------ | ---- | ------------------ |
220| formId | string | Yes  | ID of the widget to be destroyed.|
221
222**Example**
223
224```ts
225import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
226
227export default class MyFormExtensionAbility extends FormExtensionAbility {
228  onRemoveForm(formId: string) {
229    console.log(`FormExtensionAbility onRemoveForm, formId: ${formId}`);
230  }
231};
232```
233
234## onConfigurationUpdate
235
236onConfigurationUpdate(newConfig: Configuration): void;
237
238Called when the configuration of the environment where the FormExtensionAbility is running is updated.
239
240**System capability**: SystemCapability.Ability.Form
241
242**Parameters**
243
244| Name| Type| Mandatory| Description|
245| -------- | -------- | -------- | -------- |
246| newConfig | [Configuration](js-apis-application-configuration.md) | Yes| New configuration.|
247
248**Example**
249
250```ts
251import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
252import { Configuration } from '@ohos.app.ability.Configuration';
253
254export default class MyFormExtensionAbility extends FormExtensionAbility {
255  onConfigurationUpdate(config: Configuration) {
256    console.log(`onConfigurationUpdate, config: ${JSON.stringify(config)}`);
257  }
258};
259```
260
261## onAcquireFormState
262
263onAcquireFormState?(want: Want): formInfo.FormState;
264
265Called to notify the widget provider that the widget host is requesting the widget state. By default, the initial state is returned.
266
267**System capability**: SystemCapability.Ability.Form
268
269**Parameters**
270
271| Name| Type| Mandatory| Description|
272| -------- | -------- | -------- | -------- |
273| want | [Want](js-apis-app-ability-want.md) | Yes| Description of the widget state, including the bundle name, ability name, module name, widget name, and widget dimension.|
274
275**Example**
276
277```ts
278import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
279import formInfo from '@ohos.app.form.formInfo';
280import Want from '@ohos.app.ability.Want';
281
282export default class MyFormExtensionAbility extends FormExtensionAbility {
283  onAcquireFormState(want: Want) {
284    console.log(`FormExtensionAbility onAcquireFormState, want: ${want}`);
285    return formInfo.FormState.UNKNOWN;
286  }
287};
288```
289
290## onShareForm
291
292onShareForm?(formId: string): { [key: string]: Object }
293
294Called to notify the widget provider that the widget host is sharing the widget data.
295
296**System API**: This is a system API.
297
298**System capability**: SystemCapability.Ability.Form
299
300**Parameters**
301
302| Name| Type| Mandatory| Description|
303| -------- | -------- | -------- | -------- |
304| formId | string | Yes  | Widget ID.|
305
306**Return value**
307
308| Type                                                        | Description                                                       |
309| ------------------------------------------------------------ | ----------------------------------------------------------- |
310| {[key: string]: any} | Data to be shared by the widget, in the form of key-value pairs.|
311
312**Example**
313
314```ts
315import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
316
317export default class MyFormExtensionAbility extends FormExtensionAbility {
318  onShareForm(formId: string) {
319    console.log(`FormExtensionAbility onShareForm, formId: ${formId}`);
320    let wantParams: Record<string, Object> = {
321      'temperature': '20',
322      'time': '2022-8-8 09:59',
323    };
324    return wantParams;
325  }
326};
327```
328
329## onAcquireFormData<sup>10+</sup>
330
331onAcquireFormData?(formId: string): { [key: string]: Object }
332
333Called to notify the widget provider that the widget host is requesting the custom data.
334
335**System API**: This is a system API.
336
337**System capability**: SystemCapability.Ability.Form
338
339**Parameters**
340
341| Name| Type| Mandatory| Description|
342| -------- | -------- | -------- | -------- |
343| formId | string | Yes  | Widget ID.|
344
345**Return value**
346
347| Type                                                        | Description                                                       |
348| ------------------------------------------------------------ | ----------------------------------------------------------- |
349| {[key: string]: any} | Custom data of the widget, in the form of key-value pairs.|
350
351**Example**
352
353```ts
354import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
355
356export default class MyFormExtensionAbility extends FormExtensionAbility {
357  onAcquireFormData(formId: string) {
358    console.log(`FormExtensionAbility onAcquireFormData, formId: ${formId}`);
359    let wantParams: Record<string, Object> = {
360      'temperature': '20',
361      'time': '2022-8-8 09:59',
362    };
363    return wantParams;
364  }
365};
366```
367