diff --git a/BackGroundTasksKit/TransientTask/.gitignore b/BackGroundTasksKit/TransientTask/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/BackGroundTasksKit/TransientTask/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/AppScope/app.json5 b/BackGroundTasksKit/TransientTask/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5dec5cc9d24e6d369fd6129d9a259c8810fd6b02 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/AppScope/app.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 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. + */ + +{ + "app": { + "bundleName": "ohos.samples.transienttask", + "vendor": "samples", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:layered_image", + "label": "$string:app_name", + "distributedNotificationEnabled": true + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/AppScope/resources/base/element/string.json b/BackGroundTasksKit/TransientTask/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d61e5e468ac09b47fc8b9e592698d7032f0544e9 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "TransientTask" + } + ] +} diff --git a/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/app_icon.png b/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/app_icon.png differ diff --git a/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/background.png b/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/background.png differ diff --git a/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/foreground.png b/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/foreground.png differ diff --git a/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/layered_image.json b/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/BackGroundTasksKit/TransientTask/AppScope/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/README_zh.md b/BackGroundTasksKit/TransientTask/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..a2fde5133523f45f3b8262075b20160f799c281e --- /dev/null +++ b/BackGroundTasksKit/TransientTask/README_zh.md @@ -0,0 +1,68 @@ +# 短时任务 + +### 介绍 + +本示例主要展示后台任务中的短时任务。 + +通过[@ohos.resourceschedule.backgroundTaskManager](https://gitcode.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-backgroundtasks-kit/js-apis-resourceschedule-backgroundTaskManager.md),[Timer](https://gitcode.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/common/js-apis-timer.md) 等接口,实现应用退后台仍然可以计数的功能,展现短时任务的机制。 + +### 效果预览 + +|退后台前 |退后台一段时间在进前台 | +|---------------------------------------|-------------------------------------| +|![image](screenshots/device/before.png) |![image](screenshots/device/after.png)| + +使用说明 + +1.进入应用,点击申请短时任务按钮,点击开始计数按钮; + +2.将应用退出到后台后关闭; + +3.应用在后台一段时间(单次不超过3分钟、一天不超过10分钟)内,仍然可以进行计数; + +### 工程目录 +``` +entry/src/main/ets/ +|---Application +| |---MyAbilityStage.ts +|---pages +| |---Index.ets // 首页 +| |---Interval.ets // 定时计数 +| |---TitleBar.ets // 标题 +| |---TransientTaskDialog.ets // 短时任务 +|---util +| |---Logger.ts // 日志工具 +``` +### 具体实现 + +* 该示例使用requestSuspendDelay方法申请短时任务,延迟挂起,setInterval方法进行计数,实现应用退后台仍然可以计数的功能。 +* 源码链接:[Interval.ets](entry/src/main/ets/pages/Interval.ets),[TransientTaskDialog.ets](entry/src/main/ets/pages/TransientTaskDialog.ets) +* 接口参考:[@ohos.resourceschedule.backgroundTaskManager](https://gitcode.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-backgroundtasks-kit/js-apis-resourceschedule-backgroundTaskManager.md),[timer](https://gitcode.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/common/js-apis-timer.md) + +### 相关权限 + +不涉及 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行,支持设备:RK3568; + +2.本示例已适配API version 9版本SDK,版本号:3.2.11.9; + +3.本示例需要使用DevEco Studio NEXT Developer Preview2 (Build Version: 5.0.5.306, built on December 12, 2024)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: +``` +git init +git config core.sparsecheckout true +echo code/SystemFeature/TaskManagement/TransientTask/ > .git/info/sparse-checkout +git remote add origin https://gitcode.com/openharmony/applications_app_samples.git +git pull origin master + +``` \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/build-profile.json5 b/BackGroundTasksKit/TransientTask/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2d46b188fd3561818e55e6c76181831bc6972bf1 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/build-profile.json5 @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023-2025 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. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.4(16)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/code-linter.json5 b/BackGroundTasksKit/TransientTask/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ed05653cca31b61d64cf6471529eaf50d4f70709 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/code-linter.json5 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + "@security/no-unsafe-aes": "error", + "@security/no-unsafe-hash": "error", + "@security/no-unsafe-mac": "warn", + "@security/no-unsafe-dh": "error", + "@security/no-unsafe-dsa": "error", + "@security/no-unsafe-ecdsa": "error", + "@security/no-unsafe-rsa-encrypt": "error", + "@security/no-unsafe-rsa-sign": "error", + "@security/no-unsafe-rsa-key": "error", + "@security/no-unsafe-dsa-key": "error", + "@security/no-unsafe-dh-key": "error", + "@security/no-unsafe-3des": "error" + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/.gitignore b/BackGroundTasksKit/TransientTask/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/build-profile.json5 b/BackGroundTasksKit/TransientTask/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a0bbcffd8cec2d586c3140696ad48a7aff8ad13d --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-2025 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. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/hvigorfile.ts b/BackGroundTasksKit/TransientTask/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/BackGroundTasksKit/TransientTask/entry/obfuscation-rules.txt b/BackGroundTasksKit/TransientTask/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/oh-package.json5 b/BackGroundTasksKit/TransientTask/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0620e896a3c6321c4a50f0daf8a5ec82467c57b4 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023-2025 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. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/ets/Application/MyAbilityStage.ts b/BackGroundTasksKit/TransientTask/entry/src/main/ets/Application/MyAbilityStage.ts new file mode 100644 index 0000000000000000000000000000000000000000..ff4473e46c2204b1d8e117aab3f24aa5cb614772 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/ets/Application/MyAbilityStage.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 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 hilog from '@ohos.hilog'; +import AbilityStage from '@ohos.app.ability.AbilityStage'; + +export default class MyAbilityStage extends AbilityStage { + onCreate() { + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); + hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage onCreate'); + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/ets/entryability/EntryAbility.ets b/BackGroundTasksKit/TransientTask/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..48197b3343d524207abc2923e6d250e0348c0d91 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023-2025 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 { AbilityConstant, ConfigurationConstant, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import UIAbility from '@ohos.app.ability.UIAbility'; +import Window from '@ohos.window'; +import Logger from '../util/Logger'; +import { BusinessError } from '@kit.BasicServicesKit'; + +const DOMAIN = 0x0000; +const TAG: string = 'MainAbility'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + Logger.info(TAG, `onCreate`); + let status = want.parameters; + AppStorage.SetOrCreate('status', status); + AppStorage.SetOrCreate('filePath', this.context.filesDir); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy() { + Logger.info(TAG, `onDestroy`); + } + + onWindowStageCreate(windowStage: Window.WindowStage) { + // Main window is created, set main page for this ability + Logger.info(TAG, `onWindowStageCreate`); + windowStage.loadContent('pages/Index', (err, data) => { + if (err.code) { + Logger.info(TAG, `Failed to load the content. Cause: ${JSON.stringify(err)}`); + return; + } + Logger.info(TAG, `Succeeded in loading the content. Data: ${JSON.stringify(data)}`); + }) + } + + onWindowStageDestroy() { + // Main window is destroyed, release UI related resources + Logger.info(TAG, `onWindowStageDestroy`); + } + + onForeground() { + // Ability has brought to foreground + Logger.info(TAG, `onForeground`); + } + + onBackground(): void { + // Ability has back to background + Logger.info(TAG, `onBackground`); + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/BackGroundTasksKit/TransientTask/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..1fca37ecc34e1079aaf6557f1272cd694aa0068c --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/Index.ets b/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..0bf20dfa242142ead69cb8c9402e0ffeaca830ed --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023-2025 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 { TitleBar } from './TitleBar'; +import { TransientTaskDialog } from './TransientTaskDialog'; +import { Interval } from './Interval'; + +@Entry +@Component +struct Index { + build() { + Column() { + TitleBar(); + Image($r('app.media.Tomato')) + .width(250) + .height(250) + .borderRadius(10) + Interval(); + TransientTaskDialog(); + } + .width('100%') + .height('100%') + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/Interval.ets b/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/Interval.ets new file mode 100644 index 0000000000000000000000000000000000000000..2e9556a94e8f1027dfed1b83b893a6655e76feee --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/Interval.ets @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 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. + */ + +@Component +export struct Interval { + @State timer: number = 0; + + build() { + Column() { + Row(){ + Text($r('app.string.interval_number')).fontSize(20); + Text(this.timer.toString()).fontSize(20).id('interval_number'); + } + Button($r('app.string.counter')) + .id('counter') + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + .type(ButtonType.Capsule) + .backgroundColor($r('app.color.background')) + .size({ width: '70%', height: '8%' }) + .onClick(() => { + setInterval(() => { + this.timer += 1; + console.info('text now is '+ this.timer); + }, 1000) + }) + } + .width('100%') + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/TitleBar.ets b/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/TitleBar.ets new file mode 100644 index 0000000000000000000000000000000000000000..86111d1bb346b811c737c6f543231e2eb8c2345d --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/TitleBar.ets @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023-2025 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. + */ + +@Component +export struct TitleBar { + build() { + Row() { + Text($r("app.string.MainAbility_label")) + .fontSize(25) + .layoutWeight(1) + .fontColor(Color.White) + .fontWeight(FontWeight.Bold) + .textAlign(TextAlign.Center) + } + .width('100%') + .height('8%') + .backgroundColor($r('app.color.background')) + .constraintSize({ minHeight: 50 }) + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/TransientTaskDialog.ets b/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/TransientTaskDialog.ets new file mode 100644 index 0000000000000000000000000000000000000000..f4fd0c6c4ab566eb44cb6bcf47d32c701f316044 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/ets/pages/TransientTaskDialog.ets @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025 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 { backgroundTaskManager } from '@kit.BackgroundTasksKit'; +import { BusinessError } from '@kit.BasicServicesKit'; + +@Component +export struct TransientTaskDialog { + // [Start request_suspend_delay] + @State requestId : number = 0; // 申请短时任务ID + @State delayTime : number = 0; // 本次申请短时任务的剩余时间 + + requestSuspendDelay() { + let myReason = 'test requestSuspendDelay'; // 申请原因 + try { + let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => { + // 回调函数。应用申请的短时任务即将超时,通过此函数回调应用,执行一些清理和标注工作,并取消短时任务 + console.info('suspend delay task will timeout'); + }) + this.requestId = delayInfo.requestId; + this.delayTime = delayInfo.actualDelayTime; + console.info(`Succeeded in requestSuspendDelay. requestId is :${this.requestId}, delayTime is :${this.delayTime}`); + } catch (error) { + console.error(`Operation requestSuspendDelay failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + } + } + // [End request_suspend_delay] + + // [Start get_time] + getRemainingDelayTime() { + try { + backgroundTaskManager.getRemainingDelayTime(this.requestId).then((res: number) => { + console.info(`Succeeded in getting remaining delay time. delay time is :${res}`); + }).catch((err: BusinessError) => { + console.error(`Failed to get remaining delay time. Code: ${err.code}, message: ${err.message}`); + }) + } catch (error) { + console.error(`Operation getRemainingDelayTime failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + } + } + // [End get_time] + + // [Start cancel_suspend_delay] + cancelSuspendDelay() { + try { + backgroundTaskManager.cancelSuspendDelay(this.requestId); + console.info('Succeeded in cancelSuspendDelay'); + } catch (error) { + console.error(`Operation cancelSuspendDelay failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + } + } + // [End cancel_suspend_delay] + + build() { + Column() { + Row(){ + Text($r('app.string.transient_task_id')).fontSize(20); + Text(this.requestId.toString()).fontSize(20).id('transient_task_id'); + } + Button($r('app.string.request_suspend_delay')) + .id('request_suspend_delay') + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + .type(ButtonType.Capsule) + .backgroundColor($r('app.color.background')) + .size({ width: '70%', height: '8%' }) + .onClick(() => { + this.requestSuspendDelay(); + }) + Button($r('app.string.get_remaining_delay_time')) + .id('get_remaining_delay_time') + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + .type(ButtonType.Capsule) + .backgroundColor($r('app.color.background')) + .size({ width: '70%', height: '8%' }) + .onClick(() => { + this.getRemainingDelayTime(); + }) + Button($r('app.string.cancel_suspend_delay')) + .id('cancel_suspend_delay') + .fontSize(20) + .margin({ top: 10, bottom: 10 }) + .type(ButtonType.Capsule) + .backgroundColor($r('app.color.background')) + .size({ width: '70%', height: '8%' }) + .onClick(() => { + this.cancelSuspendDelay(); + }) + }.width('100%') + .margin({ top: 5 }) + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/ets/util/Logger.ts b/BackGroundTasksKit/TransientTask/entry/src/main/ets/util/Logger.ts new file mode 100644 index 0000000000000000000000000000000000000000..0483a8a21b28b2d14b5be1197de79ba1e5cddcc9 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/ets/util/Logger.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 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 hilog from '@ohos.hilog'; + +class Logger { + private domain: number; + private prefix: string; + private format: string = '%{public}s, %{public}s'; + + constructor(prefix: string) { + this.prefix = prefix; + this.domain = 0xFF00; + } + + debug(...args: string[]) { + hilog.debug(this.domain, this.prefix, this.format, args); + } + + info(...args: string[]) { + hilog.info(this.domain, this.prefix, this.format, args); + } + + warn(...args: string[]) { + hilog.warn(this.domain, this.prefix, this.format, args); + } + + error(...args: string[]) { + hilog.error(this.domain, this.prefix, this.format, args); + } +} + +export default new Logger('[Sample_TransientTask]'); \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/module.json5 b/BackGroundTasksKit/TransientTask/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b6f35841523a07dce5d7501d8c412e6cf9aaee6e --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/module.json5 @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023-2025 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. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "srcEntrance": "./ets/Application/MyAbilityStage.ts", + "description": "$string:entry_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:MainAbility_desc", + "icon": "$media:layered_image", + "label": "$string:MainAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ] + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/color.json b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..1d6744b0109022b8569062597c7bfee14572d16d --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/color.json @@ -0,0 +1,12 @@ +{ + "color": [ + { + "name": "white", + "value": "#FFFFFF" + }, + { + "name": "background", + "value": "#0D9FFB" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/float.json b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..5f0600c7083c3ac3776dcc13ce3e3f03c378fc6d --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/string.json b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d5291a99169292a95e1c0189d738d6b22ab0efbb --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/element/string.json @@ -0,0 +1,84 @@ +{ + "string": [ + { + "name": "entry_desc", + "value": "短时任务管理" + }, + { + "name": "MainAbility_desc", + "value": "短时任务管理" + }, + { + "name": "MainAbility_label", + "value": "短时任务" + }, + { + "name": "install_bundle", + "value": "快速修复的补丁安装接口权限" + }, + { + "name": "get_bundle_info", + "value": "获取应用的快速修复信息权限" + }, + { + "name": "internet", + "value": "网络下载权限" + }, + { + "name": "short_task", + "value": "短时任务" + }, + { + "name": "check", + "value": "检查更新" + }, + { + "name": "message", + "value": "最新版本为3.2.8.3,是否更新" + }, + { + "name": "cancel", + "value": "取消" + }, + { + "name": "update", + "value": "更新" + }, + { + "name": "update_message", + "value": "当前版本为3.1.7.5" + }, + { + "name": "update_after", + "value": "当前版本为3.2.8.3" + }, + { + "name": "update_software", + "value": "软件更新" + }, + { + "name": "request_suspend_delay", + "value": "申请短时任务" + }, + { + "name": "get_remaining_delay_time", + "value": "获取短时任务剩余时间" + }, + { + "name": "cancel_suspend_delay", + "value": "取消短时任务" + }, + { + "name": "transient_task_id", + "value": "短时任务Id:" + }, + { + "name": "interval_number", + "value": "当前计数为:" + }, + { + "name": "counter", + "value": "开始计数" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/Tomato.png b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/Tomato.png new file mode 100644 index 0000000000000000000000000000000000000000..93ad983d4862485684c34cfcf1632569a13ae5a1 Binary files /dev/null and b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/Tomato.png differ diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/background.png b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/background.png differ diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/foreground.png b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/foreground.png differ diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/icon.png b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/icon.png differ diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/layered_image.json b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/startIcon.png b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/media/startIcon.png differ diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/profile/backup_config.json b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..d742c2f96e7dd0f406f499941f3147345e998f95 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/profile/main_pages.json b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..55c3f007f87b7ce5206d325f968cc56f2f79441f --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/main/resources/dark/element/color.json b/BackGroundTasksKit/TransientTask/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..438d5bc43bb23c59c210d586b96635a72da5b64a --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/mock/mock-config.json5 b/BackGroundTasksKit/TransientTask/entry/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b9a78e201535765168a92d3543c690273ecdc019 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/mock/mock-config.json5 @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2025 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. + */ + +{ +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/TestRunner/OpenHarmonyTestRunner.ts b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/TestRunner/OpenHarmonyTestRunner.ts new file mode 100644 index 0000000000000000000000000000000000000000..51f343b4ddef85963c1c759af4852211e163c0b1 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/TestRunner/OpenHarmonyTestRunner.ts @@ -0,0 +1,62 @@ +/* +* Copyright (C) 2023 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 hilog from '@ohos.hilog'; +import TestRunner from '@ohos.application.testRunner'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; + +let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator | undefined = undefined; +let abilityDelegatorArguments: AbilityDelegatorRegistry.AbilityDelegatorArgs | undefined = undefined; + +async function onAbilityCreateCallback(): Promise { + hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); +} + +async function addAbilityMonitorCallback(err: any): Promise { + hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); +} + +export default class OpenHarmonyTestRunner implements TestRunner { + constructor() { + } + + onPrepare(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare '); + } + + async onRun(): Promise { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments(); + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + let testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'; + let lMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback); + let cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName; + let debug = abilityDelegatorArguments.parameters['-D']; + if (debug === 'true') { + cmd += ' -D'; + } + hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd); + abilityDelegator.executeShellCommand(cmd, + (err: any, d: any) => { + hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? ''); + }); + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/test/Ability.test.ets b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..5cd9e11d48d9c7c0468cc73b87e6ad6e7439ff4f --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2023-2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; +import { Driver, ON, Component } from '@ohos.UiTest'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import { Want } from '@kit.AbilityKit'; +import Logger from '../../../main/ets/util/Logger'; +import { resourceManager } from '@kit.LocalizationKit'; + +const BUNDLE = 'TransientTaskTest_'; +const TAG = '[Sample_TransientTask]'; +const DOMAIN = 0xF811; + +export default function abilityTest() { + let delegator = AbilityDelegatorRegistry.getAbilityDelegator(); + let driver: Driver = Driver.create(); + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + hilog.info(DOMAIN, TAG, BUNDLE + 'beforeAll start'); + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + hilog.info(DOMAIN, TAG, BUNDLE + 'afterEach start'); + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + hilog.info(DOMAIN, TAG, BUNDLE + 'afterAll start'); + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + + /** + * 拉起应用 com.samples.transienttask + */ + it(BUNDLE + 'StartAbility_001', 0, async (done: Function) => { + Logger.info(TAG, `${BUNDLE}CameraAbility_StartAbility_001 start`); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'ohos.samples.transienttask', + abilityName: 'EntryAbility' + }) + expect(true).assertTrue(); + } catch (exception) { + Logger.info(TAG, `${BUNDLE}Failed to start ability, error info: ${JSON.stringify(exception)}`); + expect().assertFail(); + } + await driver.delayMs(1000); + Logger.info(TAG, `${BUNDLE}StartAbility_001 end`); + done(); + }) + + /** + * 判断起应用后,判断控件是否存在 + */ + it(BUNDLE + 'ComponentIfExist_001', 0, async (done: Function) => { + Logger.info(TAG, `${BUNDLE}ComponentIfExist_001 start`); + await driver.delayMs(1000); + // 测试控件是否存在 + await driver.assertComponentExist(ON.id('interval_number')); + await driver.assertComponentExist(ON.id('counter')); + await driver.assertComponentExist(ON.id('transient_task_id')); + await driver.assertComponentExist(ON.id('request_suspend_delay')); + await driver.assertComponentExist(ON.id('get_remaining_delay_time')); + await driver.assertComponentExist(ON.id('cancel_suspend_delay')); + expect(true).assertTrue(); + Logger.info(TAG, `${BUNDLE}ComponentIfExist_001 end`); + done(); + }) + + /** + * 测试控件点击, 判断短时任务是否申请成功 + */ + it(BUNDLE + 'requestSuspendDelay_003', 0, async (done: Function) => { + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_003 start`); + await driver.delayMs(1000); + let taskIdAfter = ''; + let requestBtn = await driver.findComponent(ON.id('request_suspend_delay')); + let taskId = await driver.findComponent(ON.id('transient_task_id')); + await requestBtn.click(); + await driver.delayMs(1000); + await taskId.getText().then(value => { + taskIdAfter = value; + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_003 after requestBtn: ` + taskIdAfter); + }) + if (taskIdAfter === '0') { + expect(false).assertTrue(); + } + expect(true).assertTrue(); + await driver.delayMs(1000); + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_003 end`); + done(); + }) + + /** + * 测试控件点击, 判断短时任务是否最多申请三次 + */ + it(BUNDLE + 'requestSuspendDelay_004', 0, async (done: Function) => { + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_004 start`); + // 取消短时任务 + let cancelBtn = await driver.findComponent(ON.id('cancel_suspend_delay')); + await cancelBtn.click(); + await driver.delayMs(1000); + let taskIdBefore = ''; + let taskIdAfter = ''; + let taskId = await driver.findComponent(ON.id('transient_task_id')); + await taskId.getText().then(value => { + taskIdBefore = value; + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_004 before requestBtn: ` + taskIdBefore); + }) + // 申请短时任务 + let requestBtn = await driver.findComponent(ON.id('request_suspend_delay')); + for (let i = 0; i < 4; i++) { + await requestBtn.click(); + await driver.delayMs(1000); + } + await taskId.getText().then(value => { + taskIdAfter = value; + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_004 after requestBtn: ` + taskIdAfter); + }) + await driver.delayMs(1000); + // 判断短时任务是否最多可申请3次 + taskIdBefore = (parseInt(taskIdBefore, 10) + 3).toString(); + if (taskIdAfter != taskIdBefore) { + expect(false).assertTrue(); + } + expect(true).assertTrue(); + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_004 end`); + done(); + }) + + /** + * 测试控件点击, 获取短时任务剩余时间 + */ + it(BUNDLE + 'requestSuspendDelay_005', 0, async (done: Function) => { + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_005 start`); + // 取消短时任务 + let cancelBtn = await driver.findComponent(ON.id('cancel_suspend_delay')); + await cancelBtn.click(); + await driver.delayMs(1000); + // 申请短时任务 + let requestBtn = await driver.findComponent(ON.id('request_suspend_delay')); + await requestBtn.click(); + await driver.delayMs(1000); + // 获取短时任务剩余时间 + let getTimeBtn = await driver.findComponent(ON.id('get_remaining_delay_time')); + await getTimeBtn.click(); + await driver.delayMs(1000); + expect(true).assertTrue(); + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_005 end`); + done(); + }) + + /** + * 测试控件点击, 取消短时任务 + */ + it(BUNDLE + 'requestSuspendDelay_006', 0, async (done: Function) => { + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_006 start`); + // 取消短时任务 + let cancelBtn = await driver.findComponent(ON.id('cancel_suspend_delay')); + await cancelBtn.click(); + await driver.delayMs(1000); + expect(true).assertTrue(); + Logger.info(TAG, `${BUNDLE}requestSuspendDelay_006 end`); + done(); + }) + }) +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/test/List.test.ets b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..657d194164f932396390ddb56c59cbd65c7d1259 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023 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 abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/testability/TestAbility.ets b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/testability/TestAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..2fbabce864749640c9ea72e138ed474476a4bc1e --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/testability/TestAbility.ets @@ -0,0 +1,62 @@ +/* +* Copyright (C) 2023 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 UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import hilog from '@ohos.hilog'; +import { Hypium } from '@ohos/hypium'; +import testsuite from '../test/List.test'; +import window from '@ohos.window'; +import Want from '@ohos.app.ability.Want'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + +export default class TestAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); + hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? ''); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + let abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments(); + hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate'); + windowStage.loadContent('testability/pages/Index', (err, data) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', + JSON.stringify(data) ?? ''); + }); + } + + onWindowStageDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy'); + } + + onForeground(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground'); + } + + onBackground(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground'); + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/testability/pages/Index.ets b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/testability/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..bc6b6a57c4013cc9ceb421eeee170bc2bc021ff4 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/ets/testability/pages/Index.ets @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 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 hilog from '@ohos.hilog'; + +@Entry +@Component +struct Index { + aboutToAppear() { + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear'); + } + + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('next page') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(() => { + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/module.json5 b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9c7e6edf1a3a1654b7edceb56d16082260b2cda6 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/module.json5 @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023-2025 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. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "description": "$string:entry_test_desc", + "mainElement": "TestAbility", + "deviceTypes": [ + "default", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:test_pages", + "abilities": [ + { + "name": "TestAbility", + "srcEntry": "./ets/testability/TestAbility.ets", + "description": "$string:TestAbility_desc", + "icon": "$media:icon", + "label": "$string:TestAbility_label", + "visible": true, + "startWindowIcon": "$media:icon", + "startWindowBackground": "$color:white", + "skills": [ + { + "actions": [ + "action.system.home" + ], + "entities": [ + "entity.system.home" + ] + } + ], + } + ] + } +} diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/element/color.json b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..1bbc9aa9617e97c45440e1d3d66afc1154837012 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "white", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/element/string.json b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c7c657b09f1d31a9193c8fcb1a4839a30c640b85 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/element/string.json @@ -0,0 +1,32 @@ +{ + "string": [ + { + "name": "entry_test_desc", + "value": "test ability description" + }, + { + "name": "TestAbility_desc", + "value": "the test ability" + }, + { + "name": "TestAbility_label", + "value": "test label" + }, + { + "name": "update_message", + "value": "当前版本为3.1.7.5" + }, + { + "name": "update", + "value": "更新" + }, + { + "name": "update_after", + "value": "当前版本为3.2.8.3" + }, + { + "name": "short_task", + "value": "短时任务" + } + ] +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/media/icon.png b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/media/icon.png differ diff --git a/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/profile/test_pages.json b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/profile/test_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..b7e7343cacb32ce982a45e76daad86e435e054fe --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/ohosTest/resources/base/profile/test_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "testability/pages/Index" + ] +} diff --git a/BackGroundTasksKit/TransientTask/entry/src/test/List.test.ets b/BackGroundTasksKit/TransientTask/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 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 localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/entry/src/test/LocalUnit.test.ets b/BackGroundTasksKit/TransientTask/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/BackGroundTasksKit/TransientTask/hvigor/hvigor-config.json5 b/BackGroundTasksKit/TransientTask/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fe698abcb8e9c3085683db0c1be2308bb1a64633 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.4", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/BackGroundTasksKit/TransientTask/hvigorfile.ts b/BackGroundTasksKit/TransientTask/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/BackGroundTasksKit/TransientTask/hvigorw b/BackGroundTasksKit/TransientTask/hvigorw new file mode 100644 index 0000000000000000000000000000000000000000..39f66a85d4d17cf6b0cda498ca3effbb65485958 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/hvigorw @@ -0,0 +1,61 @@ +#!/bin/bash + +# Copyright (c) 2023 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. + +# ---------------------------------------------------------------------------- +# Hvigor startup script, version 1.0.0 +# +# Required ENV vars: +# ------------------ +# NODE_HOME - location of a Node home dir +# or +# Add /usr/local/nodejs/bin to the PATH environment variable +# ---------------------------------------------------------------------------- + +HVIGOR_APP_HOME=$(dirname $(readlink -f $0)) +HVIGOR_WRAPPER_SCRIPT=${HVIGOR_APP_HOME}/hvigor/hvigor-wrapper.js +warn() { + echo "" + echo -e "\033[1;33m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m" +} + +error() { + echo "" + echo -e "\033[1;31m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m" +} + +fail() { + error "$@" + exit 1 +} + +# Determine node to start hvigor wrapper script +if [ -n "${NODE_HOME}" ];then + EXECUTABLE_NODE="${NODE_HOME}/bin/node" + if [ ! -x "$EXECUTABLE_NODE" ];then + fail "ERROR: NODE_HOME is set to an invalid directory,check $NODE_HOME\n\nPlease set NODE_HOME in your environment to the location where your nodejs installed" + fi +else + EXECUTABLE_NODE="node" + which ${EXECUTABLE_NODE} > /dev/null 2>&1 || fail "ERROR: NODE_HOME is not set and not 'node' command found in your path" +fi + +# Check hvigor wrapper script +if [ ! -r "$HVIGOR_WRAPPER_SCRIPT" ];then + fail "ERROR: Couldn't find hvigor/hvigor-wrapper.js in ${HVIGOR_APP_HOME}" +fi + +# start hvigor-wrapper script +exec "${EXECUTABLE_NODE}" \ + "${HVIGOR_WRAPPER_SCRIPT}" "$@" diff --git a/BackGroundTasksKit/TransientTask/hvigorw.bat b/BackGroundTasksKit/TransientTask/hvigorw.bat new file mode 100644 index 0000000000000000000000000000000000000000..af893e9a2c9170d04610c906f1cf1b6f3ac144ac --- /dev/null +++ b/BackGroundTasksKit/TransientTask/hvigorw.bat @@ -0,0 +1,72 @@ +:: Copyright (c) 2023 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. + +@echo off + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Hvigor startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +set WRAPPER_MODULE_PATH=%APP_HOME%\hvigor\hvigor-wrapper.js +set NODE_EXE=node.exe + +goto start + +:start +@rem Find node.exe +if defined NODE_HOME goto findNodeFromNodeHome + +%NODE_EXE% --version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: NODE_HOME is not set and no 'node' command could be found in your PATH. +echo. +echo Please set the NODE_HOME variable in your environment to match the +echo location of your NodeJs installation. + +goto fail + +:findNodeFromNodeHome +set NODE_HOME=%NODE_HOME:"=% +set NODE_EXE_PATH=%NODE_HOME%/%NODE_EXE% + +if exist "%NODE_EXE_PATH%" goto execute +echo. +echo ERROR: NODE_HOME is not set and no 'node' command could be found in your PATH. +echo. +echo Please set the NODE_HOME variable in your environment to match the +echo location of your NodeJs installation. + +goto fail + +:execute +@rem Execute hvigor +"%NODE_EXE%" %WRAPPER_MODULE_PATH% %* + +:fail +exit /b 1 diff --git a/BackGroundTasksKit/TransientTask/oh-package.json5 b/BackGroundTasksKit/TransientTask/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..077520425b4fe9aeba3dac13c709415bbcc312b6 --- /dev/null +++ b/BackGroundTasksKit/TransientTask/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.4", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.21", + "@ohos/hamock": "1.0.0" + } +} diff --git a/BackGroundTasksKit/TransientTask/screenshots/device/after.png b/BackGroundTasksKit/TransientTask/screenshots/device/after.png new file mode 100644 index 0000000000000000000000000000000000000000..45e787da5162173d3bf6e74e940e826a140ab314 Binary files /dev/null and b/BackGroundTasksKit/TransientTask/screenshots/device/after.png differ diff --git a/BackGroundTasksKit/TransientTask/screenshots/device/before.png b/BackGroundTasksKit/TransientTask/screenshots/device/before.png new file mode 100644 index 0000000000000000000000000000000000000000..26b822615afb1d5a76f181f94b4a6c8410aff56c Binary files /dev/null and b/BackGroundTasksKit/TransientTask/screenshots/device/before.png differ