• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# FormExtensionAbility (Widget)
2
3
4## Widget Overview
5
6FormExtensionAbility provides a service widget (also called widget), which is a set of UI components that display important information or operations specific to an application. It provides users with direct access to a desired application service, without the need to open the application first.
7
8A widget usually appears as a part of the UI of another application (which currently can only be a system application) and provides basic interactive features such as opening a UI page or sending a message.
9
10Before you get started, it would be helpful if you have a basic understanding of the following concepts:
11
12- Widget host: an application that displays the widget content and controls the widget location.
13
14- Widget Manager: a resident agent that provides widget management features such as periodic widget updates.
15
16- Widget provider: an atomic service that provides the widget content to display and controls how widget components are laid out and how they interact with users.
17
18
19## Working Principles
20
21Figure 1 shows the working principles of the widget framework.
22
23**Figure 1** Widget framework working principles in the stage model
24![form-extension](figures/form-extension.png)
25
26The widget host consists of the following modules:
27
28- Widget usage: provides operations such as creating, deleting, or updating a widget.
29
30- Communication adapter: provided by the OpenHarmony SDK for communication with the Widget Manager. It sends widget-related operations to the Widget Manager.
31
32The Widget Manager consists of the following modules:
33
34- Periodic updater: starts a scheduled task based on the update policy to periodically update a widget after it is added to the Widget Manager.
35
36- Cache manager: caches view information of a widget after it is added to the Widget Manager to directly return the cached data when the widget is obtained next time. This reduces the latency greatly.
37
38- Lifecycle manager: suspends update when a widget is switched to the background or is blocked, and updates and/or clears widget data during upgrade and deletion.
39
40- Object manager: manages RPC objects of the widget host. It is used to verify requests from the widget host and process callbacks after the widget update.
41
42- Communication adapter: communicates with the widget host and provider through RPCs.
43
44The widget provider consists of the following modules:
45
46- Widget service: implemented by the widget provider developer to process requests on widget creation, update, and deletion, and to provide corresponding widget services.
47
48- Instance manager: implemented by the widget provider developer for persistent management of widget instances allocated by the Widget Manager.
49
50- Communication adapter: provided by the OpenHarmony SDK for communication with the Widget Manager. It pushes update data to the Widget Manager.
51
52> **NOTE**
53>
54> You only need to develop the widget provider. The system automatically handles the work of the widget host and Widget Manager.
55
56
57## Available APIs
58
59The **FormExtensionAbility** class has the following APIs. For details, see [FormExtensionAbility](../reference/apis/js-apis-app-form-formExtensionAbility.md).
60
61| API| Description|
62| -------- | -------- |
63| onAddForm(want: Want): formBindingData.FormBindingData | Called to notify the widget provider that a widget has been created.|
64| onCastToNormalForm(formId: string): void | Called to notify the widget provider that a temporary widget has been converted to a normal one.|
65| onUpdateForm(formId: string): void | Called to notify the widget provider that a widget has been updated.|
66| onChangeFormVisibility(newStatus: { [key: string]: number }): void | Called to notify the widget provider of the change in widget visibility.|
67| onFormEvent(formId: string, message: string): void | Called to instruct the widget provider to receive and process a widget event.|
68| onRemoveForm(formId: string): void| Called to notify the widget provider that a widget has been destroyed.|
69| onConfigurationUpdate(config: Configuration): void | Called when the configuration of the environment where the widget is running is updated.|
70| onShareForm?(formId: string): { [key: string]: any }| Called by the widget provider to receive shared widget data.|
71
72The **FormExtensionAbility** class also has a member context, that is, the FormExtensionContext class. For details, see [FormExtensionContext](../reference/apis/js-apis-inner-application-formExtensionContext.md).
73
74| API| Description|
75| -------- | -------- |
76| startAbility(want: Want, callback: AsyncCallback<void>): void | Starts UIAbility of the application to which a widget belongs. This API uses an asynchronous callback to return the result. (This is a system API and cannot be called by third-party applications. You must apply for the permission to use the API.)|
77| startAbility(want: Want): Promise<void> | Starts UIAbility of the application to which a widget belongs. This API uses a promise to return the result. (This is a system API and cannot be called by third-party applications. You must apply for the permission to use the API.)|
78
79The **FormProvider** class has the following APIs. For details, see [FormProvider](../reference/apis/js-apis-app-form-formProvider.md).
80
81| API| Description|
82| -------- | -------- |
83| setFormNextRefreshTime(formId: string, minute: number, callback: AsyncCallback<void>): void; | Sets the next refresh time for a widget. This API uses an asynchronous callback to return the result.|
84| setFormNextRefreshTime(formId: string, minute: number): Promise<void>; | Sets the next refresh time for a widget. This API uses a promise to return the result.|
85| updateForm(formId: string, formBindingData: FormBindingData, callback: AsyncCallback<void>): void; | Updates a widget. This API uses an asynchronous callback to return the result.|
86| updateForm(formId: string, formBindingData: FormBindingData): Promise<void>;| Updates a widget. This API uses a promise to return the result.|
87
88The **FormBindingData** class has the following APIs. For details, see [FormBindingData](../reference/apis/js-apis-app-form-formBindingData.md).
89
90| API| Description|
91| -------- | -------- |
92| createFormBindingData(obj?: Object \ string): FormBindingData| | Creates a **FormBindingData** object.|
93
94
95## How to Develop
96
97The widget provider development based on the [stage model](stage-model-development-overview.md) involves the following key steps:
98
99- [Creating a FormExtensionAbility Instance](#creating-a-formextensionability-instance): Develop the lifecycle callback functions of FormExtensionAbility.
100
101- [Configuring the Widget Configuration File](#configuring-the-widget-configuration-file): Configure the application configuration file **module.json5** and profile configuration file.
102
103- [Persistently Storing Widget Data](#persistently-storing-widget-data): Perform persistent management on widget information.
104
105- [Updating Widget Data](#updating-widget-data): Call **updateForm()** to update the information displayed on a widget.
106
107- [Developing the Widget UI Page](#developing-the-widget-ui-page): Use HML+CSS+JSON to develop a JS widget UI page.
108
109- [Developing Widget Events](#developing-widget-events): Add the router and message events for a widget.
110
111
112### Creating a FormExtensionAbility Instance
113
114To create a widget in the stage model, implement the lifecycle callbacks of **FormExtensionAbility**. Generate a widget template by referring to [Developing a Service Widget](https://developer.harmonyos.com/en/docs/documentation/doc-guides/ohos-development-service-widget-0000001263280425).
115
1161. Import related modules to **EntryFormAbility.ts**.
117
118   ```ts
119   import FormExtension from '@ohos.app.form.FormExtensionAbility';
120   import formBindingData from '@ohos.app.form.formBindingData';
121   import formInfo from '@ohos.app.form.formInfo';
122   import formProvider from '@ohos.app.form.formProvider';
123   import dataStorage from '@ohos.data.storage';
124   ```
125
1262. Implement the FormExtension lifecycle callbacks in **EntryFormAbility.ts**.
127
128   ```ts
129   export default class EntryFormAbility extends FormExtension {
130       onAddForm(want) {
131           console.info('[EntryFormAbility] onAddForm');
132           // Called when the widget is created. The widget provider should return the widget data binding class.
133           let obj = {
134               "title": "titleOnCreate",
135               "detail": "detailOnCreate"
136           };
137           let formData = formBindingData.createFormBindingData(obj);
138           return formData;
139       }
140       onCastToNormalForm(formId) {
141           // Called when the widget host converts the temporary widget into a normal one. The widget provider should do something to respond to the conversion.
142           console.info('[EntryFormAbility] onCastToNormalForm');
143       }
144       onUpdateForm(formId) {
145           // Override this method to support scheduled updates, periodic updates, or updates requested by the widget host.
146           console.info('[EntryFormAbility] onUpdateForm');
147           let obj = {
148               "title": "titleOnUpdate",
149               "detail": "detailOnUpdate"
150           };
151           let formData = formBindingData.createFormBindingData(obj);
152           formProvider.updateForm(formId, formData).catch((error) => {
153               console.info('[EntryFormAbility] updateForm, error:' + JSON.stringify(error));
154           });
155       }
156       onChangeFormVisibility(newStatus) {
157           // Called when the widget host initiates an event about visibility changes. The widget provider should do something to respond to the notification. This callback takes effect only for system applications.
158           console.info('[EntryFormAbility] onChangeFormVisibility');
159       }
160       onFormEvent(formId, message) {
161           // If the widget supports event triggering, override this method and implement the trigger.
162           console.info('[EntryFormAbility] onFormEvent');
163       }
164       onRemoveForm(formId) {
165           // Delete widget data.
166           console.info('[EntryFormAbility] onRemoveForm');
167       }
168       onConfigurationUpdate(config) {
169           console.info('[EntryFormAbility] nConfigurationUpdate, config:' + JSON.stringify(config));
170       }
171       onAcquireFormState(want) {
172           return formInfo.FormState.READY;
173       }
174   }
175   ```
176
177> **NOTE**
178>
179> FormExtensionAbility cannot reside in the background. Therefore, continuous tasks cannot be processed in the widget lifecycle callbacks.
180
181### Configuring the Widget Configuration File
182
1831. Configure ExtensionAbility information under **extensionAbilities** in the [module.json5 file](../quick-start/module-configuration-file.md). For a FormExtensionAbility, you must specify **metadata**. Specifically, set **name** to **ohos.extension.form** (fixed), and set **resource** to the index of the widget configuration information.
184   Example configuration:
185
186
187   ```json
188   {
189     "module": {
190       // ...
191       "extensionAbilities": [
192         {
193           "name": "EntryFormAbility",
194           "srcEntrance": "./ets/entryformability/EntryFormAbility.ts",
195           "label": "$string:EntryFormAbility_label",
196           "description": "$string:EntryFormAbility_desc",
197           "type": "form",
198           "metadata": [
199             {
200               "name": "ohos.extension.form",
201               "resource": "$profile:form_config"
202             }
203           ]
204         }
205       ]
206     }
207   }
208   ```
209
2102. Configure the widget configuration information. In the **metadata** configuration item of FormExtensionAbility, you can specify the resource index of specific configuration information of the widget. For example, if resource is set to **$profile:form_config**, **form_config.json** in the **resources/base/profile/** directory of the development view is used as the profile configuration file of the widget. The following table describes the internal field structure.
211     **Table 1** Widget profile configuration file
212
213   | Field| Description| Data Type| Initial Value Allowed|
214   | -------- | -------- | -------- | -------- |
215   | name | Class name of a widget. The value is a string with a maximum of 127 bytes.| String| No|
216   | description | Description of the widget. The value can be a string or a resource index to descriptions in multiple languages. The value is a string with a maximum of 255 bytes.| String| Yes (initial value: left empty)|
217   | src | Full path of the UI code corresponding to the widget.| String| No|
218   | window | Window-related configurations.| Object| Yes|
219   | isDefault | Whether the widget is a default one. Each ability has only one default widget.<br>**true**: The widget is the default one.<br>**false**: The widget is not the default one.| Boolean| No|
220   | colorMode | Color mode of the widget.<br>**auto**: The widget adopts the auto-adaptive color mode.<br>**dark**: The widget adopts the dark color mode.<br>**light**: The widget adopts the light color mode.| String| Yes (initial value: **auto**)|
221   | supportDimensions | Grid styles supported by the widget.<br>**1 * 2**: indicates a grid with one row and two columns.<br>**2 * 2**: indicates a grid with two rows and two columns.<br>**2 * 4**: indicates a grid with two rows and four columns.<br>**4 * 4**: indicates a grid with four rows and four columns.| String array| No|
222   | defaultDimension | Default grid style of the widget. The value must be available in the **supportDimensions** array of the widget.| String| No|
223   | updateEnabled | Whether the widget can be updated periodically.<br>**true**: The widget can be updated at a specified interval (**updateDuration**) or at the scheduled time (**scheduledUpdateTime**). **updateDuration** takes precedence over **scheduledUpdateTime**.<br>**false**: The widget cannot be updated periodically.| Boolean| No|
224   | scheduledUpdateTime | Scheduled time to update the widget. The value is in 24-hour format and accurate to minute.<br>**updateDuration** takes precedence over **scheduledUpdateTime**. If both are specified, the value specified by **updateDuration** is used.| String| Yes (initial value: **0:0**)|
225   | updateDuration | Interval to update the widget. The value is a natural number, in the unit of 30 minutes.<br>If the value is **0**, this field does not take effect.<br>If the value is a positive integer *N*, the interval is calculated by multiplying *N* and 30 minutes.<br>**updateDuration** takes precedence over **scheduledUpdateTime**. If both are specified, the value specified by **updateDuration** is used.| Number| Yes (initial value: **0**)|
226   | formConfigAbility | Link to a specific page of the application. The value is a URI.| String| Yes (initial value: left empty)|
227   | formVisibleNotify | Whether the widget is allowed to use the widget visibility notification.| String| Yes (initial value: left empty)|
228   | metaData | Metadata of the widget. This field contains the array of the **customizeData** field.| Object| Yes (initial value: left empty)|
229
230   Example configuration:
231
232   ```json
233   {
234     "forms": [
235       {
236         "name": "widget",
237         "description": "This is a widget.",
238         "src": "./js/widget/pages/index/index",
239         "window": {
240           "designWidth": 720,
241           "autoDesignWidth": true
242         },
243         "colorMode": "auto",
244         "isDefault": true,
245         "updateEnabled": true,
246         "scheduledUpdateTime": "10:30",
247         "updateDuration": 1,
248         "defaultDimension": "2*2",
249         "supportDimensions": [
250           "2*2"
251         ]
252       }
253     ]
254   }
255   ```
256
257
258### Persistently Storing Widget Data
259
260A widget provider is usually started when it is needed to provide information about a widget. The Widget Manager supports multi-instance management and uses the widget ID to identify an instance. If the widget provider supports widget data modification, it must persistently store the data based on the widget ID, so that it can access the data of the target widget when obtaining, updating, or starting a widget.
261
262
263```ts
264const DATA_STORAGE_PATH = "/data/storage/el2/base/haps/form_store";
265async function storeFormInfo(formId: string, formName: string, tempFlag: boolean) {
266    // Only the widget ID (formId), widget name (formName), and whether the widget is a temporary one (tempFlag) are persistently stored.
267    let formInfo = {
268        "formName": formName,
269        "tempFlag": tempFlag,
270        "updateCount": 0
271    };
272    try {
273        const storage = await dataStorage.getStorage(DATA_STORAGE_PATH);
274        // Put the widget information.
275        await storage.put(formId, JSON.stringify(formInfo));
276        console.info(`[EntryFormAbility] storeFormInfo, put form info successfully, formId: ${formId}`);
277        await storage.flush();
278    } catch (err) {
279        console.error(`[EntryFormAbility] failed to storeFormInfo, err: ${JSON.stringify(err)}`);
280    }
281}
282
283export default class EntryFormAbility extends FormExtension {
284    // ...
285    onAddForm(want) {
286        console.info('[EntryFormAbility] onAddForm');
287
288        let formId = want.parameters["ohos.extra.param.key.form_identity"];
289        let formName = want.parameters["ohos.extra.param.key.form_name"];
290        let tempFlag = want.parameters["ohos.extra.param.key.form_temporary"];
291        // Persistently store widget data for subsequent use, such as instance acquisition and update.
292        // Implement this API based on project requirements.
293        storeFormInfo(formId, formName, tempFlag);
294
295        let obj = {
296            "title": "titleOnCreate",
297            "detail": "detailOnCreate"
298        };
299        let formData = formBindingData.createFormBindingData(obj);
300        return formData;
301    }
302}
303```
304
305You should override **onRemoveForm** to implement widget data deletion.
306
307
308```ts
309const DATA_STORAGE_PATH = "/data/storage/el2/base/haps/form_store";
310async function deleteFormInfo(formId: string) {
311    try {
312        const storage = await dataStorage.getStorage(DATA_STORAGE_PATH);
313        // Delete the widget information.
314        await storage.delete(formId);
315        console.info(`[EntryFormAbility] deleteFormInfo, del form info successfully, formId: ${formId}`);
316        await storage.flush();
317    } catch (err) {
318        console.error(`[EntryFormAbility] failed to deleteFormInfo, err: ${JSON.stringify(err)}`);
319    }
320}
321
322// ...
323
324export default class EntryFormAbility extends FormExtension {
325    // ...
326    onRemoveForm(formId) {
327        console.info('[EntryFormAbility] onRemoveForm');
328        // Delete the persistent widget instance data.
329        // Implement this API based on project requirements.
330        deleteFormInfo(formId);
331    }
332}
333```
334
335For details about how to implement persistent data storage, see [Lightweight Data Store Development](../database/database-preference-guidelines.md).
336
337The **Want** object passed in by the widget host to the widget provider contains a flag that specifies whether the requested widget is normal or temporary.
338
339- Normal widget: a widget persistently used by the widget host
340
341- Temporary widget: a widget temporarily used by the widget host
342
343Data of a temporary widget will be deleted on the Widget Manager if the widget framework is killed and restarted. The widget provider, however, is not notified of the deletion and still keeps the data. Therefore, the widget provider needs to clear the data of temporary widgets proactively if the data has been kept for a long period of time. If the widget host has converted a temporary widget into a normal one, the widget provider should change the widget data from temporary storage to persistent storage. Otherwise, the widget data may be deleted by mistake.
344
345
346### Updating Widget Data
347
348When an application initiates a scheduled or periodic update, the application obtains the latest data and calls **updateForm()** to update the widget.
349
350
351```ts
352onUpdateForm(formId) {
353    // Override this method to support scheduled updates, periodic updates, or updates requested by the widget host.
354    console.info('[EntryFormAbility] onUpdateForm');
355    let obj = {
356        "title": "titleOnUpdate",
357        "detail": "detailOnUpdate"
358    };
359    let formData = formBindingData.createFormBindingData(obj);
360    // Call the updateForm() method to update the widget. Only the data passed through the input parameter is updated. Other information remains unchanged.
361    formProvider.updateForm(formId, formData).catch((error) => {
362        console.info('[EntryFormAbility] updateForm, error:' + JSON.stringify(error));
363    });
364}
365```
366
367
368### Developing the Widget UI Page
369
370You can use the web-like paradigm (HML+CSS+JSON) to develop JS widget pages. This section describes how to develop a page shown below.
371
372![widget-development-stage](figures/widget-development-stage.png)
373
374> **NOTE**
375>
376> Only the JavaScript-based web-like development paradigm is supported when developing the widget UI.
377
378- HML: uses web-like paradigm components to describe the widget page information.
379
380  ```html
381  <div class="container">
382    <stack>
383      <div class="container-img">
384        <image src="/common/widget.png" class="bg-img"></image>
385      </div>
386      <div class="container-inner">
387        <text class="title">{{title}}</text>
388        <text class="detail_text" onclick="routerEvent">{{detail}}</text>
389      </div>
390    </stack>
391  </div>
392  ```
393
394- CSS: defines style information about the web-like paradigm components in HML.
395
396  ```css
397  .container {
398    flex-direction: column;
399    justify-content: center;
400    align-items: center;
401  }
402
403  .bg-img {
404    flex-shrink: 0;
405    height: 100%;
406  }
407
408  .container-inner {
409    flex-direction: column;
410    justify-content: flex-end;
411    align-items: flex-start;
412    height: 100%;
413    width: 100%;
414    padding: 12px;
415  }
416
417  .title {
418    font-size: 19px;
419    font-weight: bold;
420    color: white;
421    text-overflow: ellipsis;
422    max-lines: 1;
423  }
424
425  .detail_text {
426    font-size: 16px;
427    color: white;
428    opacity: 0.66;
429    text-overflow: ellipsis;
430    max-lines: 1;
431    margin-top: 6px;
432  }
433  ```
434
435- JSON: defines data and event interaction on the widget UI page.
436
437  ```json
438  {
439    "data": {
440      "title": "TitleDefault",
441      "detail": "TextDefault"
442    },
443    "actions": {
444      "routerEvent": {
445        "action": "router",
446        "abilityName": "EntryAbility",
447        "params": {
448          "message": "add detail"
449        }
450      }
451    }
452  }
453  ```
454
455
456### Developing Widget Events
457
458You can set router and message events for components on a widget. The router event applies to ability redirection, and the message event applies to custom click events.
459
460The key steps are as follows:
461
4621. Set the **onclick** field in the HML file to **routerEvent** or **messageEvent**, depending on the **actions** settings in the JSON file.
463
4642. Set the router event.
465   - **action**: **"router"**, which indicates a router event.
466   - **abilityName**: name of the ability to redirect to (PageAbility component in the FA model and UIAbility component in the stage model). For example, the default UIAbility name of the stage model created by DevEco Studio is EntryAbility.
467   - **params**: custom parameters passed to the target ability. Set them as required. The value can be obtained from **parameters** in **want** used for starting the target ability. For example, in the lifecycle function **onCreate** of the main ability in the stage model, you can obtain **want** and its **parameters** field.
468
4693. Set the message event.
470   - **action**: **"message"**, which indicates a message event.
471   - **params**: custom parameters of the message event. Set them as required. The value can be obtained from **message** in the widget lifecycle function **onFormEvent()**.
472
473The following is an example:
474
475- HML file:
476
477  ```html
478  <div class="container">
479    <stack>
480      <div class="container-img">
481        <image src="/common/widget.png" class="bg-img"></image>
482      </div>
483      <div class="container-inner">
484        <text class="title" onclick="routerEvent">{{title}}</text>
485        <text class="detail_text" onclick="messageEvent">{{detail}}</text>
486      </div>
487    </stack>
488  </div>
489  ```
490
491- CSS file:
492
493  ```css
494  .container {
495    flex-direction: column;
496    justify-content: center;
497    align-items: center;
498  }
499
500  .bg-img {
501    flex-shrink: 0;
502    height: 100%;
503  }
504
505  .container-inner {
506    flex-direction: column;
507    justify-content: flex-end;
508    align-items: flex-start;
509    height: 100%;
510    width: 100%;
511    padding: 12px;
512  }
513
514  .title {
515    font-size: 19px;
516    font-weight: bold;
517    color: white;
518    text-overflow: ellipsis;
519    max-lines: 1;
520  }
521
522  .detail_text {
523    font-size: 16px;
524    color: white;
525    opacity: 0.66;
526    text-overflow: ellipsis;
527    max-lines: 1;
528    margin-top: 6px;
529  }
530  ```
531
532- JSON file:
533
534  ```json
535  {
536    "data": {
537      "title": "TitleDefault",
538      "detail": "TextDefault"
539    },
540    "actions": {
541      "routerEvent": {
542        "action": "router",
543        "abilityName": "EntryAbility",
544        "params": {
545          "info": "router info",
546          "message": "router message"
547        }
548      },
549      "messageEvent": {
550        "action": "message",
551        "params": {
552          "detail": "message detail"
553        }
554      }
555    }
556  }
557  ```
558
559- Receive the router event and obtain parameters in UIAbility.
560
561  ```ts
562  import UIAbility from '@ohos.app.ability.UIAbility'
563
564  export default class EntryAbility extends UIAbility {
565      onCreate(want, launchParam) {
566          // Obtain the info parameter passed in the router event.
567          if (want.parameters.info === "router info") {
568              // Do something.
569              // console.log("router info:" + want.parameters.info)
570          }
571          // Obtain the message parameter passed in the router event.
572          if (want.parameters.message === "router message") {
573              // Do something.
574              // console.log("router message:" + want.parameters.message)
575          }
576      }
577      // ...
578  };
579  ```
580
581- Receive the message event in FormExtensionAbility and obtain parameters.
582
583  ```ts
584  import FormExtension from '@ohos.app.form.FormExtensionAbility';
585
586  export default class FormAbility extends FormExtension {
587      // ...
588      onFormEvent(formId, message) {
589          // Obtain the detail parameter passed in the message event.
590          let msg = JSON.parse(message)
591          if (msg.params.detail === "message detail") {
592              // Do something.
593              // console.log("message info:" + msg.params.detail)
594          }
595      }
596      // ...
597  };
598  ```
599
600