@hxa-rn/react-native-background-actions 0.0.0-stage → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +177 -2
- package/harmony/background_actions/LICENSE +22 -0
- package/harmony/background_actions/NOTICE +33 -0
- package/harmony/background_actions/OAT.xml +38 -0
- package/harmony/background_actions/build-profile.json5 +16 -0
- package/harmony/background_actions/hvigorfile.ts +2 -0
- package/harmony/background_actions/index.ets +1 -0
- package/harmony/background_actions/oh-package.json5 +14 -0
- package/harmony/background_actions/src/main/cpp/BackgroundActionsPackage.h +26 -0
- package/harmony/background_actions/src/main/cpp/BackgroundActionsTimerModule.cpp +54 -0
- package/harmony/background_actions/src/main/cpp/BackgroundActionsTimerModule.h +35 -0
- package/harmony/background_actions/src/main/cpp/CMakeLists.txt +3 -0
- package/harmony/background_actions/src/main/cpp/bridge/CMakeLists.txt +8 -0
- package/harmony/background_actions/src/main/cpp/generated/RNBackgroundActions.cpp +30 -0
- package/harmony/background_actions/src/main/cpp/generated/RNBackgroundActions.h +21 -0
- package/harmony/background_actions/src/main/cpp/generated/RNOHGeneratedPackage.h +76 -0
- package/harmony/background_actions/src/main/ets/BackgroundActionsModule.ets +425 -0
- package/harmony/background_actions/src/main/ets/BackgroundActionsTurboModulesFactory.ets +9 -0
- package/harmony/background_actions/src/main/ets/generated/components/ts.ts +5 -0
- package/harmony/background_actions/src/main/ets/generated/index.ets +8 -0
- package/harmony/background_actions/src/main/ets/generated/ts.ts +9 -0
- package/harmony/background_actions/src/main/ets/generated/turboModules/RNBackgroundActions.ts +65 -0
- package/harmony/background_actions/src/main/ets/generated/turboModules/ts.ts +8 -0
- package/harmony/background_actions/src/main/module.json5 +13 -0
- package/harmony/background_actions/src/main/resources/base/element/string.json +8 -0
- package/harmony/background_actions/src/main/resources/en_US/element/string.json +8 -0
- package/harmony/background_actions/src/main/resources/zh_CN/element/string.json +8 -0
- package/harmony/background_actions.har +0 -0
- package/package.json +82 -4
- package/src/BackgroundTimers.ts +72 -0
- package/src/RNBackgroundActionsModule.ts +78 -0
- package/src/index.ts +131 -0
- package/src/specs/v1/.gitkeep +1 -0
- package/src/specs/v1/NativeRNBackgroundActionsSpec.ts +27 -0
- package/src/specs/v2/.gitkeep +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Rapsssito
|
|
4
|
+
Modifications Copyright (c) 2026 HarmonyOS X-platform Alliance
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,178 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-native-background-actions
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
本项目基于 [react-native-background-actions](https://github.com/Rapsssito/react-native-background-actions) 开发。如果在使用过程中有任何问题,欢迎在AtomGit提交Issue,会及时跟进。
|
|
4
|
+
|
|
5
|
+
## 项目介绍
|
|
6
|
+
|
|
7
|
+
`@hxa-rn/react-native-background-actions` 是 `react-native-background-actions` 的鸿蒙(OpenHarmony)适配版本,当前规范版本号为 **4.1.0**。通过 ArkTS TurboModule 提供有限后台时间申请、业务启停、状态查询、通知更新和事件接口。
|
|
8
|
+
|
|
9
|
+
| 鸿蒙适配包版本 | 原始库版本 | 支持 RN 版本 | Autolink | 编译 API 版本 |
|
|
10
|
+
| --- | --- | --- | --- | --- |
|
|
11
|
+
| 4.1.0 | react-native-background-actions 4.1.0 | 0.72+ | 是 | API 13+ |
|
|
12
|
+
|
|
13
|
+
## 集成指南
|
|
14
|
+
|
|
15
|
+
安装鸿蒙适配包:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @hxa-rn/react-native-background-actions
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
配套基线为 React 18.2.0、React Native 0.72.5 和 `@react-native-oh/react-native-harmony` 0.72.139;包的 `peerDependencies.react-native` 为 `>=0.72`。通过 RNOH autolinking 注册自有 HAR。`harmony.alias` 为 `react-native-background-actions`,配置后业务代码继续使用原库导入名。
|
|
22
|
+
|
|
23
|
+
宿主需声明 `ohos.permission.KEEP_BACKGROUND_RUNNING`,网络业务还需 `ohos.permission.INTERNET`。长时业务应声明真实使用的 `backgroundModes`;仅声明模式不代替播放、传输、录音等真实业务或所需授权。通知授权由系统确认。
|
|
24
|
+
|
|
25
|
+
## 使用说明
|
|
26
|
+
|
|
27
|
+
宿主提供 `startIcon` 媒体资源;本仓库 Example 已包含该资源。以下函数先申请有限短时后台时间,再更新任务通知。任务自然完成会释放资源;用户结束或组件卸载时调用 `stopExample()`,不将启动确认视为业务完成。
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import BackgroundActions from 'react-native-background-actions';
|
|
31
|
+
|
|
32
|
+
export async function startExample(): Promise<void> {
|
|
33
|
+
await BackgroundActions.start(async (parameters?: {steps: number}) => {
|
|
34
|
+
for (let step = 0; step < (parameters?.steps ?? 5); step++) {
|
|
35
|
+
await new Promise<void>(resolve => setTimeout(resolve, 1000));
|
|
36
|
+
if (!BackgroundActions.isRunning()) break;
|
|
37
|
+
}
|
|
38
|
+
}, {
|
|
39
|
+
taskName: 'FiniteWork', taskTitle: '有限后台工作', taskDesc: '等待执行',
|
|
40
|
+
taskIcon: {name: 'startIcon', type: 'media'},
|
|
41
|
+
foregroundServiceType: ['shortService'], parameters: {steps: 5}
|
|
42
|
+
});
|
|
43
|
+
await BackgroundActions.updateNotification({taskDesc: '正在执行'});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function stopExample(): Promise<void> {
|
|
47
|
+
await BackgroundActions.stop();
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
调用方应对上述异步函数使用 `try/catch` 展示真实错误。任务执行后的异常通过 `taskError` 事件提供 `code/message/generation`;根 `expiration` 回调无参数,来自真实系统到期。手动 `emit` 只能验证 JS 派发。
|
|
52
|
+
|
|
53
|
+
## 接口文档
|
|
54
|
+
|
|
55
|
+
默认单例公开4个业务方法及10个 EventEmitter3 方法。深入口 `@hxa-rn/react-native-background-actions/src/RNBackgroundActionsModule` 导出 `RNBackgroundActions` 和 `nativeEventEmitter`。公开可调用接口共32个;内部后台计时桥 `scheduleTimer/cancelTimer` 不作为业务接口。方法签名以当前 TypeScript 声明为准。
|
|
56
|
+
|
|
57
|
+
表中“完全支持(示例用例已实机验证)”表示接口已实现,且对应 `example_auto` 场景已在测试设备上手动验证;它不代表系统对授权、时长或配额提供无限制保证。只有存在平台能力差异的接口才标为“部分支持”,所需的授权或运行条件另行注明。
|
|
58
|
+
|
|
59
|
+
| API | 描述 | 参数 | 返回值 | HarmonyOS 支持 |
|
|
60
|
+
| --- | --- | --- | --- | --- |
|
|
61
|
+
| start | 执行 task(parameters) | task, options | Promise<void> | ⚠️ 部分支持(后台运行时长和配额受 HarmonyOS 限制;示例用例已实机验证) |
|
|
62
|
+
| stop | 释放任务及通知 | 无 | Promise<void> | ✅ 完全支持(示例用例已实机验证;需先成功启动任务) |
|
|
63
|
+
| isRunning | 读取原生活动状态 | 无 | boolean | ✅ 完全支持(读取 HarmonyOS 原生任务状态;示例用例已实机验证) |
|
|
64
|
+
| updateNotification | 更新同 ID 通知 | patch | Promise<void> | ✅ 完全支持(示例用例已实机验证;受系统通知授权及内容规则约束) |
|
|
65
|
+
| on | 登记监听 | event, listener, context? | 单例 | ✅ 完全支持(纯 JS) |
|
|
66
|
+
| addListener | 登记监听 | 同 on | 单例 | ✅ 完全支持(纯 JS) |
|
|
67
|
+
| once | 一次性监听 | 同 on | 单例 | ✅ 完全支持(纯 JS) |
|
|
68
|
+
| emit | 同步派发 JS 事件 | event, ...args | boolean | ✅ 完全支持(纯 JS) |
|
|
69
|
+
| off | 移除监听 | event, listener?, context?, once? | 单例 | ✅ 完全支持(纯 JS) |
|
|
70
|
+
| removeListener | 移除监听 | 同 off | 单例 | ✅ 完全支持(纯 JS) |
|
|
71
|
+
| removeAllListeners | 移除指定或全部监听 | event? | 单例 | ✅ 完全支持(纯 JS) |
|
|
72
|
+
| eventNames | 列出事件名称 | 无 | Array<string或symbol> | ✅ 完全支持(纯 JS) |
|
|
73
|
+
| listeners | 查询监听函数 | event | Function[] | ✅ 完全支持(纯 JS) |
|
|
74
|
+
| listenerCount | 查询监听数量 | event | number | ✅ 完全支持(纯 JS) |
|
|
75
|
+
| RNBackgroundActions.start | 原生后台申请,不执行 JS 函数 | NativeOptions | Promise<void> | ⚠️ 部分支持(后台运行时长和配额受 HarmonyOS 限制;示例用例已实机验证) |
|
|
76
|
+
| RNBackgroundActions.stop | 取消原生任务 | 无 | Promise<void> | ✅ 完全支持(示例用例已实机验证;需先成功启动任务) |
|
|
77
|
+
| RNBackgroundActions.updateNotification | 完整配置更新 | NativeOptions | Promise<void> | ✅ 完全支持(示例用例已实机验证;受系统通知授权及内容规则约束) |
|
|
78
|
+
| RNBackgroundActions.addListener | 登记 expiration 桥接监听 | eventName | void | ✅ 完全支持(示例用例已实机验证;到期事件由系统实际到期触发) |
|
|
79
|
+
| RNBackgroundActions.removeListeners | 减少桥接监听计数 | count | void | ✅ 完全支持(示例用例已实机验证) |
|
|
80
|
+
| RNBackgroundActions.isRunning | 原生活动状态 | 无 | boolean | ✅ 完全支持(读取 HarmonyOS 原生任务状态;示例用例已实机验证) |
|
|
81
|
+
| RNBackgroundActions.getStatus | 实际句柄/模式/错误状态 | 无 | Promise<TaskStatus> | ✅ 完全支持(示例用例已实机验证) |
|
|
82
|
+
| RNBackgroundActions.requestNotifications | 系统通知授权 | 无 | Promise<boolean> | ✅ 完全支持(示例用例已实机验证;返回实际系统授权状态) |
|
|
83
|
+
| RNBackgroundActions.scheduleTimer | 内部后台计时桥 | id, delay, repeats | void | ⚠️ 部分支持(后台计时受短时任务有效期限制;示例用例已实机验证) |
|
|
84
|
+
| RNBackgroundActions.cancelTimer | 取消内部计时句柄 | id | void | ✅ 完全支持(示例用例已实机验证) |
|
|
85
|
+
| nativeEventEmitter.addListener | 实际原生事件订阅 | eventType, listener, context? | Subscription | ✅ 完全支持(示例用例已实机验证;事件触发受对应系统事件约束) |
|
|
86
|
+
| nativeEventEmitter.once | 一次性实际原生订阅 | 同 addListener | Subscription | ✅ 完全支持(示例用例已实机验证;事件触发受对应系统事件约束) |
|
|
87
|
+
| nativeEventEmitter.listeners | 查询订阅句柄 | eventType | Subscription[] | ✅ 完全支持(JS 兼容层;示例用例已实机验证) |
|
|
88
|
+
| nativeEventEmitter.listenerCount | 查询兼容层订阅数量 | eventType | number | ✅ 完全支持(JS 兼容层;示例用例已实机验证) |
|
|
89
|
+
| nativeEventEmitter.emit | 手动 JS 派发 | eventType, ...args | void | ✅ 完全支持(JS 兼容层;示例用例已实机验证) |
|
|
90
|
+
| nativeEventEmitter.removeListener | 按原回调移除 | eventType, listener | void | ✅ 完全支持(JS 兼容层;示例用例已实机验证) |
|
|
91
|
+
| nativeEventEmitter.removeAllListeners | 按事件移除全部订阅 | eventType | void | ✅ 完全支持(JS 兼容层;示例用例已实机验证) |
|
|
92
|
+
| nativeEventEmitter.removeSubscription | 校验归属并释放 | subscription | void | ✅ 完全支持(JS 兼容层;示例用例已实机验证) |
|
|
93
|
+
| nativeEventEmitter.removeCurrentListener | 派发期间移除自身 | 无 | void | ✅ 完全支持(JS 兼容层;示例用例已实机验证) |
|
|
94
|
+
| nativeEventEmitter.subscription.remove | 幂等释放真实订阅 | 无 | void | ✅ 完全支持(JS 兼容层;示例用例已实机验证) |
|
|
95
|
+
| BackgroundTaskOptions | 完整原字段及 parameters 泛型;新增 harmonyBackgroundModes | 类型声明 | — | ✅ 完全支持(TypeScript 类型定义;无独立运行时行为) |
|
|
96
|
+
| ForegroundServiceType | 原13种字面量 | 类型声明 | — | ✅ 完全支持(TypeScript 类型定义;无独立运行时行为) |
|
|
97
|
+
| NotificationPatch | 可更新通知字段的浅合并 | 类型声明 | — | ✅ 完全支持(TypeScript 类型定义;无独立运行时行为) |
|
|
98
|
+
| TaskFailure | taskError 的 code/message/generation | 类型声明 | — | ✅ 完全支持(TypeScript 类型定义;无独立运行时行为) |
|
|
99
|
+
|
|
100
|
+
关键参数:`taskName` 非空;标题和正文允许空串,不接受 null。`taskIcon.name` 指向本应用媒体资源,type 接受 media/mipmap/drawable,显式 package 必须为当前 bundleName。`linkingURI` 为绝对URI,宿主使用 Linking 接收并路由。`progressBar` 需 max>0、0≤value≤max;小数分别向下取整后换算百分比,不支持 indeterminate。通知补丁采用浅合并,嵌套图标整体替换,成功后才更新缓存。
|
|
101
|
+
|
|
102
|
+
`ForegroundServiceType` 保留13种字面量,新增 `harmonyBackgroundModes` 用于明确有歧义的实际业务模式。类型导出还包括 `BackgroundTaskOptions`、`NotificationPatch` 和 `TaskFailure`,不具有运行时构造值。
|
|
103
|
+
|
|
104
|
+
## 快速验证(运行 Example)
|
|
105
|
+
|
|
106
|
+
### 前置条件
|
|
107
|
+
|
|
108
|
+
| 依赖 | 版本要求 |
|
|
109
|
+
| --- | --- |
|
|
110
|
+
| Node.js | 20+(Example engines.node) |
|
|
111
|
+
| DevEco Studio | 6.0+,配套当前 RNOH 工具链 |
|
|
112
|
+
| HarmonyOS SDK | 编译 API 24,兼容 API 13+ |
|
|
113
|
+
|
|
114
|
+
### 运行步骤
|
|
115
|
+
|
|
116
|
+
**1. 克隆仓库**
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
git clone https://gitcode.com/hxa-rn/react-native-background-actions.git
|
|
120
|
+
cd react-native-background-actions
|
|
121
|
+
git checkout br_rnoh0.72
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**2. 安装依赖并构建**
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
npm install --legacy-peer-deps
|
|
128
|
+
npm pack # 生成 tgz 包,prepare 执行 TypeScript 检查
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**3. 进入 example 目录,安装依赖**
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
cd example # 或 example_auto
|
|
135
|
+
npm install --legacy-peer-deps
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**4. 生成 JS Bundle**
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
npm run dev
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
产物:`harmony/entry/src/main/resources/rawfile/bundle.harmony.js`
|
|
145
|
+
|
|
146
|
+
**5. 用 DevEco Studio 打开鸿蒙工程**
|
|
147
|
+
|
|
148
|
+
打开 DevEco Studio,选择 `example/harmony` 或 `example_auto/harmony` 目录,等待 Sync 完成。
|
|
149
|
+
|
|
150
|
+
**6. 编译并运行 HAP**
|
|
151
|
+
|
|
152
|
+
在 DevEco Studio 配置自己的设备签名后运行到设备或模拟器。未签名 HAP 可用于构建交付,不能据此声称已安装运行。
|
|
153
|
+
|
|
154
|
+
> **注意**:Example 已预置插件依赖和 Package 注册,无需手动配置 Link;通知授权及网络访问依赖真实系统环境。
|
|
155
|
+
|
|
156
|
+
## 约束与限制
|
|
157
|
+
|
|
158
|
+
本仓库使用 React 18.2.0、React Native 0.72.5、RNOH 0.72.139,编译 SDK API24、兼容API13。低版本RN与设备兼容性未完成实机认证。
|
|
159
|
+
|
|
160
|
+
- 默认、空类型数组及 shortService 使用系统有限短时额度;不自动续租,不承诺永久后台执行或进程死亡后恢复 JS 闭包。
|
|
161
|
+
- dataSync→dataTransfer、mediaPlayback→audioPlayback、phoneCall→voip、location→location、microphone→audioRecording。宿主负责真实业务与相关权限,任务必须符合系统后台策略。
|
|
162
|
+
- connectedDevice、mediaProjection 需要 harmonyBackgroundModes 明确业务模式;camera、health、remoteMessaging、systemExempted、specialUse 无通用等价模式,明确拒绝。
|
|
163
|
+
- color 仅兼容输入,不承诺通知染色;跨包Android图标、不确定进度等能力明确拒绝。
|
|
164
|
+
- API15+ 支持长时取消事件;API13/14 不能承诺即时发现全部系统取消。
|
|
165
|
+
- 使用权限包括 KEEP_BACKGROUND_RUNNING;网络业务需要 INTERNET,定位、录音等长时宿主业务另需其对应系统权限和动态授权。通知授权由 NotificationKit 处理。
|
|
166
|
+
- 后台任务活动期间新建的 setTimeout/setInterval 由后台计时桥调度;停止后恢复原调度。调用方仍须协作退出业务并清理自己的资源。
|
|
167
|
+
|
|
168
|
+
## 开源license
|
|
169
|
+
|
|
170
|
+
本项目基于 MIT 协议,详见 [LICENSE](LICENSE) 文件。
|
|
171
|
+
|
|
172
|
+
## 问题反馈渠道
|
|
173
|
+
|
|
174
|
+
使用问题请在 AtomGit 提交 Issue。也可在 GitCode 仓库反馈:
|
|
175
|
+
|
|
176
|
+
https://gitcode.com/hxa-rn/react-native-background-actions
|
|
177
|
+
|
|
178
|
+
https://gitcode.com/hxa-rn/react-native-background-actions/issues
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Rapsssito
|
|
4
|
+
Modifications Copyright (c) 2026 HarmonyOS X-platform Alliance
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
OPEN SOURCE SOFTWARE NOTICE
|
|
2
|
+
|
|
3
|
+
Please note we provide an open source software notice for the third party open source software along with this software and/or this software component (in the following just “this SOFTWARE”). The open source software licenses are granted by the respective right holders.
|
|
4
|
+
|
|
5
|
+
Warranty Disclaimer
|
|
6
|
+
THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
|
|
7
|
+
|
|
8
|
+
Copyright Notice and License Texts
|
|
9
|
+
|
|
10
|
+
----------------------------------------------------------------------
|
|
11
|
+
Software: react-native-netinfo V11.1.0
|
|
12
|
+
|
|
13
|
+
MIT License
|
|
14
|
+
|
|
15
|
+
Copyright (c) 2015-present, Facebook, Inc.
|
|
16
|
+
|
|
17
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
18
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
19
|
+
in the Software without restriction, including without limitation the rights
|
|
20
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
21
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
22
|
+
furnished to do so, subject to the following conditions:
|
|
23
|
+
|
|
24
|
+
The above copyright notice and this permission notice shall be included in all
|
|
25
|
+
copies or substantial portions of the Software.
|
|
26
|
+
|
|
27
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
28
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
29
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
30
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
31
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
32
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
33
|
+
SOFTWARE.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<configuration>
|
|
3
|
+
<oatconfig>
|
|
4
|
+
<licensefile>LICENSE</licensefile>
|
|
5
|
+
<filefilterlist>
|
|
6
|
+
<filefilter name="copyrightPolicyFilter" desc="Filters for compatibility,license header policies">
|
|
7
|
+
<filteritem type="filename" name="hvigorfile.ts" desc="hvigor构建脚本,DevEco Studio自动生成,不需要添加版权头"/>
|
|
8
|
+
<filteritem type="filename" name="*.json5" desc="hvigor工程配置文件,DevEco Studio自动生成,不需要添加版权头"/>
|
|
9
|
+
<filteritem type="filename" name="*.proto" desc="资源文件,不需要添加版权头"/>
|
|
10
|
+
<filteritem type="filename" name="*.json" desc="资源文件,不需要添加版权头"/>
|
|
11
|
+
<filteritem type="filepath" name="hvigorw" desc="工程模板,不修改版权头,以防有修改版权风险"/>
|
|
12
|
+
<filteritem type="filepath" name="hvigorw.bat" desc="工程模板,不修改版权头,以防有修改版权风险"/>
|
|
13
|
+
<filteritem type="filepath" name="hvigor/hvigor-wrapper.js" desc="工程模板,不修改版权头,以防有修改版权风险"/>
|
|
14
|
+
<filteritem type="filename" name="LICENSE" desc="工程文件,不修改版权头"/>
|
|
15
|
+
</filefilter>
|
|
16
|
+
<filefilter name="defaultPolicyFilter" desc="Filters for compatibility,license header policies">
|
|
17
|
+
<filteritem type="filename" name="hvigorfile.ts" desc="hvigor构建脚本,DevEco Studio自动生成,不需要添加许可证头"/>
|
|
18
|
+
<filteritem type="filename" name="*.json5" desc="hvigor工程配置文件,DevEco Studio自动生成,不需要添加许可证头"/>
|
|
19
|
+
<filteritem type="filename" name="LICENSE" desc="原三方库证书文件无需更改,因此添加过滤"/>
|
|
20
|
+
<filteritem type="filename" name="*.proto" desc="资源文件,不需要添加许可证头"/>
|
|
21
|
+
<filteritem type="filename" name="*.json" desc="资源文件,不需要添加许可证头"/>
|
|
22
|
+
<filteritem type="filepath" name="hvigorw" desc="工程模板,不修改版权头,以防有修改版权风险"/>
|
|
23
|
+
<filteritem type="filepath" name="hvigorw.bat" desc="工程模板,不修改版权头,以防有修改版权风险"/>
|
|
24
|
+
<filteritem type="filepath" name="hvigor/hvigor-wrapper.js" desc="工程模板,不修改版权头,以防有修改版权风险"/>
|
|
25
|
+
</filefilter>
|
|
26
|
+
<filefilter name="binaryFileTypePolicyFilter" desc="Filters for resources files policies">
|
|
27
|
+
<filteritem type="filename" name="icon.png" desc="应用图标"/>
|
|
28
|
+
<filteritem type="filename" name="app_icon.png" desc="应用图标"/>
|
|
29
|
+
<filteritem type="filename" name="warn.png" desc="页面展示图标"/>
|
|
30
|
+
</filefilter>
|
|
31
|
+
</filefilterlist>
|
|
32
|
+
<policylist>
|
|
33
|
+
<policy name="projectPolicy" desc="">
|
|
34
|
+
<policyitem type="license" name="MIT" path="*.*" desc="license under the MIT"/>
|
|
35
|
+
</policy>
|
|
36
|
+
</policylist>
|
|
37
|
+
</oatconfig>
|
|
38
|
+
</configuration>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"apiType": "stageMode",
|
|
3
|
+
// "buildOption": {
|
|
4
|
+
// "externalNativeOptions": {
|
|
5
|
+
// "path": "./src/main/cpp/CMakeLists.txt",
|
|
6
|
+
// "arguments": "",
|
|
7
|
+
// "cppFlags": ""
|
|
8
|
+
// },
|
|
9
|
+
// },
|
|
10
|
+
"targets": [
|
|
11
|
+
{
|
|
12
|
+
"name": "default",
|
|
13
|
+
"runtimeOS": "HarmonyOS"
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BackgroundActionsPackage as default } from './src/main/ets/BackgroundActionsTurboModulesFactory';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"license": "MIT",
|
|
3
|
+
"devDependencies": {
|
|
4
|
+
|
|
5
|
+
},
|
|
6
|
+
"author": "hxa-rn",
|
|
7
|
+
"name": "@hxa-rn/react-native-background-actions",
|
|
8
|
+
"description": "HarmonyOS background actions TurboModule",
|
|
9
|
+
"main": "index.ets",
|
|
10
|
+
"version": "4.1.0",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@rnoh/react-native-openharmony": "file:../react_native_openharmony_release.har"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#ifndef BACKGROUNDACTIONSPACKAGE_H
|
|
2
|
+
#define BACKGROUNDACTIONSPACKAGE_H
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include "generated/RNOHGeneratedPackage.h"
|
|
7
|
+
#include "BackgroundActionsTimerModule.h"
|
|
8
|
+
|
|
9
|
+
namespace rnoh {
|
|
10
|
+
class BackgroundActionsFactoryDelegate : public RNOHGeneratedPackageTurboModuleFactoryDelegate {
|
|
11
|
+
public:
|
|
12
|
+
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
|
|
13
|
+
if (name == "RNBackgroundActions") { return std::make_shared<BackgroundActionsTimerModule>(ctx, name); }
|
|
14
|
+
return RNOHGeneratedPackageTurboModuleFactoryDelegate::createTurboModule(ctx, name);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
class BackgroundActionsPackage : public RNOHGeneratedPackage {
|
|
18
|
+
public:
|
|
19
|
+
using Super = RNOHGeneratedPackage;
|
|
20
|
+
using Super::Super;
|
|
21
|
+
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
|
|
22
|
+
return std::make_unique<BackgroundActionsFactoryDelegate>();
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
} // namespace rnoh
|
|
26
|
+
#endif //BACKGROUNDACTIONSPACKAGE_H
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#include "BackgroundActionsTimerModule.h"
|
|
2
|
+
#include <algorithm>
|
|
3
|
+
#include <cmath>
|
|
4
|
+
|
|
5
|
+
namespace rnoh {
|
|
6
|
+
BackgroundActionsTimerObserver::BackgroundActionsTimerObserver(ArkTSTurboModule::Context const& ctx)
|
|
7
|
+
: ArkTSMessageHub::Observer(ctx.arkTSMessageHub), m_context(ctx) {}
|
|
8
|
+
BackgroundActionsTimerObserver::~BackgroundActionsTimerObserver() { clear(); }
|
|
9
|
+
void BackgroundActionsTimerObserver::cancel(int64_t id) {
|
|
10
|
+
auto it = m_timers.find(id);
|
|
11
|
+
if (it == m_timers.end()) return;
|
|
12
|
+
it->second.active->store(false);
|
|
13
|
+
m_context.taskExecutor->cancelDelayedTask(it->second.task);
|
|
14
|
+
m_timers.erase(it);
|
|
15
|
+
}
|
|
16
|
+
void BackgroundActionsTimerObserver::clear() {
|
|
17
|
+
while (!m_timers.empty()) cancel(m_timers.begin()->first);
|
|
18
|
+
}
|
|
19
|
+
void BackgroundActionsTimerObserver::onMessageReceived(ArkTSMessage const& message) {
|
|
20
|
+
if (message.name == "BACKGROUND_ACTIONS_CLEAR_TIMERS") { clear(); return; }
|
|
21
|
+
if (message.name != "BACKGROUND_ACTIONS_TIMER" && message.name != "BACKGROUND_ACTIONS_CLEAR_TIMER") return;
|
|
22
|
+
auto const& payload = message.payload;
|
|
23
|
+
if (!payload.isObject() || !payload.count("id") || !payload["id"].isNumber()) return;
|
|
24
|
+
double rawId = payload["id"].asDouble();
|
|
25
|
+
if (!std::isfinite(rawId) || rawId >= 0 || rawId < -9007199254740991.0 || std::floor(rawId) != rawId) return;
|
|
26
|
+
int64_t id = static_cast<int64_t>(rawId);
|
|
27
|
+
cancel(id);
|
|
28
|
+
if (message.name == "BACKGROUND_ACTIONS_CLEAR_TIMER") return;
|
|
29
|
+
if (!payload.count("delay") || !payload["delay"].isNumber() ||
|
|
30
|
+
!payload.count("repeats") || !payload["repeats"].isBool()) return;
|
|
31
|
+
double delay = payload["delay"].asDouble();
|
|
32
|
+
if (!std::isfinite(delay) || delay < 0 || delay > 2147483647.0) return;
|
|
33
|
+
bool repeats = payload["repeats"].asBool();
|
|
34
|
+
auto active = std::make_shared<std::atomic<bool>>(true);
|
|
35
|
+
auto task = m_context.taskExecutor->runDelayedTask(TaskThread::JS,
|
|
36
|
+
[safeInstance = m_context.safeInstance, active, id] {
|
|
37
|
+
if (!active->load()) return;
|
|
38
|
+
if (auto instance = safeInstance.lock()) {
|
|
39
|
+
instance->callJSFunction("RCTDeviceEventEmitter", "emit",
|
|
40
|
+
folly::dynamic::array("backgroundActionsTimer", static_cast<double>(id)));
|
|
41
|
+
}
|
|
42
|
+
}, static_cast<uint64_t>(delay), repeats ? std::max<uint64_t>(1, static_cast<uint64_t>(delay)) : 0);
|
|
43
|
+
m_timers.emplace(id, Timer{task, active});
|
|
44
|
+
}
|
|
45
|
+
BackgroundActionsTimerModule::BackgroundActionsTimerModule(ArkTSTurboModule::Context const& ctx, std::string const& name)
|
|
46
|
+
: RNBackgroundActions(ctx, name), m_state(std::make_shared<State>()), m_executor(ctx.taskExecutor) {
|
|
47
|
+
m_executor->runTask(TaskThread::MAIN, [state = m_state, ctx] {
|
|
48
|
+
state->observer = std::make_unique<BackgroundActionsTimerObserver>(ctx);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
BackgroundActionsTimerModule::~BackgroundActionsTimerModule() {
|
|
52
|
+
m_executor->runTask(TaskThread::MAIN, [state = std::move(m_state)] { state->observer.reset(); });
|
|
53
|
+
}
|
|
54
|
+
} // namespace rnoh
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include "generated/RNBackgroundActions.h"
|
|
3
|
+
#include "RNOH/ArkTSMessageHub.h"
|
|
4
|
+
#include <atomic>
|
|
5
|
+
#include <map>
|
|
6
|
+
|
|
7
|
+
namespace rnoh {
|
|
8
|
+
// All registration, cancellation and observer ownership is on MAIN. Callback
|
|
9
|
+
// execution is on JS; atomic tokens suppress callbacks already in the queue.
|
|
10
|
+
class BackgroundActionsTimerObserver final : public ArkTSMessageHub::Observer {
|
|
11
|
+
public:
|
|
12
|
+
explicit BackgroundActionsTimerObserver(ArkTSTurboModule::Context const& ctx);
|
|
13
|
+
~BackgroundActionsTimerObserver() override;
|
|
14
|
+
void onMessageReceived(ArkTSMessage const& message) override;
|
|
15
|
+
private:
|
|
16
|
+
struct Timer {
|
|
17
|
+
TaskExecutor::DelayedTask task;
|
|
18
|
+
std::shared_ptr<std::atomic<bool>> active;
|
|
19
|
+
};
|
|
20
|
+
void cancel(int64_t id);
|
|
21
|
+
void clear();
|
|
22
|
+
ArkTSTurboModule::Context m_context;
|
|
23
|
+
std::map<int64_t, Timer> m_timers;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
class BackgroundActionsTimerModule final : public RNBackgroundActions {
|
|
27
|
+
public:
|
|
28
|
+
BackgroundActionsTimerModule(ArkTSTurboModule::Context const& ctx, std::string const& name);
|
|
29
|
+
~BackgroundActionsTimerModule() override;
|
|
30
|
+
private:
|
|
31
|
+
struct State { std::unique_ptr<BackgroundActionsTimerObserver> observer; };
|
|
32
|
+
std::shared_ptr<State> m_state;
|
|
33
|
+
TaskExecutor::Shared m_executor;
|
|
34
|
+
};
|
|
35
|
+
} // namespace rnoh
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
set(library_cpp_dir "${CMAKE_CURRENT_SOURCE_DIR}/..")
|
|
2
|
+
set(library_generated_dir "${library_cpp_dir}/generated")
|
|
3
|
+
file(GLOB_RECURSE library_generated_SRC CONFIGURE_DEPENDS "${library_generated_dir}/*.cpp")
|
|
4
|
+
file(GLOB library_SRC CONFIGURE_DEPENDS "${library_cpp_dir}/*.cpp")
|
|
5
|
+
file(GLOB library_codegen_ns_dirs LIST_DIRECTORIES true "${library_generated_dir}/_oh_rn_*")
|
|
6
|
+
add_library(background_actions SHARED ${library_SRC} ${library_generated_SRC})
|
|
7
|
+
target_include_directories(background_actions PUBLIC ${library_cpp_dir} ${library_generated_dir} ${library_codegen_ns_dirs})
|
|
8
|
+
target_link_libraries(background_actions PUBLIC rnoh)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-harmony"
|
|
3
|
+
*
|
|
4
|
+
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
+
* lost once the code is regenerated.
|
|
6
|
+
*
|
|
7
|
+
* @generatorVersion: 1
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#include "RNBackgroundActions.h"
|
|
11
|
+
|
|
12
|
+
namespace rnoh {
|
|
13
|
+
using namespace facebook;
|
|
14
|
+
|
|
15
|
+
RNBackgroundActions::RNBackgroundActions(const ArkTSTurboModule::Context ctx, const std::string name) : ArkTSTurboModule(ctx, name) {
|
|
16
|
+
methodMap_ = {
|
|
17
|
+
ARK_ASYNC_METHOD_METADATA(start, 1),
|
|
18
|
+
ARK_ASYNC_METHOD_METADATA(stop, 0),
|
|
19
|
+
ARK_ASYNC_METHOD_METADATA(updateNotification, 1),
|
|
20
|
+
ARK_METHOD_METADATA(isRunning, 0),
|
|
21
|
+
ARK_ASYNC_METHOD_METADATA(getStatus, 0),
|
|
22
|
+
ARK_ASYNC_METHOD_METADATA(requestNotifications, 0),
|
|
23
|
+
ARK_METHOD_METADATA(scheduleTimer, 3),
|
|
24
|
+
ARK_METHOD_METADATA(cancelTimer, 1),
|
|
25
|
+
ARK_METHOD_METADATA(addListener, 1),
|
|
26
|
+
ARK_METHOD_METADATA(removeListeners, 1),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
} // namespace rnoh
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-harmony"
|
|
3
|
+
*
|
|
4
|
+
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
+
* lost once the code is regenerated.
|
|
6
|
+
*
|
|
7
|
+
* @generatorVersion: 1
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#pragma once
|
|
11
|
+
|
|
12
|
+
#include "RNOH/ArkTSTurboModule.h"
|
|
13
|
+
|
|
14
|
+
namespace rnoh {
|
|
15
|
+
|
|
16
|
+
class JSI_EXPORT RNBackgroundActions : public ArkTSTurboModule {
|
|
17
|
+
public:
|
|
18
|
+
RNBackgroundActions(const ArkTSTurboModule::Context ctx, const std::string name);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
} // namespace rnoh
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-harmony"
|
|
3
|
+
*
|
|
4
|
+
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
+
* lost once the code is regenerated.
|
|
6
|
+
*
|
|
7
|
+
* @generatorVersion: 1
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#pragma once
|
|
11
|
+
|
|
12
|
+
#include "RNOH/Package.h"
|
|
13
|
+
#include "RNOH/ArkTSTurboModule.h"
|
|
14
|
+
#include "generated/RNBackgroundActions.h"
|
|
15
|
+
|
|
16
|
+
namespace rnoh {
|
|
17
|
+
|
|
18
|
+
class RNOHGeneratedPackageTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
|
|
19
|
+
public:
|
|
20
|
+
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
|
|
21
|
+
if (name == "RNBackgroundActions") {
|
|
22
|
+
return std::make_shared<RNBackgroundActions>(ctx, name);
|
|
23
|
+
}
|
|
24
|
+
return nullptr;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class GeneratedEventEmitRequestHandler : public EventEmitRequestHandler {
|
|
29
|
+
public:
|
|
30
|
+
void handleEvent(Context const &ctx) override {
|
|
31
|
+
auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter<facebook::react::EventEmitter>(ctx.tag);
|
|
32
|
+
auto componentName = ctx.shadowViewRegistry->getComponentName(ctx.tag);
|
|
33
|
+
|
|
34
|
+
if (eventEmitter == nullptr) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
std::vector<std::string> supportedComponentNames = {
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
std::vector<std::string> supportedEventNames = {
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
if (std::find(supportedComponentNames.begin(), supportedComponentNames.end(), componentName) != supportedComponentNames.end() &&
|
|
45
|
+
std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) != supportedEventNames.end()) {
|
|
46
|
+
eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
class RNOHGeneratedPackage : public Package {
|
|
52
|
+
public:
|
|
53
|
+
RNOHGeneratedPackage(Package::Context ctx) : Package(ctx){};
|
|
54
|
+
|
|
55
|
+
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
|
|
56
|
+
return std::make_unique<RNOHGeneratedPackageTurboModuleFactoryDelegate>();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
std::vector<facebook::react::ComponentDescriptorProvider> createComponentDescriptorProviders() override {
|
|
60
|
+
return {
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
ComponentJSIBinderByString createComponentJSIBinderByName() override {
|
|
65
|
+
return {
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
EventEmitRequestHandlers createEventEmitRequestHandlers() override {
|
|
70
|
+
return {
|
|
71
|
+
std::make_shared<GeneratedEventEmitRequestHandler>(),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
} // namespace rnoh
|