1/* 2 * Copyright (c) 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 featureAbility from '@ohos.ability.featureAbility'; 17import type Want from '@ohos.app.ability.Want'; 18import formBindingData from '@ohos.app.form.formBindingData'; 19import formInfo from '@ohos.app.form.formInfo'; 20import formProvider from '@ohos.app.form.formProvider'; 21import dataPreferences from '@ohos.data.preferences'; 22import hilog from '@ohos.hilog'; 23 24const TAG: string = '[Sample_FAModelAbilityDevelop]'; 25const domain: number = 0xFF00; 26const DATA_STORAGE_PATH: string = 'form_store'; 27let storeFormInfo = async (formId: string, formName: string, tempFlag: boolean, context: featureAbility.Context): Promise<void> => { 28 // 此处仅对卡片ID:formId,卡片名:formName和是否为临时卡片:tempFlag进行了持久化 29 let formInfo: Record<string, string | number | boolean> = { 30 formName: 'formName', 31 tempFlag: 'tempFlag', 32 updateCount: 0 33 }; 34 try { 35 const storage = await dataPreferences.getPreferences(context, DATA_STORAGE_PATH); 36 // put form info 37 await storage.put(formId, JSON.stringify(formInfo)); 38 hilog.info(domain, TAG, `storeFormInfo, put form info successfully, formId: ${formId}`); 39 await storage.flush(); 40 } catch (err) { 41 hilog.error(domain, TAG, `failed to storeFormInfo, err: ${JSON.stringify(err as Error)}`); 42 } 43}; 44 45let deleteFormInfo = async (formId: string, context: featureAbility.Context): Promise<void> => { 46 try { 47 const storage = await dataPreferences.getPreferences(context, DATA_STORAGE_PATH); 48 // del form info 49 await storage.delete(formId); 50 hilog.info(domain, TAG, `deleteFormInfo, del form info successfully, formId: ${formId}`); 51 await storage.flush(); 52 } catch (err) { 53 hilog.error(domain, TAG, `failed to deleteFormInfo, err: ${JSON.stringify(err)}`); 54 } 55}; 56 57class LifeCycle { 58 onCreate: (want: Want) => formBindingData.FormBindingData = (want) => ({ data: '' }); 59 onCastToNormal: (formId: string) => void = (formId) => { 60 }; 61 onUpdate: (formId: string) => void = (formId) => { 62 }; 63 onVisibilityChange: (newStatus: Record<string, number>) => void = (newStatus) => { 64 let obj: Record<string, number> = { 65 'test': 1 66 }; 67 return obj; 68 }; 69 onEvent: (formId: string, message: string) => void = (formId, message) => { 70 }; 71 onDestroy: (formId: string) => void = (formId) => { 72 }; 73 onAcquireFormState?: (want: Want) => formInfo.FormState = (want) => (0); 74 onShare?: (formId: string) => Record<string, number | string | boolean | object | undefined | null> = (formId) => { 75 let obj: Record<string, number> = { 76 test: 1 77 }; 78 return obj; 79 }; 80} 81 82let obj: LifeCycle = { 83 onCreate(want: Want) { 84 hilog.info(domain, TAG, 'FormAbility onCreate'); 85 if (want.parameters) { 86 let formId = String(want.parameters['ohos.extra.param.key.form_identity']); 87 let formName = String(want.parameters['ohos.extra.param.key.form_name']); 88 let tempFlag = Boolean(want.parameters['ohos.extra.param.key.form_temporary']); 89 // 将创建的卡片信息持久化,以便在下次获取/更新该卡片实例时进行使用 90 // 此接口请根据实际情况实现,具体请参考:FormExtAbility Stage模型卡片实例 91 hilog.info(domain, TAG, 'FormAbility onCreate' + formId); 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 onCastToNormal(formId: string) { 104 // 使用方将临时卡片转换为常态卡片触发,提供方需要做相应的处理 105 hilog.info(domain, TAG, 'FormAbility onCastToNormal'); 106 }, 107 onUpdate(formId: string) { 108 // 若卡片支持定时更新/定点更新/卡片使用方主动请求更新功能,则提供方需要重写该方法以支持数据更新 109 hilog.info(domain, TAG, 'FormAbility onUpdate'); 110 let obj: Record<string, string> = { 111 title: 'titleOnUpdate', 112 detail: 'detailOnUpdate' 113 }; 114 let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj); 115 // 调用updateForm接口去更新对应的卡片,仅更新入参中携带的数据信息,其他信息保持不变 116 formProvider.updateForm(formId, formData).catch((error: Error) => { 117 hilog.error(domain, TAG, 'FormAbility updateForm, error:' + JSON.stringify(error)); 118 }); 119 }, 120 onVisibilityChange(newStatus: Record<string, number>) { 121 // 使用方发起可见或者不可见通知触发,提供方需要做相应的处理,仅系统应用生效 122 hilog.info(domain, TAG, 'FormAbility onVisibilityChange'); 123 }, 124 onEvent(formId: string, message: string) { 125 // 若卡片支持触发事件,则需要重写该方法并实现对事件的触发 126 let obj: Record<string, string> = { 127 title: 'titleOnEvent', 128 detail: 'detailOnEvent' 129 }; 130 let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj); 131 // 调用updateForm接口去更新对应的卡片,仅更新入参中携带的数据信息,其他信息保持不变 132 formProvider.updateForm(formId, formData).catch((error: Error) => { 133 hilog.error(domain, TAG, 'FormAbility updateForm, error:' + JSON.stringify(error)); 134 }); 135 hilog.info(domain, TAG, 'FormAbility onEvent'); 136 }, 137 onDestroy(formId: string) { 138 // 删除卡片实例数据 139 hilog.info(domain, TAG, 'FormAbility onDestroy'); 140 // 删除之前持久化的卡片实例数据 141 // 此接口请根据实际情况实现,具体请参考:FormExtAbility Stage模型卡片实例 142 deleteFormInfo(formId, this.context); 143 }, 144 onAcquireFormState(want: Want) { 145 hilog.info(domain, TAG, 'FormAbility onAcquireFormState'); 146 return formInfo.FormState.READY; 147 } 148}; 149 150export default obj;