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 16import { common, Want } from '@kit.AbilityKit'; 17import { hilog } from '@kit.PerformanceAnalysisKit'; 18import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit'; 19import { BusinessError } from '@kit.BasicServicesKit'; 20import { preferences } from '@kit.ArkData'; 21import { Configuration } from '@kit.ArkUI'; 22 23const TAG: string = 'JsCardFormAbility'; 24const DATA_STORAGE_PATH: string = '/data/storage/el2/base/haps/form_store'; 25const DOMAIN_NUMBER: number = 0xFF00; 26 27let storeFormInfo = 28 async (formId: string, formName: string, tempFlag: boolean, context: common.FormExtensionContext): Promise<void> => { 29 // 此处仅对卡片ID:formId,卡片名:formName和是否为临时卡片:tempFlag进行了持久化 30 let formInfo: Record<string, string | boolean | number> = { 31 formName: formName, 32 tempFlag: tempFlag, 33 updateCount: 0 34 }; 35 try { 36 const storage: preferences.Preferences = await preferences.getPreferences(context, DATA_STORAGE_PATH); 37 // put form info 38 await storage.put(formId, JSON.stringify(formInfo)); 39 hilog.info(DOMAIN_NUMBER, TAG, `[EntryFormAbility] storeFormInfo, put form info successfully, formId: ${formId}`); 40 await storage.flush(); 41 } catch (err) { 42 hilog.error(DOMAIN_NUMBER, TAG, 43 `[EntryFormAbility] failed to storeFormInfo, err: ${JSON.stringify(err as BusinessError)}`); 44 } 45 ; 46 }; 47 48let deleteFormInfo = async (formId: string, context: common.FormExtensionContext): Promise<void> => { 49 try { 50 const storage: preferences.Preferences = await preferences.getPreferences(context, DATA_STORAGE_PATH); 51 // del form info 52 await storage.delete(formId); 53 hilog.info(DOMAIN_NUMBER, TAG, `[EntryFormAbility] deleteFormInfo, del form info successfully, formId: ${formId}`); 54 await storage.flush(); 55 } catch (err) { 56 hilog.error(DOMAIN_NUMBER, TAG, 57 `[EntryFormAbility] failed to deleteFormInfo, err: ${JSON.stringify(err as BusinessError)}`); 58 } 59 ; 60}; 61 62export default class JsCardFormAbility extends FormExtensionAbility { 63 onAddForm(want: Want): formBindingData.FormBindingData { 64 hilog.info(DOMAIN_NUMBER, TAG, '[JsCardFormAbility] onAddForm'); 65 66 if (want.parameters) { 67 let formId = JSON.stringify(want.parameters['ohos.extra.param.key.form_identity']); 68 let formName = JSON.stringify(want.parameters['ohos.extra.param.key.form_name']); 69 let tempFlag = want.parameters['ohos.extra.param.key.form_temporary'] as boolean; 70 // 将创建的卡片信息持久化,以便在下次获取/更新该卡片实例时进行使用 71 // 此接口请根据实际情况实现,具体请参考:FormExtAbility Stage模型卡片实例 72 storeFormInfo(formId, formName, tempFlag, this.context); 73 } 74 75 // 使用方创建卡片时触发,提供方需要返回卡片数据绑定类 76 let obj: Record<string, string> = { 77 title: 'titleOnCreate', 78 detail: 'detailOnCreate' 79 }; 80 let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj); 81 return formData; 82 } 83 84 onCastToNormalForm(formId: string): void { 85 // 使用方将临时卡片转换为常态卡片触发,提供方需要做相应的处理 86 hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onCastToNormalForm'); 87 } 88 89 onUpdateForm(formId: string): void { 90 // 若卡片支持定时更新/定点更新/卡片使用方主动请求更新功能,则提供方需要重写该方法以支持数据更新 91 hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onUpdateForm'); 92 let obj: Record<string, string> = { 93 title: 'titleOnUpdate', 94 detail: 'detailOnUpdate' 95 }; 96 let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj); 97 formProvider.updateForm(formId, formData).catch((error: BusinessError) => { 98 hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] updateForm, error:' + JSON.stringify(error)); 99 }); 100 } 101 102 onChangeFormVisibility(newStatus: Record<string, number>): void { 103 // 使用方发起可见或者不可见通知触发,提供方需要做相应的处理,仅系统应用生效 104 hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onChangeFormVisibility'); 105 } 106 107 onFormEvent(formId: string, message: string): void { 108 // 若卡片支持触发事件,则需要重写该方法并实现对事件的触发 109 hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onFormEvent'); 110 // 获取message事件中传递的detail参数 111 let msg: Record<string, string> = JSON.parse(message); 112 if (msg.detail === 'message detail') { 113 // do something 114 hilog.info(DOMAIN_NUMBER, TAG, 'message info:' + msg.detail); 115 } 116 } 117 118 onRemoveForm(formId: string): void { 119 // 删除卡片实例数据 120 hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onRemoveForm'); 121 // 删除之前持久化的卡片实例数据 122 // 此接口请根据实际情况实现,具体请参考:FormExtAbility Stage模型卡片实例 123 deleteFormInfo(formId, this.context); 124 } 125 126 onConfigurationUpdate(config: Configuration) { 127 // 当前formExtensionAbility存活时更新系统配置信息时触发的回调。 128 // 需注意:formExtensionAbility创建后5秒内无操作将会被清理。 129 hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onConfigurationUpdate:' + JSON.stringify(config)); 130 } 131 132 onAcquireFormState(want: Want) { 133 // 卡片提供方接收查询卡片状态通知接口,默认返回卡片初始状态。 134 return formInfo.FormState.READY; 135 } 136}