diff --git a/FormExtAbility/AppScope/app.json5 b/FormExtAbility/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b8503a42990004309af97dafe616664147b424fd --- /dev/null +++ b/FormExtAbility/AppScope/app.json5 @@ -0,0 +1,13 @@ +{ + "app": { + "bundleName": "ohos.samples.FormApplication", + "vendor": "samples", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name", + "distributedNotificationEnabled": true, + "minAPIVersion": 9, + "targetAPIVersion": 9 + } +} diff --git a/FormExtAbility/AppScope/resources/base/element/string.json b/FormExtAbility/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f57f59f5345916c7f416e811235c6d706384b040 --- /dev/null +++ b/FormExtAbility/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "FormApplication" + } + ] +} diff --git a/FormExtAbility/build-profile.json5 b/FormExtAbility/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0cc5c856df497c85f756e379e24ec2a5de8cb5ba --- /dev/null +++ b/FormExtAbility/build-profile.json5 @@ -0,0 +1,40 @@ +{ + "app": { + "signingConfigs": [ + { + "name": "default", + "material": { + "certpath": "C:\\Users\\dwx1140490\\.ohos\\config\\openharmony\\auto_ohos.cer", + "storePassword": "00000018E6A239278CEDE269F3233D751C970F7C478C5BE3B20DDC69AB981537FB3A2A9AC8BA7E8D", + "keyAlias": "debugKey", + "keyPassword": "00000018344AD7FA0D2FAAABD6136F0BF458E251613CE69C7BA13B017A92DA1875D341266EE07E95", + "profile": "C:\\Users\\dwx1140490\\.ohos\\config\\openharmony\\auto_ohos_default_ohos.samples.FormApplication.p7b", + "signAlg": "SHA256withECDSA", + "storeFile": "C:\\Users\\dwx1140490\\.ohos\\config\\openharmony\\auto_ohos.p12" + } + } + ], + "compileSdkVersion": 9, + "compatibleSdkVersion": 9, + "products": [ + { + "name": "default", + "signingConfig": "default", + }, + ], + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default", + ], + }, + ], + }, + ], +} \ No newline at end of file diff --git a/FormExtAbility/entry/build-profile.json5 b/FormExtAbility/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..bbd5f525872a55fecd376afba7c43c50d28dca45 --- /dev/null +++ b/FormExtAbility/entry/build-profile.json5 @@ -0,0 +1,14 @@ +{ + "apiType": 'stageMode', + "showInServiceCenter": true, + "buildOption": { + }, + "targets": [ + { + "name": "default", + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/FormExtAbility/entry/hvigorfile.js b/FormExtAbility/entry/hvigorfile.js new file mode 100644 index 0000000000000000000000000000000000000000..d7720ee6a7aad5c617d1fd2f6fc8c87067bfa32c --- /dev/null +++ b/FormExtAbility/entry/hvigorfile.js @@ -0,0 +1,2 @@ +// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. +module.exports = require('@ohos/hvigor-ohos-plugin').hapTasks diff --git a/FormExtAbility/entry/package.json b/FormExtAbility/entry/package.json new file mode 100644 index 0000000000000000000000000000000000000000..dfa2726ff1c9ad35dcbf24c5feb7928630b5c6d2 --- /dev/null +++ b/FormExtAbility/entry/package.json @@ -0,0 +1,11 @@ +{ + "devDependencies": {}, + "name": "entry", + "ohos": { + "org": "huawei", + "directoryLevel": "module", + "buildTool": "hvigor" + }, + "version": "1.0.0", + "dependencies": {} +} diff --git a/FormExtAbility/entry/src/main/ets/Application/AbilityStage.ts b/FormExtAbility/entry/src/main/ets/Application/AbilityStage.ts new file mode 100644 index 0000000000000000000000000000000000000000..2504b97755c185a49eb93b650462858183194f1a --- /dev/null +++ b/FormExtAbility/entry/src/main/ets/Application/AbilityStage.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import AbilityStage from "@ohos.application.AbilityStage" +import Logger from '../model/Logger'; + +export default class MyAbilityStage extends AbilityStage { + onCreate() { + Logger.log("[Demo] MyAbilityStage onCreate") + } +} \ No newline at end of file diff --git a/FormExtAbility/entry/src/main/ets/FormAbility/FormAbility.ts b/FormExtAbility/entry/src/main/ets/FormAbility/FormAbility.ts new file mode 100644 index 0000000000000000000000000000000000000000..258a6b220db36c459a61211e1471134f385013b4 --- /dev/null +++ b/FormExtAbility/entry/src/main/ets/FormAbility/FormAbility.ts @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import dataStorage from '@ohos.data.storage'; +import FormExtension from '@ohos.application.FormExtension'; +import formBindingData from '@ohos.application.formBindingData'; +import formInfo from '@ohos.application.formInfo'; +import formProvider from '@ohos.application.formProvider'; +import Logger from '../model/Logger' + + +const DATA_STORAGE_PATH = "/data/storage/el2/base/haps/form_store"; +const FORM_PARAM_IDENTITY_KEY = "ohos.extra.param.key.form_identity"; +const FORM_PARAM_NAME_KEY = "ohos.extra.param.key.form_name"; +const FORM_PARAM_TEMPORARY_KEY = "ohos.extra.param.key.form_temporary"; +var timer : number = 0 + +function getTemperature(formId: string, count: number) { + const DECIMAL: number = 10; + const parsedFormId: number = parseInt(formId, DECIMAL); + const BASE_TEMP_MOD: number = 20; + const baseTemperature: number = parsedFormId % BASE_TEMP_MOD; + const RANGE_TEMP_MOD: number = 20; + return baseTemperature + Math.abs(count % RANGE_TEMP_MOD - RANGE_TEMP_MOD / 2); +} + +function padZero(num: number) { + // trans num to string and pad 0 + if (num < 10) { + return `0${num}`; + } else { + return num.toString(); + } +} + +function getTime() { + const date = new Date(); + const hours = padZero(date.getHours()); + const minutes = padZero(date.getMinutes()); + const seconds = padZero(date.getSeconds()); + return `${hours}:${minutes}:${seconds}` +} + +async function storeFormInfo(formId: string, formName: string, tempFlag: boolean) { + let formInfo = { + "formName": formName, + "tempFlag": tempFlag, + "updateCount": 0 + }; + try { + const storage = await dataStorage.getStorage(DATA_STORAGE_PATH); + // put form info + await storage.put(formId, JSON.stringify(formInfo)); + Logger.log(`storeFormInfo, put form info successfully, formId: ${formId}`); + await storage.flush(); + } catch (err) { + Logger.error(`failed to storeFormInfo, err: ${JSON.stringify(err)}`); + } +} + +async function updateTempFormInfo(formId: string) { + let formInfoDefault = { + "formName": "", + "tempFlag": false, + "updateCount": 0 + }; + try { + const storage = await dataStorage.getStorage(DATA_STORAGE_PATH); + // get form info + const data = await storage.get(formId, JSON.stringify(formInfoDefault)); + Logger.log(`updateTempFormInfo, get form info successfully, formId: ${formId}`); + const formInfo = JSON.parse(data.toString()); + if (!formInfo.tempFlag) { + Logger.log(`updateTempFormInfo, formId: ${formId} is not temporary.`); + return; + } + + formInfo.tempFlag = false; + // update form info + await storage.put(formId, JSON.stringify(formInfo)); + Logger.log(`updateTempFormInfo, update form info successfully, formId: ${formId}`); + await storage.flush(); + } catch (err) { + Logger.error(`failed to updateTempFormInfo, err: ${JSON.stringify(err)}`); + } +} + +async function updateForm(formId: string) { + let formInfoDefault = { + "formName": "", + "tempFlag": false, + "updateCount": 0 + }; + try { + const storage = await dataStorage.getStorage(DATA_STORAGE_PATH); + // get form info + const data = await storage.get(formId, JSON.stringify(formInfoDefault)); + Logger.log(`updateForm, get form info successfully, formId: ${formId}`); + const formInfo = JSON.parse(data.toString()); + formInfo.updateCount = formInfo.updateCount + 1; + + let obj = { + "temperature": getTemperature(formId, formInfo.updateCount).toString(), + "time": getTime() + }; + let formData = formBindingData.createFormBindingData(obj); + formProvider.updateForm(formId, formData).catch((err) => { + Logger.error(`updateForm, err: ${JSON.stringify(err)}`); + }); + + // update form info + await storage.put(formId, JSON.stringify(formInfo)); + Logger.log(`updateForm, update form info successfully, formId: ${formId}`); + await storage.flush(); + } catch (err) { + Logger.error(`failed to updateForm, err: ${JSON.stringify(err)}`); + } +} + + +async function deleteFormInfo(formId: string) { + try { + const storage = await dataStorage.getStorage(DATA_STORAGE_PATH); + // del form info + await storage.delete(formId); + Logger.log(`deleteFormInfo, del form info successfully, formId: ${formId}`); + await storage.flush(); + } catch (err) { + Logger.error(`failed to deleteFormInfo, err: ${JSON.stringify(err)}`); + } +} + +export default class FormAbility extends FormExtension { + onCreate(want) { + Logger.log(`FormAbility onCreate, want: ${JSON.stringify(want)}`); + + // get form info + let formId = want.parameters[FORM_PARAM_IDENTITY_KEY]; + let formName = want.parameters[FORM_PARAM_NAME_KEY]; + let tempFlag = want.parameters[FORM_PARAM_TEMPORARY_KEY]; + storeFormInfo(formId, formName, tempFlag); + + let obj = { + "temperature": getTemperature(formId, 0).toString(), + "time": getTime() + }; + let formData = formBindingData.createFormBindingData(obj); + return formData; + } + + onCastToNormal(formId) { + Logger.log(`FormAbility onCastToNormal, formId: ${formId}`); + updateTempFormInfo(formId); + } + + onUpdate(formId) { + Logger.log(`FormAbility onUpdate, formId: ${formId}`); + updateForm(formId); + } + + onVisibilityChange(newStatus) { + Logger.log(`FormAbility onVisibilityChange`); + } + + onEvent(formId, message) { + Logger.log(`FormAbility onEvent, formId = ${formId}, message: ${JSON.stringify(message)}`); + + let jsonParams = JSON.parse(message) + let eventName = jsonParams.params.name + + switch(eventName){ + case "onUpdateEvent" : + this.onUpdate(formId) + break; + case "openRefresh" : + Logger.log("openRefresh started ") + + let count = 1 + timer = setInterval(()=>{ + Logger.log("231--- count : " + count++) + this.onUpdate(formId) + },1000) + Logger.log("openRefresh end timer : " + timer) + break; + case "closeRefresh" : + Logger.log("closeRefresh started : " + timer) + clearInterval(timer) + Logger.log("closeRefresh end timer : " + timer) + break; + default : + Logger.log("(onEvent) formId :" + formId + " message : " + message ) + break; + } + + + } + + onDestroy(formId) { + Logger.log(`FormAbility onDestroy, formId = ${formId}`); + deleteFormInfo(formId); +// clearTimeout(timeout) + } + + onAcquireFormState(want) { + Logger.log(`FormAbility onAcquireFormState`); + return formInfo.FormState.READY; + } +}; \ No newline at end of file diff --git a/FormExtAbility/entry/src/main/ets/MainAbility/MainAbility.ts b/FormExtAbility/entry/src/main/ets/MainAbility/MainAbility.ts new file mode 100644 index 0000000000000000000000000000000000000000..bfcce5d8efa33692ea68b1d7690405d4396ac7a9 --- /dev/null +++ b/FormExtAbility/entry/src/main/ets/MainAbility/MainAbility.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Ability from '@ohos.application.Ability' +import Logger from '../model/Logger'; + +export default class MainAbility extends Ability { + onCreate(want, launchParam) { + Logger.log("[Demo] MainAbility onCreate") + globalThis.abilityWant = want; + } + + onDestroy() { + Logger.log("[Demo] MainAbility onDestroy") + } + + onWindowStageCreate(windowStage) { + // Main window is created, set main page for this ability + Logger.log("[Demo] MainAbility onWindowStageCreate") + + windowStage.setUIContent(this.context, "pages/index", null) + } + + onWindowStageDestroy() { + // Main window is destroyed, release UI related resources + Logger.log("[Demo] MainAbility onWindowStageDestroy") + } + + onForeground() { + // Ability has brought to foreground + Logger.log("[Demo] MainAbility onForeground") + } + + onBackground() { + // Ability has back to background + Logger.log("[Demo] MainAbility onBackground") + } +}; diff --git a/FormExtAbility/entry/src/main/ets/model/Logger.ts b/FormExtAbility/entry/src/main/ets/model/Logger.ts new file mode 100644 index 0000000000000000000000000000000000000000..3a2384ae9b83b43f67f801941a89322fe30c021d --- /dev/null +++ b/FormExtAbility/entry/src/main/ets/model/Logger.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Logger { + constructor(private prefix: string) {} + public log(...args: any[]) { + console.log(`[${this.prefix}]: ${args.join('')}`) + } + public info(...args: any[]) { + console.info(`[${this.prefix}]: ${args.join('')}`) + } + public debug(...args: any[]) { + console.debug(`[${this.prefix}]: ${args.join('')}`) + } + public warn(...args: any[]) { + console.warn(`[${this.prefix}]: ${args.join('')}`) + } + public error(...args: any[]) { + console.error(`[${this.prefix}]: ${args.join('')}`) + } +} + +export default new Logger('eTSFormExtAbilityTag') diff --git a/FormExtAbility/entry/src/main/ets/pages/index.ets b/FormExtAbility/entry/src/main/ets/pages/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..c73d6e8074a0884802aec64d6998bf4275aa1a10 --- /dev/null +++ b/FormExtAbility/entry/src/main/ets/pages/index.ets @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import router from '@ohos.router'; +import Logger from '../model/Logger'; + +async function routePage() { + let options = { + url: 'pages/second' + } + try { + await router.push(options) + } catch (err) { + Logger.error(`fail callback, code: ${err.code}, msg: ${err.msg}`) + } +} + +@Entry +@Component +struct Index { + @State message: string = 'Hello FormExtAbility' + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(()=>{ + let want = { + "bundleName": "com.test.fts", + "abilityName": "MainAbility" + } + //AppStorage + + let context = globalThis.context + Logger.log("195 routerPage came in !!!") + + context.startAbility(want,err =>{ + Logger.log("routerPage err :" + JSON.stringify(err)) + }) + + }) + + + Button() { + Text('next page') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(() => { + routePage() + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/FormExtAbility/entry/src/main/ets/pages/second.ets b/FormExtAbility/entry/src/main/ets/pages/second.ets new file mode 100644 index 0000000000000000000000000000000000000000..b4db078c259f15f9ef327ed46503f5378e370e46 --- /dev/null +++ b/FormExtAbility/entry/src/main/ets/pages/second.ets @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import router from '@ohos.router'; + +@Entry +@Component +struct Second { + @State message: string = 'Second Page' + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('back to index') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(() => { + router.back() + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/FormExtAbility/entry/src/main/js/i18n/en-US.json b/FormExtAbility/entry/src/main/js/i18n/en-US.json new file mode 100644 index 0000000000000000000000000000000000000000..87bf765ee25396f182973db4f75f726b33097cd4 --- /dev/null +++ b/FormExtAbility/entry/src/main/js/i18n/en-US.json @@ -0,0 +1,7 @@ +{ + "strings": { + "temperature": "Current Temperature: ", + "time": "Last Update Time: " + } +} + diff --git a/FormExtAbility/entry/src/main/js/i18n/zh-CN.json b/FormExtAbility/entry/src/main/js/i18n/zh-CN.json new file mode 100644 index 0000000000000000000000000000000000000000..063384f02736cd95aa8a0acb1d7a7e336e779a85 --- /dev/null +++ b/FormExtAbility/entry/src/main/js/i18n/zh-CN.json @@ -0,0 +1,6 @@ +{ + "strings": { + "temperature": "当前温度: ", + "time": "更新时间: " + } +} \ No newline at end of file diff --git a/FormExtAbility/entry/src/main/js/widget/pages/index/index.css b/FormExtAbility/entry/src/main/js/widget/pages/index/index.css new file mode 100644 index 0000000000000000000000000000000000000000..e638640225a68216729884563e26011e9c3e337b --- /dev/null +++ b/FormExtAbility/entry/src/main/js/widget/pages/index/index.css @@ -0,0 +1,43 @@ +.container { + flex-direction: column; + justify-content: center; + align-items: center; +} + +.bg-img { + flex-shrink: 0; + height: 100%; +} + +.container-inner { + flex-direction: column; + justify-content: flex-end; + align-items: flex-start; + height: 100%; + width: 100%; + padding: 12px; + background-color: lightblue; +} + +.title { + font-size: 19px; + font-weight: bold; + color: black; + text-overflow: ellipsis; + max-lines: 1; + margin-bottom: 10%; +} + +.button{ + margin-bottom: 10%; + margin-top: 10%; +} + +.detail_text { + font-size: 16px; + color: white; + opacity: 0.66; + text-overflow: ellipsis; + max-lines: 1; + margin-top: 10%; +} \ No newline at end of file diff --git a/FormExtAbility/entry/src/main/js/widget/pages/index/index.hml b/FormExtAbility/entry/src/main/js/widget/pages/index/index.hml new file mode 100644 index 0000000000000000000000000000000000000000..ff179c3ce2bf6c71268b489e130ed53ce4275720 --- /dev/null +++ b/FormExtAbility/entry/src/main/js/widget/pages/index/index.hml @@ -0,0 +1,15 @@ +