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