• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// [Start js_card_form_ability]
17// [Start remove_form_interface]
18// [Start update_form_interface]
19import { common, Want } from '@kit.AbilityKit';
20// [Start receive_message_interface]
21import { hilog } from '@kit.PerformanceAnalysisKit';
22import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit';
23// [StartExclude receive_message_interface]
24import { BusinessError } from '@kit.BasicServicesKit';
25import { preferences } from '@kit.ArkData';
26import { Configuration } from '@kit.ArkUI';
27// [EndExclude receive_message_interface]
28
29const TAG: string = 'JsCardFormAbility';
30// [StartExclude update_form_interface]
31// [StartExclude receive_message_interface]
32const DATA_STORAGE_PATH: string = '/data/storage/el2/base/haps/form_store';
33// [EndExclude update_form_interface]
34// [EndExclude receive_message_interface]
35const DOMAIN_NUMBER: number = 0xFF00;
36
37// [StartExclude remove_form_interface]
38// [StartExclude update_form_interface]
39// [StartExclude receive_message_interface]
40let storeFormInfo =
41  async (formId: string, formName: string, tempFlag: boolean, context: common.FormExtensionContext): Promise<void> => {
42    // 此处仅对卡片ID:formId,卡片名:formName和是否为临时卡片:tempFlag进行了持久化
43    let formInfo: Record<string, string | boolean | number> = {
44      formName: formName,
45      tempFlag: tempFlag,
46      updateCount: 0
47    };
48    try {
49      const storage: preferences.Preferences = await preferences.getPreferences(context, DATA_STORAGE_PATH);
50      // put form info
51      await storage.put(formId, JSON.stringify(formInfo));
52      hilog.info(DOMAIN_NUMBER, TAG, `[EntryFormAbility] storeFormInfo, put form info successfully, formId: ${formId}`);
53      await storage.flush();
54    } catch (err) {
55      hilog.error(DOMAIN_NUMBER, TAG,
56        `[EntryFormAbility] failed to storeFormInfo, err: ${JSON.stringify(err as BusinessError)}`);
57    }
58    ;
59  };
60
61// [StartExclude js_card_form_ability]
62// [EndExclude remove_form_interface]
63let deleteFormInfo = async (formId: string, context: common.FormExtensionContext): Promise<void> => {
64  try {
65    const storage: preferences.Preferences = await preferences.getPreferences(context, DATA_STORAGE_PATH);
66    // del form info
67    await storage.delete(formId);
68    hilog.info(DOMAIN_NUMBER, TAG, `[EntryFormAbility] deleteFormInfo, del form info successfully, formId: ${formId}`);
69    await storage.flush();
70  } catch (err) {
71    hilog.error(DOMAIN_NUMBER, TAG,
72      `[EntryFormAbility] failed to deleteFormInfo, err: ${JSON.stringify(err as BusinessError)}`);
73  }
74  ;
75};
76// [EndExclude js_card_form_ability]
77// [EndExclude update_form_interface]
78// [EndExclude receive_message_interface]
79export default class JsCardFormAbility extends FormExtensionAbility {
80  // [StartExclude remove_form_interface]
81  // [StartExclude update_form_interface]
82  // [StartExclude receive_message_interface]
83  onAddForm(want: Want): formBindingData.FormBindingData {
84    hilog.info(DOMAIN_NUMBER, TAG, '[JsCardFormAbility] onAddForm');
85
86    if (want.parameters) {
87      let formId = JSON.stringify(want.parameters['ohos.extra.param.key.form_identity']);
88      let formName = JSON.stringify(want.parameters['ohos.extra.param.key.form_name']);
89      let tempFlag = want.parameters['ohos.extra.param.key.form_temporary'] as boolean;
90      // 将创建的卡片信息持久化,以便在下次获取/更新该卡片实例时进行使用
91      // 此接口请根据实际情况实现,具体请参考:FormExtAbility Stage模型卡片实例
92      storeFormInfo(formId, formName, tempFlag, this.context);
93    }
94
95    // 使用方创建卡片时触发,提供方需要返回卡片数据绑定类
96    let obj: Record<string, string> = {
97      title: 'titleOnCreate',
98      detail: 'detailOnCreate'
99    };
100    let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj);
101    return formData;
102  }
103
104  // [StartExclude js_card_form_ability]
105  onCastToNormalForm(formId: string): void {
106    // 使用方将临时卡片转换为常态卡片触发,提供方需要做相应的处理
107    hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onCastToNormalForm');
108  }
109
110  // [EndExclude update_form_interface]
111  onUpdateForm(formId: string): void {
112    // 若卡片支持定时更新/定点更新/卡片使用方主动请求更新功能,则提供方需要重写该方法以支持数据更新
113    hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onUpdateForm');
114    let obj: Record<string, string> = {
115      title: 'titleOnUpdate',
116      detail: 'detailOnUpdate'
117    };
118    let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj);
119    formProvider.updateForm(formId, formData).catch((error: BusinessError) => {
120      hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] updateForm, error:' + JSON.stringify(error));
121    });
122  }
123  // [StartExclude update_form_interface]
124
125  onChangeFormVisibility(newStatus: Record<string, number>): void {
126    // 使用方发起可见或者不可见通知触发,提供方需要做相应的处理,仅系统应用生效
127    hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onChangeFormVisibility');
128  }
129  // [EndExclude receive_message_interface]
130
131  onFormEvent(formId: string, message: string): void {
132    // 若卡片支持触发事件,则需要重写该方法并实现对事件的触发
133    hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onFormEvent');
134    // 获取message事件中传递的detail参数
135    let msg: Record<string, string> = JSON.parse(message);
136    if (msg.detail === 'message detail') {
137      // do something
138      hilog.info(DOMAIN_NUMBER, TAG, 'message info:' + msg.detail);
139    }
140  }
141  // [StartExclude receive_message_interface]
142  // [EndExclude remove_form_interface]
143  onRemoveForm(formId: string): void {
144    // 删除卡片实例数据
145    hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onRemoveForm');
146    // 删除之前持久化的卡片实例数据
147    // 此接口请根据实际情况实现,具体请参考:FormExtAbility Stage模型卡片实例
148    deleteFormInfo(formId, this.context);
149  }
150  // [StartExclude js_card_form_ability]
151
152  onConfigurationUpdate(config: Configuration) {
153    // 当前formExtensionAbility存活时更新系统配置信息时触发的回调。
154    // 需注意:formExtensionAbility创建后5秒内无操作将会被清理。
155    hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onConfigurationUpdate:' + JSON.stringify(config));
156  }
157
158  onAcquireFormState(want: Want) {
159    // 卡片提供方接收查询卡片状态通知接口,默认返回卡片初始状态。
160    return formInfo.FormState.READY;
161  }
162  // [EndExclude js_card_form_ability]
163  // [EndExclude remove_form_interface]
164  // [EndExclude update_form_interface]
165  // [EndExclude receive_message_interface]
166}
167// [End js_card_form_ability]
168// [End remove_form_interface]
169// [End update_form_interface]
170// [End receive_message_interface]