mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-11-22 15:36:10 +08:00
Compare commits
72 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f941b93cb3 | ||
|
|
bea0e077a0 | ||
|
|
dc9b5d722a | ||
|
|
e151c9c618 | ||
|
|
2a79061b89 | ||
|
|
e04ce54de6 | ||
|
|
38229a8bca | ||
|
|
6661e307cd | ||
|
|
027bd16af2 | ||
|
|
4daaadce70 | ||
|
|
43ed2f50fe | ||
|
|
e46d01714a | ||
|
|
366b2a6618 | ||
|
|
34e053ae48 | ||
|
|
d55ef1d8c8 | ||
|
|
de3e7d9e4c | ||
|
|
e0201d3882 | ||
|
|
ba5b35813d | ||
|
|
3134f36739 | ||
|
|
d4f4740053 | ||
|
|
a248f18035 | ||
|
|
268f39f43b | ||
|
|
6e4f432e26 | ||
|
|
1b4c03924a | ||
|
|
d458371f54 | ||
|
|
84381e5ed7 | ||
|
|
8970fd406d | ||
|
|
34d6fef493 | ||
|
|
84b71e33a8 | ||
|
|
6cd99dece0 | ||
|
|
d726f3602b | ||
|
|
da21c99bcc | ||
|
|
1f8748375c | ||
|
|
fa731cd583 | ||
|
|
d077dcb6d3 | ||
|
|
59b60fdc6d | ||
|
|
c768705221 | ||
|
|
57206dd2f1 | ||
|
|
4e27d906c3 | ||
|
|
c24f469475 | ||
|
|
8f8a29eda8 | ||
|
|
a78542b214 | ||
|
|
8d9ae57a5f | ||
|
|
2502935fc0 | ||
|
|
8e6d9bf460 | ||
|
|
897f334343 | ||
|
|
33bc69c3fb | ||
|
|
5028ce31be | ||
|
|
d3a4007763 | ||
|
|
a4f3e3cc38 | ||
|
|
9d51128ed3 | ||
|
|
eddb072927 | ||
|
|
4f9417d620 | ||
|
|
6f2314d3c9 | ||
|
|
a6e9ece559 | ||
|
|
78430e2ec2 | ||
|
|
ec5b9e1938 | ||
|
|
53dfb45ca2 | ||
|
|
4a62e89c73 | ||
|
|
655f4c8cf5 | ||
|
|
3732c196a1 | ||
|
|
bfb520bd07 | ||
|
|
f7be8a4d71 | ||
|
|
4383a66274 | ||
|
|
a82b75f51f | ||
|
|
584f698329 | ||
|
|
e58903a634 | ||
|
|
41e1028b2d | ||
|
|
11d40ce5f2 | ||
|
|
4a1d4d5a50 | ||
|
|
02517a9eb0 | ||
|
|
f7309f699f |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -51,3 +51,12 @@ android/bin
|
|||||||
Example/testHotUpdate/harmony
|
Example/testHotUpdate/harmony
|
||||||
Example/testHotUpdate/android/app/.cxx
|
Example/testHotUpdate/android/app/.cxx
|
||||||
Example/harmony_use_pushy/libs
|
Example/harmony_use_pushy/libs
|
||||||
|
**/mcp.json
|
||||||
|
|
||||||
|
|
||||||
|
harmony/package
|
||||||
|
**/oh_modules
|
||||||
|
harmony/pushy/.preview
|
||||||
|
Example/harmony_use_pushy/harmony/entry/src/main/resources/rawfile/meta.json
|
||||||
|
**/.hvigor
|
||||||
|
Example/harmony_use_pushy/harmony/entry/src/main/cpp/generated
|
||||||
|
|||||||
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -4,9 +4,3 @@
|
|||||||
[submodule "android/jni/HDiffPatch"]
|
[submodule "android/jni/HDiffPatch"]
|
||||||
path = android/jni/HDiffPatch
|
path = android/jni/HDiffPatch
|
||||||
url = https://github.com/sisong/HDiffPatch.git
|
url = https://github.com/sisong/HDiffPatch.git
|
||||||
[submodule "harmony/src/main/cpp/HDiffPatch"]
|
|
||||||
path = harmony/src/main/cpp/HDiffPatch
|
|
||||||
url = https://github.com/sisong/HDiffPatch.git
|
|
||||||
[submodule "harmony/src/main/cpp/lzma"]
|
|
||||||
path = harmony/src/main/cpp/lzma
|
|
||||||
url = https://github.com/sisong/lzma.git
|
|
||||||
|
|||||||
@@ -20,7 +20,8 @@
|
|||||||
"adaptiveIcon": {
|
"adaptiveIcon": {
|
||||||
"foregroundImage": "./assets/adaptive-icon.png",
|
"foregroundImage": "./assets/adaptive-icon.png",
|
||||||
"backgroundColor": "#ffffff"
|
"backgroundColor": "#ffffff"
|
||||||
}
|
},
|
||||||
|
"package": "com.anonymous.expoUsePushy"
|
||||||
},
|
},
|
||||||
"web": {
|
"web": {
|
||||||
"favicon": "./assets/favicon.png"
|
"favicon": "./assets/favicon.png"
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1 @@
|
|||||||
import { registerRootComponent } from 'expo';
|
import 'expo-router/entry';
|
||||||
|
|
||||||
import App from './App';
|
|
||||||
|
|
||||||
// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
|
|
||||||
// It also ensures that whether you load the app in Expo Go or in a native build,
|
|
||||||
// the environment is set up appropriately
|
|
||||||
registerRootComponent(App);
|
|
||||||
|
|||||||
@@ -1,24 +1,50 @@
|
|||||||
{
|
{
|
||||||
"name": "expousepushy",
|
"name": "expousepushy",
|
||||||
"version": "1.0.0",
|
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
"version": "1.0.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "expo start",
|
"start": "expo start",
|
||||||
|
"reset-project": "node ./scripts/reset-project.js",
|
||||||
"android": "expo run:android",
|
"android": "expo run:android",
|
||||||
"ios": "expo run:ios",
|
"ios": "expo run:ios",
|
||||||
"web": "expo start --web"
|
"web": "expo start --web",
|
||||||
|
"lint": "expo lint"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"expo": "~52.0.46",
|
"@expo/vector-icons": "^14.1.0",
|
||||||
"expo-status-bar": "~2.0.1",
|
"@react-navigation/bottom-tabs": "^7.3.10",
|
||||||
"react": "18.3.1",
|
"@react-navigation/elements": "^2.3.8",
|
||||||
"react-native": "0.76.9",
|
"@react-navigation/native": "^7.1.6",
|
||||||
"react-native-update": "^10.28.7"
|
"expo": "~53.0.22",
|
||||||
|
"expo-blur": "~14.1.5",
|
||||||
|
"expo-constants": "~17.1.7",
|
||||||
|
"expo-font": "~13.3.2",
|
||||||
|
"expo-haptics": "~14.1.4",
|
||||||
|
"expo-image": "~2.4.0",
|
||||||
|
"expo-linking": "~7.1.7",
|
||||||
|
"expo-router": "~5.1.5",
|
||||||
|
"expo-splash-screen": "~0.30.10",
|
||||||
|
"expo-status-bar": "~2.2.3",
|
||||||
|
"expo-symbols": "~0.4.5",
|
||||||
|
"expo-system-ui": "~5.0.11",
|
||||||
|
"expo-web-browser": "~14.2.0",
|
||||||
|
"react": "19.0.0",
|
||||||
|
"react-dom": "19.0.0",
|
||||||
|
"react-native": "0.79.6",
|
||||||
|
"react-native-gesture-handler": "~2.24.0",
|
||||||
|
"react-native-reanimated": "~3.17.4",
|
||||||
|
"react-native-safe-area-context": "5.4.0",
|
||||||
|
"react-native-screens": "~4.11.1",
|
||||||
|
"react-native-update": "^10.34.4",
|
||||||
|
"react-native-web": "~0.20.0",
|
||||||
|
"react-native-webview": "13.13.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.25.2",
|
"@babel/core": "^7.25.2",
|
||||||
"@types/react": "~18.3.12",
|
"@types/react": "~19.0.10",
|
||||||
"typescript": "^5.3.3"
|
"typescript": "~5.8.3",
|
||||||
|
"eslint": "^9.25.0",
|
||||||
|
"eslint-config-expo": "~9.2.0"
|
||||||
},
|
},
|
||||||
"private": true
|
"private": true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
/* eslint-disable react/no-unstable-nested-components */
|
|
||||||
/* eslint-disable react-native/no-inline-styles */
|
/* eslint-disable react-native/no-inline-styles */
|
||||||
import React, {useState} from 'react';
|
import React, {useState} from 'react';
|
||||||
import {StyleSheet, Text, View, TouchableOpacity, Image} from 'react-native';
|
import {StyleSheet, Text, View, TouchableOpacity, Image} from 'react-native';
|
||||||
@@ -6,7 +5,7 @@ import {StyleSheet, Text, View, TouchableOpacity, Image} from 'react-native';
|
|||||||
import TestConsole from './TestConsole';
|
import TestConsole from './TestConsole';
|
||||||
|
|
||||||
import _updateConfig from './update.json';
|
import _updateConfig from './update.json';
|
||||||
import {PushyProvider, Pushy, usePushy} from 'react-native-update';
|
import {UpdateProvider, Pushy, useUpdate} from 'react-native-update';
|
||||||
const {appKey} = _updateConfig.harmony;
|
const {appKey} = _updateConfig.harmony;
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
@@ -20,7 +19,7 @@ function App() {
|
|||||||
packageVersion,
|
packageVersion,
|
||||||
currentHash,
|
currentHash,
|
||||||
progress: {received, total} = {},
|
progress: {received, total} = {},
|
||||||
} = usePushy();
|
} = useUpdate();
|
||||||
const [useDefaultAlert, setUseDefaultAlert] = useState(false);
|
const [useDefaultAlert, setUseDefaultAlert] = useState(false);
|
||||||
const [showTestConsole, setShowTestConsole] = useState(false);
|
const [showTestConsole, setShowTestConsole] = useState(false);
|
||||||
const [showUpdateBanner, setShowUpdateBanner] = useState(false);
|
const [showUpdateBanner, setShowUpdateBanner] = useState(false);
|
||||||
@@ -41,6 +40,7 @@ function App() {
|
|||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<Text style={styles.welcome}>欢迎使用Pushy热更新服务</Text>
|
<Text style={styles.welcome}>欢迎使用Pushy热更新服务</Text>
|
||||||
|
{/* <Image source={require('./gmail.png')} style={styles.image} /> */}
|
||||||
{/* <Text style={styles.welcome}>😁hdiffFromAPP更新成功!!!</Text> */}
|
{/* <Text style={styles.welcome}>😁hdiffFromAPP更新成功!!!</Text> */}
|
||||||
{/* <Text style={styles.welcome}>😁hdiffFromPPk更新成功!!!</Text> */}
|
{/* <Text style={styles.welcome}>😁hdiffFromPPk更新成功!!!</Text> */}
|
||||||
<View style={{flexDirection: 'row'}}>
|
<View style={{flexDirection: 'row'}}>
|
||||||
@@ -166,7 +166,7 @@ function App() {
|
|||||||
style={{marginRight: 20}}>
|
style={{marginRight: 20}}>
|
||||||
<Text style={{color: '#2196F3'}}>下次再说</Text>
|
<Text style={{color: '#2196F3'}}>下次再说</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
<TouchableOpacity onPress={switchVersion}>
|
<TouchableOpacity onPress={() => switchVersion()}>
|
||||||
<Text style={{color: '#2196F3'}}>立即重启</Text>
|
<Text style={{color: '#2196F3'}}>立即重启</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
</View>
|
</View>
|
||||||
@@ -204,18 +204,22 @@ const styles = StyleSheet.create({
|
|||||||
color: '#333333',
|
color: '#333333',
|
||||||
marginBottom: 5,
|
marginBottom: 5,
|
||||||
},
|
},
|
||||||
image: {},
|
image: {
|
||||||
|
width: 109,
|
||||||
|
height: 40,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const pushyClient = new Pushy({
|
const pushyClient = new Pushy({
|
||||||
appKey,
|
appKey,
|
||||||
debug: true,
|
debug: true,
|
||||||
|
updateStrategy: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function Root() {
|
export default function Root() {
|
||||||
return (
|
return (
|
||||||
<PushyProvider client={pushyClient}>
|
<UpdateProvider client={pushyClient}>
|
||||||
<App />
|
<App />
|
||||||
</PushyProvider>
|
</UpdateProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
source 'https://rubygems.org'
|
|
||||||
|
|
||||||
# You may use http://rbenv.org/ or https://rvm.io/ to install and use this version
|
|
||||||
ruby ">= 2.6.10"
|
|
||||||
|
|
||||||
gem 'cocoapods', '~> 1.12'
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,41 +1,44 @@
|
|||||||
{
|
{
|
||||||
"app": {
|
app: {
|
||||||
"signingConfigs": [],
|
signingConfigs: [],
|
||||||
"products": [
|
products: [
|
||||||
{
|
{
|
||||||
"name": "default",
|
name: 'default',
|
||||||
"signingConfig": "default",
|
signingConfig: 'default',
|
||||||
"compatibleSdkVersion": "5.0.0(12)",
|
compatibleSdkVersion: '5.0.0(12)',
|
||||||
"runtimeOS": "HarmonyOS",
|
runtimeOS: 'HarmonyOS',
|
||||||
"buildOption": {
|
buildOption: {
|
||||||
"strictMode": {
|
strictMode: {
|
||||||
"caseSensitiveCheck": true,
|
caseSensitiveCheck: true,
|
||||||
"useNormalizedOHMUrl": true
|
useNormalizedOHMUrl: true
|
||||||
}
|
},
|
||||||
}
|
"nativeCompiler": "BiSheng"
|
||||||
}
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
"buildModeSet": [
|
buildModeSet: [
|
||||||
{
|
{
|
||||||
"name": "debug",
|
name: 'debug',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "release"
|
name: 'release',
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
},
|
},
|
||||||
"modules": [
|
modules: [
|
||||||
{
|
{
|
||||||
"name": "entry",
|
name: 'entry',
|
||||||
"srcPath": "./entry",
|
srcPath: './entry',
|
||||||
"targets": [
|
targets: [
|
||||||
{
|
{
|
||||||
"name": "default",
|
name: 'default',
|
||||||
"applyToProducts": [
|
applyToProducts: ['default'],
|
||||||
"default"
|
},
|
||||||
]
|
],
|
||||||
}
|
},
|
||||||
]
|
{
|
||||||
}
|
name: 'pushy',
|
||||||
]
|
srcPath: '../node_modules/react-native-update/harmony/pushy',
|
||||||
}
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,44 +1,9 @@
|
|||||||
import { hapTasks } from '@ohos/hvigor-ohos-plugin';
|
import {hapTasks} from '@ohos/hvigor-ohos-plugin';
|
||||||
import fs from 'fs';
|
import {reactNativeUpdatePlugin} from 'pushy/hvigor-plugin';
|
||||||
import path from 'path';
|
|
||||||
|
|
||||||
export function generatePushyBuildTime(str?: string) {
|
|
||||||
return {
|
|
||||||
pluginId: 'PushyBuildTimePlugin',
|
|
||||||
apply(pluginContext) {
|
|
||||||
pluginContext.registerTask({
|
|
||||||
name: 'pushy_build_time',
|
|
||||||
run: (taskContext) => {
|
|
||||||
const metaFilePath = path.resolve(__dirname, 'src/main/resources/rawfile/meta.json');
|
|
||||||
const dirPath = path.dirname(metaFilePath);
|
|
||||||
if (!fs.existsSync(dirPath)) {
|
|
||||||
fs.mkdirSync(dirPath, { recursive: true });
|
|
||||||
}
|
|
||||||
const moduleJsonPath = path.resolve(__dirname, '../AppScope/app.json5');
|
|
||||||
let versionName = '';
|
|
||||||
if (fs.existsSync(moduleJsonPath)) {
|
|
||||||
const moduleContent = fs.readFileSync(moduleJsonPath, 'utf-8');
|
|
||||||
const versionMatch = moduleContent.match(/"versionName":\s*"([^"]+)"/);
|
|
||||||
if (versionMatch && versionMatch[1]) {
|
|
||||||
versionName = versionMatch[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const buildTime = new Date().toISOString();
|
|
||||||
const metaContent = {
|
|
||||||
pushy_build_time: buildTime,
|
|
||||||
versionName: versionName
|
|
||||||
};
|
|
||||||
fs.writeFileSync(metaFilePath, JSON.stringify(metaContent, null, 4));
|
|
||||||
console.log(`Build time written to ${metaFilePath}`);
|
|
||||||
},
|
|
||||||
dependencies: [],
|
|
||||||
postDependencies: ['default@BuildJS']
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
|
system: hapTasks /* Built-in plugin of Hvigor. It cannot be modified. */,
|
||||||
plugins:[generatePushyBuildTime()] /* Custom plugin to extend the functionality of Hvigor. */
|
plugins: [
|
||||||
}
|
reactNativeUpdatePlugin(),
|
||||||
|
] /* Custom plugin to extend the functionality of Hvigor. */,
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,25 +1,26 @@
|
|||||||
{
|
{
|
||||||
"meta": {
|
"meta": {
|
||||||
"stableOrder": true
|
"stableOrder": true,
|
||||||
|
"enableUnifiedLockfile": false
|
||||||
},
|
},
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
|
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
|
||||||
"specifiers": {
|
"specifiers": {
|
||||||
"@rnoh/react-native-openharmony@0.72.38": "@rnoh/react-native-openharmony@0.72.38",
|
"@rnoh/react-native-openharmony@0.72.96": "@rnoh/react-native-openharmony@0.72.96",
|
||||||
"pushy@../../node_modules/react-native-update/harmony": "pushy@../../node_modules/react-native-update/harmony"
|
"pushy@../../node_modules/react-native-update/harmony/pushy": "pushy@../../node_modules/react-native-update/harmony/pushy"
|
||||||
},
|
},
|
||||||
"packages": {
|
"packages": {
|
||||||
"@rnoh/react-native-openharmony@0.72.38": {
|
"@rnoh/react-native-openharmony@0.72.96": {
|
||||||
"name": "@rnoh/react-native-openharmony",
|
"name": "",
|
||||||
"version": "0.72.38",
|
"version": "0.72.96",
|
||||||
"integrity": "sha512-br5SIrbB0OarSLirenleE7eTOX1lNccMJ7nb/G7qWTyJ7kW4DalmTXVKYpoT2qaOLls1uEE7McD1OjbZZM9jug==",
|
"integrity": "sha512-gBbm8LLyqi5UE7qHWdZYeQnjyncfEpCczKZUP/9M2U1Z7exR0Kya8PMKMwr1ta5ujy7w/hZVC2LomEV4QvBeqA==",
|
||||||
"resolved": "https://ohpm.openharmony.cn/ohpm/@rnoh/react-native-openharmony/-/react-native-openharmony-0.72.38.har",
|
"resolved": "https://ohpm.openharmony.cn/ohpm/@rnoh/react-native-openharmony/-/react-native-openharmony-0.72.96.har",
|
||||||
"registryType": "ohpm"
|
"registryType": "ohpm"
|
||||||
},
|
},
|
||||||
"pushy@../../node_modules/react-native-update/harmony": {
|
"pushy@../../node_modules/react-native-update/harmony/pushy": {
|
||||||
"name": "pushy",
|
"name": "pushy",
|
||||||
"version": "3.1.0-0.0.7",
|
"version": "3.1.0-0.0.7",
|
||||||
"resolved": "../../node_modules/react-native-update/harmony",
|
"resolved": "",
|
||||||
"registryType": "local",
|
"registryType": "local",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@rnoh/react-native-openharmony": "^0.72.38"
|
"@rnoh/react-native-openharmony": "^0.72.38"
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "entry",
|
name: 'entry',
|
||||||
"version": "1.0.0",
|
version: '1.0.0',
|
||||||
"description": "Please describe the basic information.",
|
description: 'Please describe the basic information.',
|
||||||
"main": "",
|
main: '',
|
||||||
"author": "",
|
author: '',
|
||||||
"license": "",
|
license: '',
|
||||||
"dependencies": {
|
dependencies: {
|
||||||
"@rnoh/react-native-openharmony": "0.72.38",
|
'@rnoh/react-native-openharmony': '0.72.96',
|
||||||
"pushy": "file:../../node_modules/react-native-update/harmony/pushy.har",
|
pushy: 'file:../../node_modules/react-native-update/harmony/pushy',
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
/**
|
|
||||||
* This code was generated by "react-native codegen-harmony"
|
|
||||||
*
|
|
||||||
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
||||||
* lost once the code is regenerated.
|
|
||||||
*
|
|
||||||
* @generatorVersion: 1
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "RNOH/Package.h"
|
|
||||||
#include "RNOH/ArkTSTurboModule.h"
|
|
||||||
|
|
||||||
namespace rnoh {
|
|
||||||
|
|
||||||
class RNOHGeneratedPackageTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
|
|
||||||
public:
|
|
||||||
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
|
|
||||||
return nullptr;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
class GeneratedEventEmitRequestHandler : public EventEmitRequestHandler {
|
|
||||||
public:
|
|
||||||
void handleEvent(Context const &ctx) override {
|
|
||||||
auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter<facebook::react::EventEmitter>(ctx.tag);
|
|
||||||
if (eventEmitter == nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::string> supportedEventNames = {
|
|
||||||
};
|
|
||||||
if (std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) != supportedEventNames.end()) {
|
|
||||||
eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class RNOHGeneratedPackage : public Package {
|
|
||||||
public:
|
|
||||||
RNOHGeneratedPackage(Package::Context ctx) : Package(ctx){};
|
|
||||||
|
|
||||||
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
|
|
||||||
return std::make_unique<RNOHGeneratedPackageTurboModuleFactoryDelegate>();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<facebook::react::ComponentDescriptorProvider> createComponentDescriptorProviders() override {
|
|
||||||
return {
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
ComponentJSIBinderByString createComponentJSIBinderByName() override {
|
|
||||||
return {
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
EventEmitRequestHandlers createEventEmitRequestHandlers() override {
|
|
||||||
return {
|
|
||||||
std::make_shared<GeneratedEventEmitRequestHandler>(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace rnoh
|
|
||||||
@@ -54,7 +54,6 @@ struct Index {
|
|||||||
enableCAPIArchitecture: true,
|
enableCAPIArchitecture: true,
|
||||||
arkTsComponentNames: arkTsComponentNames,
|
arkTsComponentNames: arkTsComponentNames,
|
||||||
},
|
},
|
||||||
initialProps: { "foo": "bar" } as Record<string, string>,
|
|
||||||
appKey: "harmony_use_pushy",
|
appKey: "harmony_use_pushy",
|
||||||
wrappedCustomRNComponentBuilder: wrappedCustomRNComponentBuilder,
|
wrappedCustomRNComponentBuilder: wrappedCustomRNComponentBuilder,
|
||||||
onSetUp: (rnInstance) => {
|
onSetUp: (rnInstance) => {
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"pushy_build_time": "2025-04-30T02:46:33.340Z",
|
|
||||||
"versionName": "1.0.0"
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"modelVersion": "5.0.0",
|
"modelVersion": "5.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
pushy: 'file:../../node_modules/react-native-update/harmony/pushy'
|
||||||
},
|
},
|
||||||
"execution": {
|
"execution": {
|
||||||
// "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */
|
// "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { appTasks } from '@ohos/hvigor-ohos-plugin';
|
import {appTasks} from '@ohos/hvigor-ohos-plugin';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
|
system: appTasks /* Built-in plugin of Hvigor. It cannot be modified. */,
|
||||||
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
|
plugins: [] /* Custom plugin to extend the functionality of Hvigor. */,
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,23 +1,16 @@
|
|||||||
{
|
{
|
||||||
"meta": {
|
"meta": {
|
||||||
"stableOrder": true
|
"stableOrder": true,
|
||||||
|
"enableUnifiedLockfile": false
|
||||||
},
|
},
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
|
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
|
||||||
"specifiers": {
|
"specifiers": {
|
||||||
"@ohos/hamock@1.0.0": "@ohos/hamock@1.0.0",
|
|
||||||
"@ohos/hypium@1.0.19": "@ohos/hypium@1.0.19"
|
"@ohos/hypium@1.0.19": "@ohos/hypium@1.0.19"
|
||||||
},
|
},
|
||||||
"packages": {
|
"packages": {
|
||||||
"@ohos/hamock@1.0.0": {
|
|
||||||
"name": "@ohos/hamock",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"integrity": "sha512-K6lDPYc6VkKe6ZBNQa9aoG+ZZMiwqfcR/7yAVFSUGIuOAhPvCJAo9+t1fZnpe0dBRBPxj2bxPPbKh69VuyAtDg==",
|
|
||||||
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hamock/-/hamock-1.0.0.har",
|
|
||||||
"registryType": "ohpm"
|
|
||||||
},
|
|
||||||
"@ohos/hypium@1.0.19": {
|
"@ohos/hypium@1.0.19": {
|
||||||
"name": "@ohos/hypium",
|
"name": "",
|
||||||
"version": "1.0.19",
|
"version": "1.0.19",
|
||||||
"integrity": "sha512-cEjDgLFCm3cWZDeRXk7agBUkPqjWxUo6AQeiu0gEkb3J8ESqlduQLSIXeo3cCsm8U/asL7iKjF85ZyOuufAGSQ==",
|
"integrity": "sha512-cEjDgLFCm3cWZDeRXk7agBUkPqjWxUo6AQeiu0gEkb3J8ESqlduQLSIXeo3cCsm8U/asL7iKjF85ZyOuufAGSQ==",
|
||||||
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.19.har",
|
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.19.har",
|
||||||
|
|||||||
@@ -1,20 +1,18 @@
|
|||||||
{
|
{
|
||||||
"modelVersion": "5.0.0",
|
modelVersion: '5.0.0',
|
||||||
"description": "Please describe the basic information.",
|
description: 'Please describe the basic information.',
|
||||||
"dependencies": {
|
dependencies: {},
|
||||||
|
devDependencies: {
|
||||||
|
'@ohos/hypium': '1.0.19'
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
arkTs: {
|
||||||
"@ohos/hypium": "1.0.19",
|
compilerOptions: {
|
||||||
"@ohos/hamock": "1.0.0"
|
noImplicitAny: false,
|
||||||
|
suppressImplicitAnyIndexErrors: true,
|
||||||
|
strict: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"arkTs": {
|
overrides: {
|
||||||
"compilerOptions": {
|
'@rnoh/react-native-openharmony': '0.72.96',
|
||||||
"noImplicitAny": false,
|
|
||||||
"suppressImplicitAnyIndexErrors": true,
|
|
||||||
"strict": false
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"overrides": {
|
|
||||||
"@rnoh/react-native-openharmony": "0.72.38"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"start": "npm run codegen && hdc rport tcp:8081 tcp:8081 && react-native start",
|
"start": "npm run codegen && hdc rport tcp:8081 tcp:8081 && react-native start",
|
||||||
"codegen": "react-native codegen-harmony --rnoh-module-path ./harmony/react_native_openharmony",
|
"codegen": "react-native codegen-harmony --rnoh-module-path ./harmony/react_native_openharmony",
|
||||||
"build": "pushy bundle --platform harmony",
|
"build": "pushy bundle --platform harmony --no-interactive",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"hdiffFromPPK": "pushy hdiffFromPPK .pushy/output/harmony.1735052610653.ppk .pushy/output/harmony.1735052678646.ppk .pushy/output/hdiff.ppk-patch",
|
"hdiffFromPPK": "pushy hdiffFromPPK .pushy/output/harmony.1735052610653.ppk .pushy/output/harmony.1735052678646.ppk .pushy/output/hdiff.ppk-patch",
|
||||||
"hdiffFromApp": "pushy hdiffFromApp .pushy/output/version-1.0.0.app .pushy/output/harmony.1735052610653.ppk .pushy/output/hdiff.app-patch",
|
"hdiffFromApp": "pushy hdiffFromApp .pushy/output/version-1.0.0.app .pushy/output/harmony.1735052610653.ppk .pushy/output/hdiff.app-patch",
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
"@react-native-oh/react-native-harmony": "^0.72.59",
|
"@react-native-oh/react-native-harmony": "^0.72.59",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
"react-native": "0.72.5",
|
"react-native": "0.72.5",
|
||||||
"react-native-update": "latest"
|
"react-native-update": "^10.35.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.20.0",
|
"@babel/core": "^7.20.0",
|
||||||
@@ -40,4 +40,4 @@
|
|||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=16"
|
"node": ">=16"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
arrowParens: 'avoid',
|
arrowParens: 'avoid',
|
||||||
bracketSameLine: true,
|
|
||||||
bracketSpacing: false,
|
|
||||||
singleQuote: true,
|
singleQuote: true,
|
||||||
trailingComma: 'all',
|
trailingComma: 'all',
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ android {
|
|||||||
minSdkVersion rootProject.ext.minSdkVersion
|
minSdkVersion rootProject.ext.minSdkVersion
|
||||||
targetSdkVersion rootProject.ext.targetSdkVersion
|
targetSdkVersion rootProject.ext.targetSdkVersion
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.0"
|
versionName "1.81.4"
|
||||||
}
|
}
|
||||||
signingConfigs {
|
signingConfigs {
|
||||||
debug {
|
debug {
|
||||||
|
|||||||
@@ -5,13 +5,11 @@ import cn.reactnative.modules.update.UpdateContext
|
|||||||
import com.facebook.react.PackageList
|
import com.facebook.react.PackageList
|
||||||
import com.facebook.react.ReactApplication
|
import com.facebook.react.ReactApplication
|
||||||
import com.facebook.react.ReactHost
|
import com.facebook.react.ReactHost
|
||||||
|
import com.facebook.react.ReactNativeApplicationEntryPoint.loadReactNative
|
||||||
import com.facebook.react.ReactNativeHost
|
import com.facebook.react.ReactNativeHost
|
||||||
import com.facebook.react.ReactPackage
|
import com.facebook.react.ReactPackage
|
||||||
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
|
|
||||||
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
|
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
|
||||||
import com.facebook.react.defaults.DefaultReactNativeHost
|
import com.facebook.react.defaults.DefaultReactNativeHost
|
||||||
import com.facebook.react.soloader.OpenSourceMergedSoMapping
|
|
||||||
import com.facebook.soloader.SoLoader
|
|
||||||
|
|
||||||
class MainApplication : Application(), ReactApplication {
|
class MainApplication : Application(), ReactApplication {
|
||||||
|
|
||||||
@@ -39,10 +37,6 @@ class MainApplication : Application(), ReactApplication {
|
|||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
SoLoader.init(this, OpenSourceMergedSoMapping)
|
loadReactNative(this)
|
||||||
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
|
|
||||||
// If you opted-in for the New Architecture, we load the native entry point for this app.
|
|
||||||
load()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
ext {
|
ext {
|
||||||
buildToolsVersion = "35.0.0"
|
buildToolsVersion = "36.0.0"
|
||||||
minSdkVersion = 24
|
minSdkVersion = 24
|
||||||
compileSdkVersion = 35
|
compileSdkVersion = 36
|
||||||
targetSdkVersion = 35
|
targetSdkVersion = 36
|
||||||
ndkVersion = "27.1.12297006"
|
ndkVersion = "27.1.12297006"
|
||||||
kotlinVersion = "2.0.21"
|
kotlinVersion = "2.1.20"
|
||||||
}
|
}
|
||||||
repositories {
|
repositories {
|
||||||
google()
|
google()
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ android.useAndroidX=true
|
|||||||
# Use this property to specify which architecture you want to build.
|
# Use this property to specify which architecture you want to build.
|
||||||
# You can also override it from the CLI using
|
# You can also override it from the CLI using
|
||||||
# ./gradlew <task> -PreactNativeArchitectures=x86_64
|
# ./gradlew <task> -PreactNativeArchitectures=x86_64
|
||||||
reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
|
# reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
|
||||||
|
reactNativeArchitectures=arm64-v8a
|
||||||
|
|
||||||
# Use this property to enable support to the new architecture.
|
# Use this property to enable support to the new architecture.
|
||||||
# This will allow you to use TurboModules and the Fabric render in
|
# This will allow you to use TurboModules and the Fabric render in
|
||||||
|
|||||||
Binary file not shown.
@@ -1,6 +1,6 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
|
||||||
networkTimeout=10000
|
networkTimeout=10000
|
||||||
validateDistributionUrl=true
|
validateDistributionUrl=true
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
|||||||
8
Example/testHotUpdate/android/gradlew
vendored
8
Example/testHotUpdate/android/gradlew
vendored
@@ -114,7 +114,7 @@ case "$( uname )" in #(
|
|||||||
NONSTOP* ) nonstop=true ;;
|
NONSTOP* ) nonstop=true ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
CLASSPATH="\\\"\\\""
|
||||||
|
|
||||||
|
|
||||||
# Determine the Java command to use to start the JVM.
|
# Determine the Java command to use to start the JVM.
|
||||||
@@ -205,7 +205,7 @@ fi
|
|||||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
# Collect all arguments for the java command:
|
# Collect all arguments for the java command:
|
||||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
# and any embedded shellness will be escaped.
|
# and any embedded shellness will be escaped.
|
||||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
# treated as '${Hostname}' itself on the command line.
|
# treated as '${Hostname}' itself on the command line.
|
||||||
@@ -213,7 +213,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|||||||
set -- \
|
set -- \
|
||||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
-classpath "$CLASSPATH" \
|
-classpath "$CLASSPATH" \
|
||||||
org.gradle.wrapper.GradleWrapperMain \
|
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
|
||||||
"$@"
|
"$@"
|
||||||
|
|
||||||
# Stop when "xargs" is not available.
|
# Stop when "xargs" is not available.
|
||||||
@@ -248,4 +248,4 @@ eval "set -- $(
|
|||||||
tr '\n' ' '
|
tr '\n' ' '
|
||||||
)" '"$@"'
|
)" '"$@"'
|
||||||
|
|
||||||
exec "$JAVACMD" "$@"
|
exec "$JAVACMD" "$@"
|
||||||
11
Example/testHotUpdate/android/gradlew.bat
vendored
11
Example/testHotUpdate/android/gradlew.bat
vendored
@@ -1,3 +1,8 @@
|
|||||||
|
@REM Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||||
|
@REM
|
||||||
|
@REM This source code is licensed under the MIT license found in the
|
||||||
|
@REM LICENSE file in the root directory of this source tree.
|
||||||
|
|
||||||
@rem
|
@rem
|
||||||
@rem Copyright 2015 the original author or authors.
|
@rem Copyright 2015 the original author or authors.
|
||||||
@rem
|
@rem
|
||||||
@@ -70,11 +75,11 @@ goto fail
|
|||||||
:execute
|
:execute
|
||||||
@rem Setup the command line
|
@rem Setup the command line
|
||||||
|
|
||||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
set CLASSPATH=
|
||||||
|
|
||||||
|
|
||||||
@rem Execute Gradle
|
@rem Execute Gradle
|
||||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
|
||||||
|
|
||||||
:end
|
:end
|
||||||
@rem End local scope for the variables with windows NT shell
|
@rem End local scope for the variables with windows NT shell
|
||||||
@@ -91,4 +96,4 @@ exit /b %EXIT_CODE%
|
|||||||
:mainEnd
|
:mainEnd
|
||||||
if "%OS%"=="Windows_NT" endlocal
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
:omega
|
:omega
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -403,6 +403,8 @@
|
|||||||
"-DFOLLY_NO_CONFIG",
|
"-DFOLLY_NO_CONFIG",
|
||||||
"-DFOLLY_MOBILE=1",
|
"-DFOLLY_MOBILE=1",
|
||||||
"-DFOLLY_USE_LIBCPP=1",
|
"-DFOLLY_USE_LIBCPP=1",
|
||||||
|
"-DFOLLY_CFG_NO_COROUTINES=1",
|
||||||
|
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
|
||||||
);
|
);
|
||||||
OTHER_LDFLAGS = "$(inherited)";
|
OTHER_LDFLAGS = "$(inherited)";
|
||||||
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
|
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
|
||||||
@@ -475,6 +477,8 @@
|
|||||||
"-DFOLLY_NO_CONFIG",
|
"-DFOLLY_NO_CONFIG",
|
||||||
"-DFOLLY_MOBILE=1",
|
"-DFOLLY_MOBILE=1",
|
||||||
"-DFOLLY_USE_LIBCPP=1",
|
"-DFOLLY_USE_LIBCPP=1",
|
||||||
|
"-DFOLLY_CFG_NO_COROUTINES=1",
|
||||||
|
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
|
||||||
);
|
);
|
||||||
OTHER_LDFLAGS = "$(inherited)";
|
OTHER_LDFLAGS = "$(inherited)";
|
||||||
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
|
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
|
||||||
|
|||||||
@@ -37,8 +37,14 @@
|
|||||||
</dict>
|
</dict>
|
||||||
</dict>
|
</dict>
|
||||||
</dict>
|
</dict>
|
||||||
|
<key>NSCameraUsageDescription</key>
|
||||||
|
<string>For taking photos</string>
|
||||||
<key>NSLocationWhenInUseUsageDescription</key>
|
<key>NSLocationWhenInUseUsageDescription</key>
|
||||||
<string></string>
|
<string></string>
|
||||||
|
<key>NSPhotoLibraryUsageDescription</key>
|
||||||
|
<string>For saving photos</string>
|
||||||
|
<key>RCTNewArchEnabled</key>
|
||||||
|
<true/>
|
||||||
<key>UILaunchStoryboardName</key>
|
<key>UILaunchStoryboardName</key>
|
||||||
<string>LaunchScreen</string>
|
<string>LaunchScreen</string>
|
||||||
<key>UIRequiredDeviceCapabilities</key>
|
<key>UIRequiredDeviceCapabilities</key>
|
||||||
@@ -53,10 +59,5 @@
|
|||||||
</array>
|
</array>
|
||||||
<key>UIViewControllerBasedStatusBarAppearance</key>
|
<key>UIViewControllerBasedStatusBarAppearance</key>
|
||||||
<false/>
|
<false/>
|
||||||
<key>NSCameraUsageDescription</key>
|
|
||||||
<string>For taking photos</string>
|
|
||||||
|
|
||||||
<key>NSPhotoLibraryUsageDescription</key>
|
|
||||||
<string>For saving photos</string>
|
|
||||||
</dict>
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -10,43 +10,42 @@
|
|||||||
"test:e2e": "detox test --configuration android.emu.debug",
|
"test:e2e": "detox test --configuration android.emu.debug",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"postinstall": "patch-package",
|
"postinstall": "patch-package",
|
||||||
"apk": "cd android && ./gradlew assembleRelease",
|
"apk": "cd android && ./gradlew assembleRelease"
|
||||||
"dev:harmony": "react-native bundle-harmony --dev"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"form-data": "^4.0.3",
|
"form-data": "^4.0.4",
|
||||||
"patch-package": "^8.0.0",
|
"patch-package": "^8.0.0",
|
||||||
"react": "19.0.0",
|
"react": "19.1.0",
|
||||||
"react-native": "0.79.2",
|
"react-native": "0.81.4",
|
||||||
"react-native-camera-kit": "^15.1.0",
|
"react-native-camera-kit": "^16.1.2",
|
||||||
"react-native-paper": "^5.14.5",
|
"react-native-paper": "^5.14.5",
|
||||||
"react-native-safe-area-context": "^5.5.0",
|
"react-native-safe-area-context": "^5.6.1",
|
||||||
"react-native-svg": "^15.12.0",
|
"react-native-svg": "^15.13.0",
|
||||||
"react-native-update": "^10.29.4",
|
"react-native-update": "^10.35.6",
|
||||||
"react-native-vector-icons": "^10.2.0"
|
"react-native-vector-icons": "^10.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.27.3",
|
"@babel/core": "^7.27.3",
|
||||||
"@babel/preset-env": "^7.27.2",
|
"@babel/preset-env": "^7.27.2",
|
||||||
"@babel/runtime": "^7.27.3",
|
"@babel/runtime": "^7.27.3",
|
||||||
"@react-native-community/cli": "18.0.0",
|
"@react-native-community/cli": "20.0.0",
|
||||||
"@react-native-community/cli-platform-android": "18.0.0",
|
"@react-native-community/cli-platform-android": "20.0.0",
|
||||||
"@react-native-community/cli-platform-ios": "18.0.0",
|
"@react-native-community/cli-platform-ios": "20.0.0",
|
||||||
"@react-native/babel-preset": "0.79.2",
|
"@react-native/babel-preset": "0.81.4",
|
||||||
"@react-native/eslint-config": "0.79.2",
|
"@react-native/eslint-config": "0.81.4",
|
||||||
"@react-native/metro-config": "0.79.2",
|
"@react-native/metro-config": "0.81.4",
|
||||||
"@react-native/typescript-config": "0.79.2",
|
"@react-native/typescript-config": "0.81.4",
|
||||||
"@types/react": "^19.0.0",
|
"@types/react": "^19.1.13",
|
||||||
"@types/react-test-renderer": "^19.0.0",
|
"@types/react-test-renderer": "^19.1.0",
|
||||||
"detox": "^20.39.0",
|
"detox": "^20.41.2",
|
||||||
"eslint": "^8.19.0",
|
"eslint": "^9.35.0",
|
||||||
"jest": "^29.6.3",
|
"jest": "^29.6.3",
|
||||||
"prettier": "2.8.8",
|
"prettier": "2.8.8",
|
||||||
"react-test-renderer": "19.0.0",
|
"react-test-renderer": "19.1.1",
|
||||||
"typescript": "5.8.3"
|
"typescript": "5.8.3"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=18"
|
"node": ">=20"
|
||||||
},
|
},
|
||||||
"trustedDependencies": [
|
"trustedDependencies": [
|
||||||
"detox",
|
"detox",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/* eslint-disable react/no-unstable-nested-components */
|
/* eslint-disable react/no-unstable-nested-components */
|
||||||
/* eslint-disable react-native/no-inline-styles */
|
/* eslint-disable react-native/no-inline-styles */
|
||||||
import React, {useRef, useState} from 'react';
|
import React, { useRef, useState } from 'react';
|
||||||
import {
|
import {
|
||||||
StyleSheet,
|
StyleSheet,
|
||||||
Platform,
|
Platform,
|
||||||
@@ -19,14 +19,14 @@ import {
|
|||||||
Modal,
|
Modal,
|
||||||
Portal,
|
Portal,
|
||||||
} from 'react-native-paper';
|
} from 'react-native-paper';
|
||||||
import {Camera} from 'react-native-camera-kit';
|
import { Camera } from 'react-native-camera-kit';
|
||||||
import {LocalSvg} from 'react-native-svg/css';
|
import { LocalSvg } from 'react-native-svg/css';
|
||||||
|
|
||||||
import TestConsole from './TestConsole';
|
import TestConsole from './TestConsole';
|
||||||
|
|
||||||
import _updateConfig from '../update.json';
|
import _updateConfig from '../update.json';
|
||||||
import {UpdateProvider, Pushy, Cresc, useUpdate} from 'react-native-update';
|
import { UpdateProvider, Pushy, useUpdate } from 'react-native-update';
|
||||||
const {appKey} = _updateConfig[Platform.OS];
|
const { appKey } = _updateConfig[Platform.OS];
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const {
|
const {
|
||||||
@@ -39,7 +39,8 @@ function App() {
|
|||||||
packageVersion,
|
packageVersion,
|
||||||
currentHash,
|
currentHash,
|
||||||
parseTestQrCode,
|
parseTestQrCode,
|
||||||
progress: {received, total} = {},
|
progress: { received, total } = {},
|
||||||
|
currentVersionInfo,
|
||||||
} = useUpdate();
|
} = useUpdate();
|
||||||
const [useDefaultAlert, setUseDefaultAlert] = useState(true);
|
const [useDefaultAlert, setUseDefaultAlert] = useState(true);
|
||||||
const [showTestConsole, setShowTestConsole] = useState(false);
|
const [showTestConsole, setShowTestConsole] = useState(false);
|
||||||
@@ -52,8 +53,8 @@ function App() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<Text style={styles.welcome}>欢迎xxx使用Pushy热更新服务</Text>
|
<Text style={styles.welcome}>欢迎使用Pushy热更新服务</Text>
|
||||||
<View style={{flexDirection: 'row'}}>
|
<View style={{ flexDirection: 'row' }}>
|
||||||
<Text>
|
<Text>
|
||||||
{useDefaultAlert ? '当前使用' : '当前不使用'}默认的alert更新提示
|
{useDefaultAlert ? '当前使用' : '当前不使用'}默认的alert更新提示
|
||||||
</Text>
|
</Text>
|
||||||
@@ -72,9 +73,9 @@ function App() {
|
|||||||
<Portal>
|
<Portal>
|
||||||
<Modal visible={showCamera} onDismiss={() => setShowCamera(false)}>
|
<Modal visible={showCamera} onDismiss={() => setShowCamera(false)}>
|
||||||
<Camera
|
<Camera
|
||||||
style={{minHeight: 320}}
|
style={{ minHeight: 320 }}
|
||||||
scanBarcode={true}
|
scanBarcode={true}
|
||||||
onReadCode={({nativeEvent: {codeStringValue}}) => {
|
onReadCode={({ nativeEvent: { codeStringValue } }) => {
|
||||||
// 防止重复扫码
|
// 防止重复扫码
|
||||||
if (lastParsedCode.current === codeStringValue) {
|
if (lastParsedCode.current === codeStringValue) {
|
||||||
return;
|
return;
|
||||||
@@ -92,7 +93,7 @@ function App() {
|
|||||||
/>
|
/>
|
||||||
</Modal>
|
</Modal>
|
||||||
</Portal>
|
</Portal>
|
||||||
<View style={{flexDirection: 'row', alignItems: 'center', gap: 10}}>
|
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 10 }}>
|
||||||
<Text>png:</Text>
|
<Text>png:</Text>
|
||||||
<Image
|
<Image
|
||||||
resizeMode={'contain'}
|
resizeMode={'contain'}
|
||||||
@@ -100,11 +101,11 @@ function App() {
|
|||||||
style={styles.image}
|
style={styles.image}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
<View style={{flexDirection: 'row', alignItems: 'center', gap: 10}}>
|
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 10 }}>
|
||||||
<Text>svg:</Text>
|
<Text>svg:</Text>
|
||||||
<LocalSvg
|
<LocalSvg
|
||||||
asset={require('./assets/react-logo.svg')}
|
asset={require('./assets/react-logo.svg')}
|
||||||
style={{width: 30, height: 30}}
|
style={{ width: 30, height: 30 }}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
<Text style={styles.instructions}>
|
<Text style={styles.instructions}>
|
||||||
@@ -113,6 +114,7 @@ function App() {
|
|||||||
{'\n'}
|
{'\n'}
|
||||||
当前热更新版本Hash: {currentHash || '(空)'}
|
当前热更新版本Hash: {currentHash || '(空)'}
|
||||||
{'\n'}
|
{'\n'}
|
||||||
|
当前热更新版本信息: {JSON.stringify(currentVersionInfo) || '(空)'}
|
||||||
</Text>
|
</Text>
|
||||||
<Text>
|
<Text>
|
||||||
下载进度:{received} / {total}
|
下载进度:{received} / {total}
|
||||||
@@ -121,16 +123,18 @@ function App() {
|
|||||||
onPress={() => {
|
onPress={() => {
|
||||||
checkUpdate();
|
checkUpdate();
|
||||||
setShowUpdateSnackbar(true);
|
setShowUpdateSnackbar(true);
|
||||||
}}>
|
}}
|
||||||
|
>
|
||||||
<Text style={styles.instructions}>点击这里检查更新</Text>
|
<Text style={styles.instructions}>点击这里检查更新</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
|
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
testID="testcase"
|
testID="testcase"
|
||||||
style={{marginTop: 15}}
|
style={{ marginTop: 15 }}
|
||||||
onLongPress={() => {
|
onLongPress={() => {
|
||||||
setShowTestConsole(true);
|
setShowTestConsole(true);
|
||||||
}}>
|
}}
|
||||||
|
>
|
||||||
<Text style={styles.instructions}>
|
<Text style={styles.instructions}>
|
||||||
react-native-update版本:{client?.version}
|
react-native-update版本:{client?.version}
|
||||||
</Text>
|
</Text>
|
||||||
@@ -152,14 +156,15 @@ function App() {
|
|||||||
await downloadUpdate();
|
await downloadUpdate();
|
||||||
setShowUpdateBanner(true);
|
setShowUpdateBanner(true);
|
||||||
},
|
},
|
||||||
}}>
|
}}
|
||||||
<Text style={{color: 'white'}}>
|
>
|
||||||
|
<Text style={{ color: 'white' }}>
|
||||||
有新版本({updateInfo.name})可用,是否更新?
|
有新版本({updateInfo.name})可用,是否更新?
|
||||||
</Text>
|
</Text>
|
||||||
</Snackbar>
|
</Snackbar>
|
||||||
)}
|
)}
|
||||||
<Banner
|
<Banner
|
||||||
style={{width: '100%', position: 'absolute', top: 0}}
|
style={{ width: '100%', position: 'absolute', top: 0 }}
|
||||||
visible={showUpdateBanner}
|
visible={showUpdateBanner}
|
||||||
actions={[
|
actions={[
|
||||||
{
|
{
|
||||||
@@ -174,9 +179,10 @@ function App() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
icon={({size}) => (
|
icon={({ size }) => (
|
||||||
<Icon name="checkcircleo" size={size} color="#00f" />
|
<Icon name="checkcircleo" size={size} color="#00f" />
|
||||||
)}>
|
)}
|
||||||
|
>
|
||||||
更新已完成,是否立即重启?
|
更新已完成,是否立即重启?
|
||||||
</Banner>
|
</Banner>
|
||||||
</View>
|
</View>
|
||||||
@@ -224,4 +230,4 @@ export default function Root() {
|
|||||||
</PaperProvider>
|
</PaperProvider>
|
||||||
</UpdateProvider>
|
</UpdateProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
5
Example/testHotUpdate/tsconfig.json
Normal file
5
Example/testHotUpdate/tsconfig.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"extends": "@react-native/typescript-config",
|
||||||
|
"include": ["**/*.ts", "**/*.tsx"],
|
||||||
|
"exclude": ["**/node_modules", "**/Pods"]
|
||||||
|
}
|
||||||
27
README.md
27
README.md
@@ -19,6 +19,33 @@
|
|||||||
7. meta 信息及开放 API,提供更高扩展性。
|
7. meta 信息及开放 API,提供更高扩展性。
|
||||||
8. 提供付费的专人技术支持。
|
8. 提供付费的专人技术支持。
|
||||||
|
|
||||||
|
### 与其他热更新库对比
|
||||||
|
|
||||||
|
| 对比维度 | react-native-update | expo-update | react-native-code-push |
|
||||||
|
|---------|---------------------|-------------|------------------------|
|
||||||
|
| **价格/成本** | 提供免费额度,多级梯度付费(最低约 66 元/月),流量不单独计费 | 提供免费额度,多级梯度付费(最低约 136 元/月),超出流量额外计费 | ❌ **已停运**(Microsoft App Center 已于 2025 年 3 月 31 日停止服务) |
|
||||||
|
| **更新包大小** | ⭐⭐⭐⭐⭐ 几十 KB(增量更新) | ⭐⭐⭐ 全量更新(通常几十 MB) | ❌ **已停运** |
|
||||||
|
| **中国地区访问速度** | ⭐⭐⭐⭐⭐ 阿里云 CDN,速度极快 | ⭐⭐ 国外服务器,可能较慢 | ❌ **已停运** |
|
||||||
|
| **iOS 支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
|
||||||
|
| **Android 支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
|
||||||
|
| **鸿蒙支持** | ✅ 支持 | ❌ 不支持 | ❌ **已停运** |
|
||||||
|
| **Expo 支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
|
||||||
|
| **RN 版本支持** | ⭐⭐⭐⭐⭐ 第一时间支持最新版本 | ⭐⭐⭐⭐ 跟随 Expo SDK | ❌ **已停运** |
|
||||||
|
| **新架构支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
|
||||||
|
| **Hermes 支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
|
||||||
|
| **崩溃回滚** | ✅ 自动回滚机制 | ✅ 支持 | ❌ **已停运** |
|
||||||
|
| **管理界面** | ✅ 命令行工具 + Web 管理界面 | ✅ Expo Dashboard | ❌ **已停运** |
|
||||||
|
| **CI/CD 集成** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
|
||||||
|
| **API 扩展性** | ✅ Meta 信息 + 开放 API | ⚠️ 有限 | ❌ **已停运** |
|
||||||
|
| **中文文档/支持** | ⭐⭐⭐⭐⭐ 完整中文文档,中文社区支持 | ⭐⭐ 英文为主 | ❌ **已停运** |
|
||||||
|
| **技术支持** | ✅ 付费专人技术支持 | ⚠️ 社区支持 | ❌ **已停运** |
|
||||||
|
| **服务器部署** | ✅ 可托管也可付费私有部署 | ✅ Expo 托管(EAS Update) | ❌ **已停运** |
|
||||||
|
| **更新策略** | 灵活配置(静默/提示/立即/延迟) | 相对固定 | ❌ **已停运** |
|
||||||
|
| **流量消耗** | ⭐⭐⭐⭐⭐ 极低(增量更新) | ⭐⭐⭐ 较高(全量更新) | ❌ **已停运** |
|
||||||
|
| **更新成功率** | ⭐⭐⭐⭐⭐ 极高(国内 CDN 优势) | ⭐⭐⭐ 中等 | ❌ **已停运** |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 本地开发
|
### 本地开发
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ android {
|
|||||||
minSdkVersion safeExtGet('minSdkVersion', 16)
|
minSdkVersion safeExtGet('minSdkVersion', 16)
|
||||||
targetSdkVersion safeExtGet('targetSdkVersion', 27)
|
targetSdkVersion safeExtGet('targetSdkVersion', 27)
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.0"
|
versionName "1.81.4"
|
||||||
consumerProguardFiles "proguard.pro"
|
consumerProguardFiles "proguard.pro"
|
||||||
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
27
android/proguard.pro
vendored
27
android/proguard.pro
vendored
@@ -10,28 +10,9 @@
|
|||||||
-keepnames class com.facebook.react.devsupport.** { *; }
|
-keepnames class com.facebook.react.devsupport.** { *; }
|
||||||
|
|
||||||
# Keep fields used in reflection
|
# Keep fields used in reflection
|
||||||
-keepclassmembers class com.facebook.react.ReactInstanceManager {
|
-keepclassmembers class com.facebook.react.ReactActivity { *; }
|
||||||
private JSBundleLoader mBundleLoader;
|
-keepclassmembers class com.facebook.react.ReactInstanceManager { *; }
|
||||||
private String mJSBundleFile;
|
-keepclassmembers class com.facebook.react.ReactDelegate { *; }
|
||||||
}
|
-keepclassmembers class com.facebook.react.ReactHost { *; }
|
||||||
|
|
||||||
-keepclassmembers class com.facebook.react.ReactDelegate {
|
|
||||||
private ReactHost mReactHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
-keepclassmembers class com.facebook.react.ReactHost {
|
|
||||||
private boolean mUseDevSupport;
|
|
||||||
private ReactHostDelegate mReactHostDelegate;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Keep Expo related classes
|
|
||||||
-keepnames class expo.modules.ExpoReactHostFactory$ExpoReactHostDelegate { *; }
|
-keepnames class expo.modules.ExpoReactHostFactory$ExpoReactHostDelegate { *; }
|
||||||
|
|
||||||
# Keep methods used in reflection
|
|
||||||
-keepclassmembers class com.facebook.react.ReactActivity {
|
|
||||||
public ReactDelegate getReactDelegate();
|
|
||||||
}
|
|
||||||
|
|
||||||
-keepclassmembers class com.facebook.react.ReactHost {
|
|
||||||
public void reload(java.lang.String);
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
package cn.reactnative.modules.update;
|
package cn.reactnative.modules.update;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Build;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
import com.facebook.react.bridge.WritableMap;
|
import com.facebook.react.bridge.WritableMap;
|
||||||
@@ -97,10 +100,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
while ((bytesRead = source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE)) != -1) {
|
while ((bytesRead = source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE)) != -1) {
|
||||||
received += bytesRead;
|
received += bytesRead;
|
||||||
sink.emit();
|
sink.emit();
|
||||||
if (UpdateContext.DEBUG) {
|
|
||||||
Log.d("react-native-update", "Progress " + received + "/" + contentLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
int percentage = (int)(received * 100.0 / contentLength + 0.5);
|
int percentage = (int)(received * 100.0 / contentLength + 0.5);
|
||||||
if (percentage > currentPercentage) {
|
if (percentage > currentPercentage) {
|
||||||
currentPercentage = percentage;
|
currentPercentage = percentage;
|
||||||
@@ -247,30 +247,209 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copyFromResource(HashMap<String, ArrayList<File> > resToCopy) throws IOException {
|
private String findDrawableFallback(String originalToPath, HashMap<String, String> copiesMap, HashMap<String, ZipEntry> availableEntries) {
|
||||||
SafeZipFile zipFile = new SafeZipFile(new File(context.getPackageResourcePath()));
|
// 检查是否是 drawable 路径
|
||||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
if (!originalToPath.contains("drawable")) {
|
||||||
while (entries.hasMoreElements()) {
|
return null;
|
||||||
ZipEntry ze = entries.nextElement();
|
}
|
||||||
|
|
||||||
String fn = ze.getName();
|
// 提取文件名(路径的最后部分)
|
||||||
ArrayList<File> targets = resToCopy.get(fn);
|
int lastSlash = originalToPath.lastIndexOf('/');
|
||||||
if (targets != null) {
|
if (lastSlash == -1) {
|
||||||
File lastTarget = null;
|
return null;
|
||||||
for (File target: targets) {
|
}
|
||||||
|
String fileName = originalToPath.substring(lastSlash + 1);
|
||||||
|
|
||||||
|
// 定义密度优先级(从高到低)
|
||||||
|
String[] densities = {"xxxhdpi", "xxhdpi", "xhdpi", "hdpi", "mdpi", "ldpi"};
|
||||||
|
|
||||||
|
// 尝试找到相同文件名但不同密度的 key
|
||||||
|
for (String density : densities) {
|
||||||
|
// 构建可能的 key 路径(替换密度部分)
|
||||||
|
String fallbackToPath = originalToPath.replaceFirst("drawable-[^/]+", "drawable-" + density);
|
||||||
|
|
||||||
|
// 检查这个 key 是否在 copies 映射中
|
||||||
|
if (copiesMap.containsKey(fallbackToPath)) {
|
||||||
|
String fallbackFromPath = copiesMap.get(fallbackToPath);
|
||||||
|
// 检查对应的 value 路径是否在 APK 中存在
|
||||||
|
if (availableEntries.containsKey(fallbackFromPath)) {
|
||||||
if (UpdateContext.DEBUG) {
|
if (UpdateContext.DEBUG) {
|
||||||
Log.d("react-native-update", "Copying from resource " + fn + " to " + target);
|
Log.d("react-native-update", "Found fallback for " + originalToPath + ": " + fallbackToPath + " -> " + fallbackFromPath);
|
||||||
}
|
|
||||||
if (lastTarget != null) {
|
|
||||||
copyFile(lastTarget, target);
|
|
||||||
} else {
|
|
||||||
zipFile.unzipToFile(ze, target);
|
|
||||||
lastTarget = target;
|
|
||||||
}
|
}
|
||||||
|
return fallbackFromPath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
zipFile.close();
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyFromResource(HashMap<String, ArrayList<File> > resToCopy, HashMap<String, String> copiesMap) throws IOException {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("react-native-update", "copyFromResource called, resToCopy size: " + resToCopy.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 收集所有 APK 路径(包括基础 APK 和所有 split APK)
|
||||||
|
ArrayList<String> apkPaths = new ArrayList<>();
|
||||||
|
apkPaths.add(context.getPackageResourcePath());
|
||||||
|
|
||||||
|
// 获取所有 split APK 路径(用于资源分割的情况)
|
||||||
|
try {
|
||||||
|
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
|
||||||
|
context.getPackageName(), 0);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && appInfo.splitSourceDirs != null) {
|
||||||
|
for (String splitPath : appInfo.splitSourceDirs) {
|
||||||
|
apkPaths.add(splitPath);
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("react-native-update", "Found split APK: " + splitPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.w("react-native-update", "Failed to get application info: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第一遍:从所有 APK 中收集所有可用的 zip 条目
|
||||||
|
HashMap<String, ZipEntry> availableEntries = new HashMap<>();
|
||||||
|
HashMap<String, SafeZipFile> zipFileMap = new HashMap<>(); // 保存每个路径对应的 ZipFile
|
||||||
|
HashMap<String, SafeZipFile> entryToZipFileMap = new HashMap<>(); // 保存每个条目对应的 ZipFile
|
||||||
|
|
||||||
|
for (String apkPath : apkPaths) {
|
||||||
|
SafeZipFile zipFile = new SafeZipFile(new File(apkPath));
|
||||||
|
zipFileMap.put(apkPath, zipFile);
|
||||||
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
ZipEntry ze = entries.nextElement();
|
||||||
|
String entryName = ze.getName();
|
||||||
|
// 如果条目已存在,保留第一个(基础 APK 优先)
|
||||||
|
if (!availableEntries.containsKey(entryName)) {
|
||||||
|
availableEntries.put(entryName, ze);
|
||||||
|
entryToZipFileMap.put(entryName, zipFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用基础 APK 的 ZipFile 作为主要操作对象
|
||||||
|
SafeZipFile zipFile = zipFileMap.get(context.getPackageResourcePath());
|
||||||
|
|
||||||
|
// 处理所有需要复制的文件
|
||||||
|
HashMap<String, ArrayList<File>> remainingFiles = new HashMap<>(resToCopy);
|
||||||
|
|
||||||
|
for (String fromPath : new ArrayList<>(remainingFiles.keySet())) {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("react-native-update", "Processing fromPath: " + fromPath);
|
||||||
|
}
|
||||||
|
ArrayList<File> targets = remainingFiles.get(fromPath);
|
||||||
|
if (targets == null || targets.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ZipEntry ze = availableEntries.get(fromPath);
|
||||||
|
String actualSourcePath = fromPath;
|
||||||
|
|
||||||
|
// 如果文件不存在,尝试 fallback
|
||||||
|
if (ze == null) {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("react-native-update", "File not found in APK: " + fromPath + ", trying fallback");
|
||||||
|
}
|
||||||
|
// 找到对应的 to 路径(从 copiesMap 的反向查找)
|
||||||
|
String toPath = null;
|
||||||
|
for (String to : copiesMap.keySet()) {
|
||||||
|
if (copiesMap.get(to).equals(fromPath)) {
|
||||||
|
toPath = to;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toPath != null) {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("react-native-update", "Found toPath: " + toPath + " for fromPath: " + fromPath);
|
||||||
|
}
|
||||||
|
String fallbackFromPath = findDrawableFallback(toPath, copiesMap, availableEntries);
|
||||||
|
if (fallbackFromPath != null) {
|
||||||
|
ze = availableEntries.get(fallbackFromPath);
|
||||||
|
actualSourcePath = fallbackFromPath;
|
||||||
|
// 确保 fallback 路径也在 entryToZipFileMap 中
|
||||||
|
if (!entryToZipFileMap.containsKey(fallbackFromPath)) {
|
||||||
|
// 查找包含该 fallback 路径的 ZipFile
|
||||||
|
for (String apkPath : apkPaths) {
|
||||||
|
SafeZipFile testZipFile = zipFileMap.get(apkPath);
|
||||||
|
if (testZipFile != null) {
|
||||||
|
try {
|
||||||
|
ZipEntry testEntry = testZipFile.getEntry(fallbackFromPath);
|
||||||
|
if (testEntry != null) {
|
||||||
|
entryToZipFileMap.put(fallbackFromPath, testZipFile);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 继续查找
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.w("react-native-update", "Using fallback: " + fallbackFromPath + " for " + fromPath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.w("react-native-update", "No fallback found for: " + fromPath + " (toPath: " + toPath + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.w("react-native-update", "No toPath found for fromPath: " + fromPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ze != null) {
|
||||||
|
File lastTarget = null;
|
||||||
|
for (File target: targets) {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("react-native-update", "Copying from resource " + actualSourcePath + " to " + target);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// 确保目标文件的父目录存在
|
||||||
|
File parentDir = target.getParentFile();
|
||||||
|
if (parentDir != null && !parentDir.exists()) {
|
||||||
|
parentDir.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastTarget != null) {
|
||||||
|
copyFile(lastTarget, target);
|
||||||
|
} else {
|
||||||
|
// 从保存的映射中获取包含该条目的 ZipFile
|
||||||
|
SafeZipFile sourceZipFile = entryToZipFileMap.get(actualSourcePath);
|
||||||
|
if (sourceZipFile == null) {
|
||||||
|
sourceZipFile = zipFile; // 回退到基础 APK
|
||||||
|
}
|
||||||
|
sourceZipFile.unzipToFile(ze, target);
|
||||||
|
lastTarget = target;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.w("react-native-update", "Failed to copy resource " + actualSourcePath + " to " + target + ": " + e.getMessage());
|
||||||
|
}
|
||||||
|
// 继续处理下一个目标
|
||||||
|
}
|
||||||
|
}
|
||||||
|
remainingFiles.remove(fromPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理剩余的文件(如果还有的话)
|
||||||
|
if (!remainingFiles.isEmpty() && UpdateContext.DEBUG) {
|
||||||
|
for (String fromPath : remainingFiles.keySet()) {
|
||||||
|
Log.w("react-native-update", "Resource not found and no fallback available: " + fromPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关闭所有 ZipFile
|
||||||
|
for (SafeZipFile zf : zipFileMap.values()) {
|
||||||
|
zf.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doPatchFromApk(DownloadTaskParams param) throws IOException, JSONException {
|
private void doPatchFromApk(DownloadTaskParams param) throws IOException, JSONException {
|
||||||
@@ -279,6 +458,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
removeDirectory(param.unzipDirectory);
|
removeDirectory(param.unzipDirectory);
|
||||||
param.unzipDirectory.mkdirs();
|
param.unzipDirectory.mkdirs();
|
||||||
HashMap<String, ArrayList<File>> copyList = new HashMap<String, ArrayList<File>>();
|
HashMap<String, ArrayList<File>> copyList = new HashMap<String, ArrayList<File>>();
|
||||||
|
HashMap<String, String> copiesMap = new HashMap<String, String>(); // to -> from 映射
|
||||||
|
|
||||||
boolean foundDiff = false;
|
boolean foundDiff = false;
|
||||||
boolean foundBundlePatch = false;
|
boolean foundBundlePatch = false;
|
||||||
@@ -304,6 +484,9 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
if (from.isEmpty()) {
|
if (from.isEmpty()) {
|
||||||
from = to;
|
from = to;
|
||||||
}
|
}
|
||||||
|
// 保存 copies 映射关系(to -> from)
|
||||||
|
copiesMap.put(to, from);
|
||||||
|
|
||||||
ArrayList<File> target = null;
|
ArrayList<File> target = null;
|
||||||
if (!copyList.containsKey(from)) {
|
if (!copyList.containsKey(from)) {
|
||||||
target = new ArrayList<File>();
|
target = new ArrayList<File>();
|
||||||
@@ -348,7 +531,14 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
|||||||
throw new Error("bundle patch not found");
|
throw new Error("bundle patch not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
copyFromResource(copyList);
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("react-native-update", "copyList size: " + copyList.size() + ", copiesMap size: " + copiesMap.size());
|
||||||
|
for (String from : copyList.keySet()) {
|
||||||
|
Log.d("react-native-update", "copyList entry: " + from + " -> " + copyList.get(from).size() + " targets");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
copyFromResource(copyList, copiesMap);
|
||||||
|
|
||||||
if (UpdateContext.DEBUG) {
|
if (UpdateContext.DEBUG) {
|
||||||
Log.d("react-native-update", "Unzip finished");
|
Log.d("react-native-update", "Unzip finished");
|
||||||
|
|||||||
@@ -66,9 +66,8 @@ public class SafeZipFile extends ZipFile {
|
|||||||
throw new SecurityException("Illegal name: " + name);
|
throw new SecurityException("Illegal name: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (UpdateContext.DEBUG) {
|
|
||||||
Log.d("RNUpdate", "Unzipping " + name);
|
Log.d("react-native-update", "Unzipping " + name);
|
||||||
}
|
|
||||||
|
|
||||||
if (ze.isDirectory()) {
|
if (ze.isDirectory()) {
|
||||||
target.mkdirs();
|
target.mkdirs();
|
||||||
|
|||||||
@@ -19,9 +19,13 @@ public class UpdateContext {
|
|||||||
private File rootDir;
|
private File rootDir;
|
||||||
private Executor executor;
|
private Executor executor;
|
||||||
|
|
||||||
public static boolean DEBUG = false;
|
public static boolean DEBUG = true;
|
||||||
private static ReactInstanceManager mReactInstanceManager;
|
private static ReactInstanceManager mReactInstanceManager;
|
||||||
private static boolean isUsingBundleUrl = false;
|
private static boolean isUsingBundleUrl = false;
|
||||||
|
|
||||||
|
// Singleton instance
|
||||||
|
private static UpdateContext sInstance;
|
||||||
|
private static final Object sLock = new Object();
|
||||||
|
|
||||||
public UpdateContext(Context context) {
|
public UpdateContext(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
@@ -36,13 +40,35 @@ public class UpdateContext {
|
|||||||
this.sp = context.getSharedPreferences("update", Context.MODE_PRIVATE);
|
this.sp = context.getSharedPreferences("update", Context.MODE_PRIVATE);
|
||||||
|
|
||||||
String packageVersion = getPackageVersion();
|
String packageVersion = getPackageVersion();
|
||||||
if (!packageVersion.equals(this.sp.getString("packageVersion", null))) {
|
String buildTime = getBuildTime();
|
||||||
|
String storedPackageVersion = this.sp.getString("packageVersion", null);
|
||||||
|
String storedBuildTime = this.sp.getString("buildTime", null);
|
||||||
|
|
||||||
|
// If stored versions don't exist, write current versions first
|
||||||
|
if (storedPackageVersion == null || storedBuildTime == null) {
|
||||||
|
SharedPreferences.Editor editor = sp.edit();
|
||||||
|
editor.putString("packageVersion", packageVersion);
|
||||||
|
editor.putString("buildTime", buildTime);
|
||||||
|
editor.apply();
|
||||||
|
storedPackageVersion = packageVersion;
|
||||||
|
storedBuildTime = buildTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean packageVersionChanged = !packageVersion.equals(storedPackageVersion);
|
||||||
|
boolean buildTimeChanged = !buildTime.equals(storedBuildTime);
|
||||||
|
|
||||||
|
if (packageVersionChanged || buildTimeChanged) {
|
||||||
|
// Execute cleanUp before clearing SharedPreferences to avoid race condition
|
||||||
|
this.cleanUp();
|
||||||
|
|
||||||
SharedPreferences.Editor editor = sp.edit();
|
SharedPreferences.Editor editor = sp.edit();
|
||||||
editor.clear();
|
editor.clear();
|
||||||
editor.putString("packageVersion", packageVersion);
|
editor.putString("packageVersion", packageVersion);
|
||||||
editor.apply();
|
editor.putString("buildTime", buildTime);
|
||||||
|
// Use commit() instead of apply() to ensure synchronous write completion
|
||||||
this.cleanUp();
|
// This prevents race condition where getBundleUrl() might read null values
|
||||||
|
// if called before apply() completes
|
||||||
|
editor.commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,12 +235,26 @@ public class UpdateContext {
|
|||||||
return mReactInstanceManager;
|
return mReactInstanceManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get singleton instance of UpdateContext
|
||||||
|
*/
|
||||||
|
public static UpdateContext getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
synchronized (sLock) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new UpdateContext(context.getApplicationContext());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
public static String getBundleUrl(Context context) {
|
public static String getBundleUrl(Context context) {
|
||||||
return new UpdateContext(context.getApplicationContext()).getBundleUrl();
|
return getInstance(context).getBundleUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getBundleUrl(Context context, String defaultAssetsUrl) {
|
public static String getBundleUrl(Context context, String defaultAssetsUrl) {
|
||||||
return new UpdateContext(context.getApplicationContext()).getBundleUrl(defaultAssetsUrl);
|
return getInstance(context).getBundleUrl(defaultAssetsUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBundleUrl() {
|
public String getBundleUrl() {
|
||||||
@@ -255,6 +295,7 @@ public class UpdateContext {
|
|||||||
if (lastVersion == null) {
|
if (lastVersion == null) {
|
||||||
editor.remove("currentVersion");
|
editor.remove("currentVersion");
|
||||||
} else {
|
} else {
|
||||||
|
editor.remove("lastVersion");
|
||||||
editor.putString("currentVersion", lastVersion);
|
editor.putString("currentVersion", lastVersion);
|
||||||
}
|
}
|
||||||
editor.putBoolean("firstTimeOk", true);
|
editor.putBoolean("firstTimeOk", true);
|
||||||
|
|||||||
@@ -23,6 +23,38 @@ import java.util.Map;
|
|||||||
public class UpdateModuleImpl {
|
public class UpdateModuleImpl {
|
||||||
|
|
||||||
public static final String NAME = "Pushy";
|
public static final String NAME = "Pushy";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取字段的兼容性方法,尝试带m前缀和不带m前缀的字段名
|
||||||
|
* @param clazz 目标类
|
||||||
|
* @param fieldName 基础字段名(不带m前缀)
|
||||||
|
* @return 找到的字段对象
|
||||||
|
* @throws NoSuchFieldException 如果两种命名都找不到字段
|
||||||
|
*/
|
||||||
|
private static Field getCompatibleField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
|
||||||
|
// 首先尝试带m前缀的字段名
|
||||||
|
try {
|
||||||
|
return clazz.getDeclaredField("m" + capitalize(fieldName));
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
// 如果找不到带m前缀的,尝试不带m前缀的
|
||||||
|
try {
|
||||||
|
return clazz.getDeclaredField(fieldName);
|
||||||
|
} catch (NoSuchFieldException e2) {
|
||||||
|
// 如果都找不到,抛出异常并包含两种尝试的信息
|
||||||
|
throw new NoSuchFieldException("Field not found with either name: m" + capitalize(fieldName) + " or " + fieldName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 首字母大写的辅助方法
|
||||||
|
*/
|
||||||
|
private static String capitalize(String str) {
|
||||||
|
if (str == null || str.length() == 0) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
return str.substring(0, 1).toUpperCase() + str.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
public static void downloadFullUpdate(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
|
public static void downloadFullUpdate(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
|
||||||
String url = options.getString("updateUrl");
|
String url = options.getString("updateUrl");
|
||||||
@@ -143,16 +175,16 @@ public class UpdateModuleImpl {
|
|||||||
ReactDelegate reactDelegate = (ReactDelegate)
|
ReactDelegate reactDelegate = (ReactDelegate)
|
||||||
getReactDelegateMethod.invoke(currentActivity);
|
getReactDelegateMethod.invoke(currentActivity);
|
||||||
|
|
||||||
Field reactHostField = ReactDelegate.class.getDeclaredField("mReactHost");
|
Field reactHostField = getCompatibleField(ReactDelegate.class, "reactHost");
|
||||||
reactHostField.setAccessible(true);
|
reactHostField.setAccessible(true);
|
||||||
Object reactHost = reactHostField.get(reactDelegate);
|
Object reactHost = reactHostField.get(reactDelegate);
|
||||||
|
|
||||||
Field devSupport = reactHost.getClass().getDeclaredField("mUseDevSupport");
|
Field devSupport = getCompatibleField(reactHost.getClass(), "useDevSupport");
|
||||||
devSupport.setAccessible(true);
|
devSupport.setAccessible(true);
|
||||||
devSupport.set(reactHost, false);
|
devSupport.set(reactHost, false);
|
||||||
|
|
||||||
// Access the mReactHostDelegate field
|
// Access the ReactHostDelegate field (compatible with mReactHostDelegate/reactHostDelegate)
|
||||||
Field reactHostDelegateField = reactHost.getClass().getDeclaredField("mReactHostDelegate");
|
Field reactHostDelegateField = getCompatibleField(reactHost.getClass(), "reactHostDelegate");
|
||||||
reactHostDelegateField.setAccessible(true);
|
reactHostDelegateField.setAccessible(true);
|
||||||
Object reactHostDelegate = reactHostDelegateField.get(reactHost);
|
Object reactHostDelegate = reactHostDelegateField.get(reactHost);
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class UpdateModule extends NativePushySpec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UpdateModule(ReactApplicationContext reactContext) {
|
public UpdateModule(ReactApplicationContext reactContext) {
|
||||||
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
|
this(reactContext, UpdateContext.getInstance(reactContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -35,7 +35,9 @@ public class UpdateModule extends NativePushySpec {
|
|||||||
final Map<String, Object> constants = new HashMap<>();
|
final Map<String, Object> constants = new HashMap<>();
|
||||||
constants.put("downloadRootDir", updateContext.getRootDir());
|
constants.put("downloadRootDir", updateContext.getRootDir());
|
||||||
constants.put("packageVersion", updateContext.getPackageVersion());
|
constants.put("packageVersion", updateContext.getPackageVersion());
|
||||||
constants.put("currentVersion", updateContext.getCurrentVersion());
|
String currentVersion = updateContext.getCurrentVersion();
|
||||||
|
constants.put("currentVersion", currentVersion);
|
||||||
|
constants.put("currentVersionInfo", updateContext.getKv("hash_" + currentVersion));
|
||||||
constants.put("buildTime", updateContext.getBuildTime());
|
constants.put("buildTime", updateContext.getBuildTime());
|
||||||
constants.put("isUsingBundleUrl", updateContext.getIsUsingBundleUrl());
|
constants.put("isUsingBundleUrl", updateContext.getIsUsingBundleUrl());
|
||||||
boolean isFirstTime = updateContext.isFirstTime();
|
boolean isFirstTime = updateContext.isFirstTime();
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class UpdateModule extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UpdateModule(ReactApplicationContext reactContext) {
|
public UpdateModule(ReactApplicationContext reactContext) {
|
||||||
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
|
this(reactContext, UpdateContext.getInstance(reactContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -48,7 +48,9 @@ public class UpdateModule extends ReactContextBaseJavaModule {
|
|||||||
final Map<String, Object> constants = new HashMap<>();
|
final Map<String, Object> constants = new HashMap<>();
|
||||||
constants.put("downloadRootDir", updateContext.getRootDir());
|
constants.put("downloadRootDir", updateContext.getRootDir());
|
||||||
constants.put("packageVersion", updateContext.getPackageVersion());
|
constants.put("packageVersion", updateContext.getPackageVersion());
|
||||||
constants.put("currentVersion", updateContext.getCurrentVersion());
|
String currentVersion = updateContext.getCurrentVersion();
|
||||||
|
constants.put("currentVersion", currentVersion);
|
||||||
|
constants.put("currentVersionInfo", updateContext.getKv("hash_" + currentVersion));
|
||||||
constants.put("buildTime", updateContext.getBuildTime());
|
constants.put("buildTime", updateContext.getBuildTime());
|
||||||
constants.put("isUsingBundleUrl", updateContext.getIsUsingBundleUrl());
|
constants.put("isUsingBundleUrl", updateContext.getIsUsingBundleUrl());
|
||||||
boolean isFirstTime = updateContext.isFirstTime();
|
boolean isFirstTime = updateContext.isFirstTime();
|
||||||
|
|||||||
Binary file not shown.
39
harmony/pushy/hvigor-plugin.ts
Normal file
39
harmony/pushy/hvigor-plugin.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
export function reactNativeUpdatePlugin() {
|
||||||
|
return {
|
||||||
|
pluginId: 'reactNativeUpdatePlugin',
|
||||||
|
apply(node) {
|
||||||
|
node.registerTask({
|
||||||
|
name: 'reactNativeUpdatePlugin',
|
||||||
|
run: () => {
|
||||||
|
const cwd = process.cwd();
|
||||||
|
const metaFilePath = path.resolve(
|
||||||
|
cwd,
|
||||||
|
'entry/src/main/resources/rawfile/meta.json',
|
||||||
|
);
|
||||||
|
fs.mkdirSync(path.dirname(metaFilePath), { recursive: true });
|
||||||
|
|
||||||
|
const moduleJsonPath = path.resolve(cwd, 'AppScope/app.json5');
|
||||||
|
let versionName = '';
|
||||||
|
if (fs.existsSync(moduleJsonPath)) {
|
||||||
|
const content = fs.readFileSync(moduleJsonPath, 'utf-8');
|
||||||
|
const match = content.match(
|
||||||
|
/(?:"versionName"|versionName):\s*["']([^"']+)["']/,
|
||||||
|
);
|
||||||
|
versionName = match?.[1] || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const metaContent = {
|
||||||
|
pushy_build_time: new Date().toISOString(),
|
||||||
|
versionName,
|
||||||
|
};
|
||||||
|
|
||||||
|
fs.writeFileSync(metaFilePath, JSON.stringify(metaContent, null, 2));
|
||||||
|
console.log(`Build time written to ${metaFilePath}`);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,18 +1,19 @@
|
|||||||
{
|
{
|
||||||
"meta": {
|
"meta": {
|
||||||
"stableOrder": true
|
"stableOrder": true,
|
||||||
|
"enableUnifiedLockfile": false
|
||||||
},
|
},
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
|
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
|
||||||
"specifiers": {
|
"specifiers": {
|
||||||
"@rnoh/react-native-openharmony@0.72.38": "@rnoh/react-native-openharmony@0.72.38"
|
"@rnoh/react-native-openharmony@^0.72.96": "@rnoh/react-native-openharmony@0.72.96"
|
||||||
},
|
},
|
||||||
"packages": {
|
"packages": {
|
||||||
"@rnoh/react-native-openharmony@0.72.38": {
|
"@rnoh/react-native-openharmony@0.72.96": {
|
||||||
"name": "@rnoh/react-native-openharmony",
|
"name": "",
|
||||||
"version": "0.72.38",
|
"version": "0.72.96",
|
||||||
"integrity": "sha512-br5SIrbB0OarSLirenleE7eTOX1lNccMJ7nb/G7qWTyJ7kW4DalmTXVKYpoT2qaOLls1uEE7McD1OjbZZM9jug==",
|
"integrity": "sha512-gBbm8LLyqi5UE7qHWdZYeQnjyncfEpCczKZUP/9M2U1Z7exR0Kya8PMKMwr1ta5ujy7w/hZVC2LomEV4QvBeqA==",
|
||||||
"resolved": "https://ohpm.openharmony.cn/ohpm/@rnoh/react-native-openharmony/-/react-native-openharmony-0.72.38.har",
|
"resolved": "https://ohpm.openharmony.cn/ohpm/@rnoh/react-native-openharmony/-/react-native-openharmony-0.72.96.har",
|
||||||
"registryType": "ohpm"
|
"registryType": "ohpm"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
{
|
{
|
||||||
"license": "ISC",
|
license: 'MIT',
|
||||||
"types": "",
|
types: '',
|
||||||
"devDependencies": {},
|
devDependencies: {},
|
||||||
"name": "pushy",
|
name: 'pushy',
|
||||||
"description": "",
|
description: '',
|
||||||
"main": "index.ets",
|
main: 'index.ets',
|
||||||
"version": "3.1.0-0.0.7",
|
version: '10.35.1',
|
||||||
"dependencies": {
|
dependencies: {
|
||||||
"@rnoh/react-native-openharmony":"^0.72.38"
|
'@rnoh/react-native-openharmony': '^0.72.96',
|
||||||
}
|
},
|
||||||
|
modelVersion: '5.0.0',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
cmake_minimum_required(VERSION 3.13)
|
cmake_minimum_required(VERSION 3.13)
|
||||||
project(rnupdate)
|
project(rnupdate)
|
||||||
|
|
||||||
set(HDIFFPATCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/HDiffPatch)
|
# Point to android/jni directory for shared source code
|
||||||
set(LZMA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lzma)
|
set(ANDROID_JNI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../node_modules/react-native-update/android/jni)
|
||||||
|
set(HDIFFPATCH_DIR ${ANDROID_JNI_DIR}/HDiffPatch)
|
||||||
|
set(LZMA_DIR ${ANDROID_JNI_DIR}/lzma)
|
||||||
set(HDP_SOURCES
|
set(HDP_SOURCES
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/pushy.c
|
${CMAKE_CURRENT_SOURCE_DIR}/pushy.c
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/hpatch.c
|
${ANDROID_JNI_DIR}/hpatch.c
|
||||||
${HDIFFPATCH_DIR}/libHDiffPatch/HPatch/patch.c
|
${HDIFFPATCH_DIR}/libHDiffPatch/HPatch/patch.c
|
||||||
${HDIFFPATCH_DIR}/file_for_patch.c
|
${HDIFFPATCH_DIR}/file_for_patch.c
|
||||||
${LZMA_DIR}/C/LzmaDec.c
|
${LZMA_DIR}/C/LzmaDec.c
|
||||||
@@ -20,6 +22,7 @@ add_library(rnupdate SHARED
|
|||||||
|
|
||||||
target_include_directories(rnupdate PRIVATE
|
target_include_directories(rnupdate PRIVATE
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
${ANDROID_JNI_DIR}
|
||||||
${HDIFFPATCH_DIR}
|
${HDIFFPATCH_DIR}
|
||||||
${HDIFFPATCH_DIR}/libHDiffPatch/HPatch
|
${HDIFFPATCH_DIR}/libHDiffPatch/HPatch
|
||||||
${LZMA_DIR}/C
|
${LZMA_DIR}/C
|
||||||
|
|||||||
@@ -1,137 +0,0 @@
|
|||||||
// hpatch.c
|
|
||||||
// Copyright 2021 housisong, All rights reserved
|
|
||||||
#include "hpatch.h"
|
|
||||||
#include "HDiffPatch/libHDiffPatch/HPatch/patch.h"
|
|
||||||
#include "HDiffPatch/file_for_patch.h"
|
|
||||||
|
|
||||||
//#define _CompressPlugin_zlib
|
|
||||||
//#define _CompressPlugin_bz2
|
|
||||||
#define _CompressPlugin_lzma
|
|
||||||
#define _CompressPlugin_lzma2
|
|
||||||
#define _IsNeedIncludeDefaultCompressHead 0
|
|
||||||
#include "lzma/C/LzmaDec.h"
|
|
||||||
#include "lzma/C/Lzma2Dec.h"
|
|
||||||
#include "HDiffPatch/decompress_plugin_demo.h"
|
|
||||||
|
|
||||||
#define kMaxLoadMemOldSize ((1<<20)*8)
|
|
||||||
|
|
||||||
#define _check(v,errorType) do{ \
|
|
||||||
if (!(v)){ if (result==kHPatch_ok) result=errorType; if (!_isInClear){ goto _clear; }; } }while(0)
|
|
||||||
|
|
||||||
int hpatch_getInfo_by_mem(hpatch_singleCompressedDiffInfo* out_patinfo,
|
|
||||||
const uint8_t* pat,size_t patsize){
|
|
||||||
hpatch_TStreamInput patStream;
|
|
||||||
mem_as_hStreamInput(&patStream,pat,pat+patsize);
|
|
||||||
if (!getSingleCompressedDiffInfo(out_patinfo,&patStream,0))
|
|
||||||
return kHPatch_error_info;//data error;
|
|
||||||
return kHPatch_ok; //ok
|
|
||||||
}
|
|
||||||
|
|
||||||
static hpatch_TDecompress* getDecompressPlugin(const char* compressType){
|
|
||||||
#ifdef _CompressPlugin_zlib
|
|
||||||
if (zlibDecompressPlugin.is_can_open(compressType))
|
|
||||||
return &zlibDecompressPlugin;
|
|
||||||
#endif
|
|
||||||
#ifdef _CompressPlugin_bz2
|
|
||||||
if (bz2DecompressPlugin.is_can_open(compressType))
|
|
||||||
return &bz2DecompressPlugin;
|
|
||||||
#endif
|
|
||||||
#ifdef _CompressPlugin_lzma
|
|
||||||
if (lzmaDecompressPlugin.is_can_open(compressType))
|
|
||||||
return &lzmaDecompressPlugin;
|
|
||||||
#endif
|
|
||||||
#ifdef _CompressPlugin_lzma2
|
|
||||||
if (lzma2DecompressPlugin.is_can_open(compressType))
|
|
||||||
return &lzma2DecompressPlugin;
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
static int hpatch_by_stream(const hpatch_TStreamInput* old,hpatch_BOOL isLoadOldAllToMem,const hpatch_TStreamInput* pat,
|
|
||||||
hpatch_TStreamOutput* out_new,const hpatch_singleCompressedDiffInfo* patInfo){
|
|
||||||
int result=kHPatch_ok;
|
|
||||||
int _isInClear=hpatch_FALSE;
|
|
||||||
hpatch_TDecompress* decompressPlugin=0;
|
|
||||||
uint8_t* temp_cache=0;
|
|
||||||
size_t temp_cache_size;
|
|
||||||
hpatch_singleCompressedDiffInfo _patinfo;
|
|
||||||
hpatch_TStreamInput _old;
|
|
||||||
{// info
|
|
||||||
if (!patInfo){
|
|
||||||
_check(getSingleCompressedDiffInfo(&_patinfo,pat,0),kHPatch_error_info);
|
|
||||||
patInfo=&_patinfo;
|
|
||||||
}
|
|
||||||
_check(old->streamSize==patInfo->oldDataSize,kHPatch_error_old_size);
|
|
||||||
_check(out_new->streamSize>=patInfo->newDataSize,kHPatch_error_new_size);
|
|
||||||
out_new->streamSize=patInfo->newDataSize;
|
|
||||||
if (strlen(patInfo->compressType)>0){
|
|
||||||
decompressPlugin=getDecompressPlugin(patInfo->compressType);
|
|
||||||
_check(decompressPlugin,kHPatch_error_compressType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{// mem
|
|
||||||
size_t mem_size;
|
|
||||||
size_t oldSize=(size_t)old->streamSize;
|
|
||||||
isLoadOldAllToMem=isLoadOldAllToMem&&(old->streamSize<=kMaxLoadMemOldSize);
|
|
||||||
temp_cache_size=patInfo->stepMemSize+hpatch_kFileIOBufBetterSize*3;
|
|
||||||
mem_size=temp_cache_size+(isLoadOldAllToMem?oldSize:0);
|
|
||||||
temp_cache=malloc(mem_size);
|
|
||||||
_check(temp_cache,kHPatch_error_malloc);
|
|
||||||
if (isLoadOldAllToMem){//load old to mem
|
|
||||||
uint8_t* oldMem=temp_cache+temp_cache_size;
|
|
||||||
_check(old->read(old,0,oldMem,oldMem+oldSize),kHPatch_error_old_fread);
|
|
||||||
mem_as_hStreamInput(&_old,oldMem,oldMem+oldSize);
|
|
||||||
old=&_old;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_check(patch_single_compressed_diff(out_new,old,pat,patInfo->diffDataPos,
|
|
||||||
patInfo->uncompressedSize,decompressPlugin,patInfo->coverCount,
|
|
||||||
patInfo->stepMemSize,temp_cache,temp_cache+temp_cache_size),kHPatch_error_patch);
|
|
||||||
|
|
||||||
_clear:
|
|
||||||
_isInClear=hpatch_TRUE;
|
|
||||||
if (temp_cache){ free(temp_cache); temp_cache=0; }
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int hpatch_by_mem(const uint8_t* old,size_t oldsize,uint8_t* newBuf,size_t newsize,
|
|
||||||
const uint8_t* pat,size_t patsize,const hpatch_singleCompressedDiffInfo* patInfo){
|
|
||||||
hpatch_TStreamInput oldStream;
|
|
||||||
hpatch_TStreamInput patStream;
|
|
||||||
hpatch_TStreamOutput newStream;
|
|
||||||
mem_as_hStreamInput(&oldStream,old,old+oldsize);
|
|
||||||
mem_as_hStreamInput(&patStream,pat,pat+patsize);
|
|
||||||
mem_as_hStreamOutput(&newStream,newBuf,newBuf+newsize);
|
|
||||||
return hpatch_by_stream(&oldStream,hpatch_FALSE,&patStream,&newStream,patInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
int hpatch_by_file(const char* oldfile, const char* newfile, const char* patchfile){
|
|
||||||
int result=kHPatch_ok;
|
|
||||||
int _isInClear=hpatch_FALSE;
|
|
||||||
int patch_result;
|
|
||||||
hpatch_TFileStreamInput oldStream;
|
|
||||||
hpatch_TFileStreamInput patStream;
|
|
||||||
hpatch_TFileStreamOutput newStream;
|
|
||||||
hpatch_TFileStreamInput_init(&oldStream);
|
|
||||||
hpatch_TFileStreamInput_init(&patStream);
|
|
||||||
hpatch_TFileStreamOutput_init(&newStream);
|
|
||||||
|
|
||||||
_check(hpatch_TFileStreamInput_open(&oldStream,oldfile),kHPatch_error_old_fopen);
|
|
||||||
_check(hpatch_TFileStreamInput_open(&patStream,patchfile),kHPatch_error_pat_fopen);
|
|
||||||
_check(hpatch_TFileStreamOutput_open(&newStream,newfile,~(hpatch_StreamPos_t)0),kHPatch_error_new_fopen);
|
|
||||||
|
|
||||||
patch_result=hpatch_by_stream(&oldStream.base,hpatch_TRUE,&patStream.base,&newStream.base,0);
|
|
||||||
if (patch_result!=kHPatch_ok){
|
|
||||||
_check(!oldStream.fileError,kHPatch_error_old_fread);
|
|
||||||
_check(!patStream.fileError,kHPatch_error_pat_fread);
|
|
||||||
_check(!newStream.fileError,kHPatch_error_new_fwrite);
|
|
||||||
_check(hpatch_FALSE,patch_result);
|
|
||||||
}
|
|
||||||
|
|
||||||
_clear:
|
|
||||||
_isInClear=hpatch_TRUE;
|
|
||||||
_check(hpatch_TFileStreamInput_close(&oldStream),kHPatch_error_old_fclose);
|
|
||||||
_check(hpatch_TFileStreamInput_close(&patStream),kHPatch_error_pat_fclose);
|
|
||||||
_check(hpatch_TFileStreamOutput_close(&newStream),kHPatch_error_new_fclose);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
// hpatch.h
|
|
||||||
// import HDiffPatch, support patchData created by "hdiffz -SD -c-lzma2 oldfile newfile patchfile"
|
|
||||||
// Copyright 2021 housisong, All rights reserved
|
|
||||||
|
|
||||||
#ifndef HDIFFPATCH_PATCH_H
|
|
||||||
#define HDIFFPATCH_PATCH_H
|
|
||||||
# include <stdint.h> //for uint8_t
|
|
||||||
#include "HDiffPatch/libHDiffPatch/HPatch/patch_types.h" //for hpatch_singleCompressedDiffInfo
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//result
|
|
||||||
enum {
|
|
||||||
kHPatch_ok = 0,
|
|
||||||
kHPatch_error_malloc =-1,
|
|
||||||
kHPatch_error_info =-2,
|
|
||||||
kHPatch_error_compressType =-3,
|
|
||||||
kHPatch_error_patch =-4,
|
|
||||||
kHPatch_error_old_fopen =-5,
|
|
||||||
kHPatch_error_old_fread =-6,
|
|
||||||
kHPatch_error_old_fclose =-7,
|
|
||||||
kHPatch_error_pat_fopen =-8,
|
|
||||||
kHPatch_error_pat_fread =-9,
|
|
||||||
kHPatch_error_pat_fclose =-10,
|
|
||||||
kHPatch_error_new_fopen =-11,
|
|
||||||
kHPatch_error_new_fwrite =-12,
|
|
||||||
kHPatch_error_new_fclose =-13,
|
|
||||||
kHPatch_error_old_size =-14,
|
|
||||||
kHPatch_error_new_size =-15,
|
|
||||||
};
|
|
||||||
|
|
||||||
int hpatch_getInfo_by_mem(hpatch_singleCompressedDiffInfo* out_patinfo,
|
|
||||||
const uint8_t* pat,size_t patsize);
|
|
||||||
|
|
||||||
//patInfo can NULL
|
|
||||||
int hpatch_by_mem(const uint8_t* old,size_t oldsize, uint8_t* newBuf,size_t newsize,
|
|
||||||
const uint8_t* pat,size_t patsize,const hpatch_singleCompressedDiffInfo* patInfo);
|
|
||||||
int hpatch_by_file(const char* oldfile, const char* newfile, const char* patchfile);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif //HDIFFPATCH_PATCH_H
|
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
import http from '@ohos.net.http';
|
import http from '@ohos.net.http';
|
||||||
import fileIo from '@ohos.file.fs';
|
import fileIo from '@ohos.file.fs';
|
||||||
import util from '@ohos.util';
|
|
||||||
import common from '@ohos.app.ability.common';
|
import common from '@ohos.app.ability.common';
|
||||||
import { BusinessError } from '@kit.BasicServicesKit';
|
import { zlib } from '@kit.BasicServicesKit';
|
||||||
import { buffer } from '@kit.ArkTS';
|
|
||||||
import zip from '@ohos.zlib';
|
|
||||||
import { EventHub } from './EventHub';
|
import { EventHub } from './EventHub';
|
||||||
import { DownloadTaskParams } from './DownloadTaskParams';
|
import { DownloadTaskParams } from './DownloadTaskParams';
|
||||||
import Pushy from 'librnupdate.so';
|
import Pushy from 'librnupdate.so';
|
||||||
@@ -37,7 +34,9 @@ export class DownloadTask {
|
|||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
const files = await fileIo.listFile(path);
|
const files = await fileIo.listFile(path);
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if (file === '.' || file === '..') continue;
|
if (file === '.' || file === '..') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
await this.removeDirectory(`${path}/${file}`);
|
await this.removeDirectory(`${path}/${file}`);
|
||||||
}
|
}
|
||||||
await fileIo.rmdir(path);
|
await fileIo.rmdir(path);
|
||||||
@@ -57,16 +56,16 @@ export class DownloadTask {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
const exists = fileIo.accessSync(params.targetFile);
|
let exists = fileIo.accessSync(params.targetFile);
|
||||||
if (exists) {
|
if (exists) {
|
||||||
await fileIo.unlink(params.targetFile);
|
await fileIo.unlink(params.targetFile);
|
||||||
}else{
|
} else {
|
||||||
const targetDir = params.targetFile.substring(
|
const targetDir = params.targetFile.substring(
|
||||||
0,
|
0,
|
||||||
params.targetFile.lastIndexOf('/'),
|
params.targetFile.lastIndexOf('/'),
|
||||||
);
|
);
|
||||||
const exists = fileIo.accessSync(targetDir);
|
exists = fileIo.accessSync(targetDir);
|
||||||
if(!exists){
|
if (!exists) {
|
||||||
await fileIo.mkdir(targetDir);
|
await fileIo.mkdir(targetDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,7 +82,7 @@ export class DownloadTask {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (response.responseCode > 299) {
|
if (response.responseCode > 299) {
|
||||||
throw new Error(`Server error: ${response.responseCode}`);
|
throw Error(`Server error: ${response.responseCode}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentLength = parseInt(response.header['content-length'] || '0');
|
const contentLength = parseInt(response.header['content-length'] || '0');
|
||||||
@@ -108,9 +107,10 @@ export class DownloadTask {
|
|||||||
const stats = await fileIo.stat(params.targetFile);
|
const stats = await fileIo.stat(params.targetFile);
|
||||||
const fileSize = stats.size;
|
const fileSize = stats.size;
|
||||||
if (fileSize !== contentLength) {
|
if (fileSize !== contentLength) {
|
||||||
throw new Error(`Download incomplete: expected ${contentLength} bytes but got ${stats.size} bytes`);
|
throw Error(
|
||||||
|
`Download incomplete: expected ${contentLength} bytes but got ${stats.size} bytes`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Download failed:', error);
|
console.error('Download failed:', error);
|
||||||
throw error;
|
throw error;
|
||||||
@@ -127,64 +127,12 @@ export class DownloadTask {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async copyFile(from: string, to: string): Promise<void> {
|
|
||||||
let reader;
|
|
||||||
let writer;
|
|
||||||
try {
|
|
||||||
reader = fileIo.openSync(from, fileIo.OpenMode.READ_ONLY);
|
|
||||||
writer = fileIo.openSync(
|
|
||||||
to,
|
|
||||||
fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY,
|
|
||||||
);
|
|
||||||
const arrayBuffer = new ArrayBuffer(4096);
|
|
||||||
let bytesRead: number;
|
|
||||||
do {
|
|
||||||
bytesRead = await fileIo
|
|
||||||
.read(reader.fd, arrayBuffer)
|
|
||||||
.catch((err: BusinessError) => {
|
|
||||||
throw new Error(
|
|
||||||
`Error reading file: ${err.message}, code: ${err.code}`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
if (bytesRead > 0) {
|
|
||||||
const buf = buffer.from(arrayBuffer, 0, bytesRead);
|
|
||||||
await fileIo
|
|
||||||
.write(writer.fd, buf.buffer, {
|
|
||||||
offset: 0,
|
|
||||||
length: bytesRead,
|
|
||||||
})
|
|
||||||
.catch((err: BusinessError) => {
|
|
||||||
throw new Error(
|
|
||||||
`Error writing file: ${err.message}, code: ${err.code}`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} while (bytesRead > 0);
|
|
||||||
console.info('File copied successfully');
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Copy file failed:', error);
|
|
||||||
throw error;
|
|
||||||
} finally {
|
|
||||||
if (reader !== undefined) {
|
|
||||||
fileIo.closeSync(reader);
|
|
||||||
}
|
|
||||||
if (writer !== undefined) {
|
|
||||||
fileIo.closeSync(writer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async doFullPatch(params: DownloadTaskParams): Promise<void> {
|
private async doFullPatch(params: DownloadTaskParams): Promise<void> {
|
||||||
await this.downloadFile(params);
|
await this.downloadFile(params);
|
||||||
await this.removeDirectory(params.unzipDirectory);
|
await this.removeDirectory(params.unzipDirectory);
|
||||||
await fileIo.mkdir(params.unzipDirectory);
|
await fileIo.mkdir(params.unzipDirectory);
|
||||||
|
|
||||||
try {
|
await zlib.decompressFile(params.targetFile, params.unzipDirectory);
|
||||||
await zip.decompressFile(params.targetFile, params.unzipDirectory);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Unzip failed:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async processUnzippedFiles(directory: string): Promise<ZipFile> {
|
private async processUnzippedFiles(directory: string): Promise<ZipFile> {
|
||||||
@@ -192,7 +140,9 @@ export class DownloadTask {
|
|||||||
try {
|
try {
|
||||||
const files = await fileIo.listFile(directory);
|
const files = await fileIo.listFile(directory);
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if (file === '.' || file === '..') continue;
|
if (file === '.' || file === '..') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const filePath = `${directory}/${file}`;
|
const filePath = `${directory}/${file}`;
|
||||||
const stat = await fileIo.stat(filePath);
|
const stat = await fileIo.stat(filePath);
|
||||||
@@ -229,7 +179,7 @@ export class DownloadTask {
|
|||||||
let foundDiff = false;
|
let foundDiff = false;
|
||||||
let foundBundlePatch = false;
|
let foundBundlePatch = false;
|
||||||
const copyList: Map<string, Array<any>> = new Map();
|
const copyList: Map<string, Array<any>> = new Map();
|
||||||
await zip.decompressFile(params.targetFile, params.unzipDirectory);
|
await zlib.decompressFile(params.targetFile, params.unzipDirectory);
|
||||||
const zipFile = await this.processUnzippedFiles(params.unzipDirectory);
|
const zipFile = await this.processUnzippedFiles(params.unzipDirectory);
|
||||||
for (const entry of zipFile.entries) {
|
for (const entry of zipFile.entries) {
|
||||||
const fn = entry.filename;
|
const fn = entry.filename;
|
||||||
@@ -245,7 +195,7 @@ export class DownloadTask {
|
|||||||
|
|
||||||
const copies = obj.copies;
|
const copies = obj.copies;
|
||||||
for (const to in copies) {
|
for (const to in copies) {
|
||||||
let from = copies[to];
|
let from = copies[to].replace('resources/rawfile/', '');
|
||||||
if (from === '') {
|
if (from === '') {
|
||||||
from = to;
|
from = to;
|
||||||
}
|
}
|
||||||
@@ -294,17 +244,13 @@ export class DownloadTask {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fn !== '.DS_Store'){
|
|
||||||
await zip.decompressFile(fn, params.unzipDirectory);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!foundDiff) {
|
if (!foundDiff) {
|
||||||
throw new Error('diff.json not found');
|
throw Error('diff.json not found');
|
||||||
}
|
}
|
||||||
if (!foundBundlePatch) {
|
if (!foundBundlePatch) {
|
||||||
throw new Error('bundle patch not found');
|
throw Error('bundle patch not found');
|
||||||
}
|
}
|
||||||
await this.copyFromResource(copyList);
|
await this.copyFromResource(copyList);
|
||||||
}
|
}
|
||||||
@@ -316,14 +262,20 @@ export class DownloadTask {
|
|||||||
|
|
||||||
let foundDiff = false;
|
let foundDiff = false;
|
||||||
let foundBundlePatch = false;
|
let foundBundlePatch = false;
|
||||||
const copyList: Map<string, Array<any>> = new Map();
|
await zlib.decompressFile(params.targetFile, params.unzipDirectory);
|
||||||
await zip.decompressFile(params.targetFile, params.unzipDirectory);
|
|
||||||
const zipFile = await this.processUnzippedFiles(params.unzipDirectory);
|
const zipFile = await this.processUnzippedFiles(params.unzipDirectory);
|
||||||
for (const entry of zipFile.entries) {
|
for (const entry of zipFile.entries) {
|
||||||
const fn = entry.filename;
|
const fn = entry.filename;
|
||||||
|
|
||||||
if (fn === '__diff.json') {
|
if (fn === '__diff.json') {
|
||||||
foundDiff = true;
|
foundDiff = true;
|
||||||
|
|
||||||
|
await fileIo
|
||||||
|
.copyDir(params.originDirectory + '/', params.unzipDirectory + '/')
|
||||||
|
.catch(error => {
|
||||||
|
console.error('copy error:', error);
|
||||||
|
});
|
||||||
|
|
||||||
let jsonContent = '';
|
let jsonContent = '';
|
||||||
const bufferArray = new Uint8Array(entry.content);
|
const bufferArray = new Uint8Array(entry.content);
|
||||||
for (let i = 0; i < bufferArray.length; i++) {
|
for (let i = 0; i < bufferArray.length; i++) {
|
||||||
@@ -331,22 +283,23 @@ export class DownloadTask {
|
|||||||
}
|
}
|
||||||
const obj = JSON.parse(jsonContent);
|
const obj = JSON.parse(jsonContent);
|
||||||
|
|
||||||
const copies = obj.copies;
|
const { copies, deletes } = obj;
|
||||||
for (const to in copies) {
|
for (const [to, from] of Object.entries(copies)) {
|
||||||
let from = copies[to];
|
await fileIo
|
||||||
if (from === '') {
|
.copyFile(
|
||||||
from = to;
|
`${params.originDirectory}/${from}`,
|
||||||
}
|
`${params.unzipDirectory}/${to}`,
|
||||||
|
)
|
||||||
if (!copyList.has(from)) {
|
.catch(error => {
|
||||||
copyList.set(from, []);
|
console.error('copy error:', error);
|
||||||
}
|
});
|
||||||
|
}
|
||||||
const target = copyList.get(from);
|
for (const fileToDelete of Object.keys(deletes)) {
|
||||||
if (target) {
|
await fileIo
|
||||||
const toFile = `${params.unzipDirectory}/${to}`;
|
.unlink(`${params.unzipDirectory}/${fileToDelete}`)
|
||||||
target.push(toFile);
|
.catch(error => {
|
||||||
}
|
console.error('delete error:', error);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -366,12 +319,18 @@ export class DownloadTask {
|
|||||||
new Uint8Array(entry.content),
|
new Uint8Array(entry.content),
|
||||||
);
|
);
|
||||||
const outputFile = `${params.unzipDirectory}/bundle.harmony.js`;
|
const outputFile = `${params.unzipDirectory}/bundle.harmony.js`;
|
||||||
const writer = await fileIo.open(outputFile, fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY);
|
const writer = await fileIo.open(
|
||||||
|
outputFile,
|
||||||
|
fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY,
|
||||||
|
);
|
||||||
const chunkSize = 4096;
|
const chunkSize = 4096;
|
||||||
let bytesWritten = 0;
|
let bytesWritten = 0;
|
||||||
const totalLength = patched.byteLength;
|
const totalLength = patched.byteLength;
|
||||||
while (bytesWritten < totalLength) {
|
while (bytesWritten < totalLength) {
|
||||||
const chunk = patched.slice(bytesWritten, bytesWritten + chunkSize);
|
const chunk = patched.slice(
|
||||||
|
bytesWritten,
|
||||||
|
bytesWritten + chunkSize,
|
||||||
|
);
|
||||||
await fileIo.write(writer.fd, chunk);
|
await fileIo.write(writer.fd, chunk);
|
||||||
bytesWritten += chunk.byteLength;
|
bytesWritten += chunk.byteLength;
|
||||||
}
|
}
|
||||||
@@ -382,15 +341,13 @@ export class DownloadTask {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await zip.decompressFile(entry.filename, params.unzipDirectory);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!foundDiff) {
|
if (!foundDiff) {
|
||||||
throw new Error('diff.json not found');
|
throw Error('diff.json not found');
|
||||||
}
|
}
|
||||||
if (!foundBundlePatch) {
|
if (!foundBundlePatch) {
|
||||||
throw new Error('bundle patch not found');
|
throw Error('bundle patch not found');
|
||||||
}
|
}
|
||||||
console.info('Patch from PPK completed');
|
console.info('Patch from PPK completed');
|
||||||
}
|
}
|
||||||
@@ -399,27 +356,14 @@ export class DownloadTask {
|
|||||||
copyList: Map<string, Array<string>>,
|
copyList: Map<string, Array<string>>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const bundlePath = this.context.bundleCodeDir;
|
const resourceManager = this.context.resourceManager;
|
||||||
|
|
||||||
const files = await fileIo.listFile(bundlePath);
|
for (const [from, targets] of copyList.entries()) {
|
||||||
for (const file of files) {
|
const fromContent = await resourceManager.getRawFileContent(from);
|
||||||
if (file === '.' || file === '..') continue;
|
for (const target of targets) {
|
||||||
|
const fileStream = fileIo.createStreamSync(target, 'w+');
|
||||||
const targets = copyList.get(file);
|
fileStream.writeSync(fromContent.buffer);
|
||||||
if (targets) {
|
fileStream.close();
|
||||||
let lastTarget: string | undefined;
|
|
||||||
|
|
||||||
for (const target of targets) {
|
|
||||||
console.info(`Copying from resource ${file} to ${target}`);
|
|
||||||
|
|
||||||
if (lastTarget) {
|
|
||||||
await this.copyFile(lastTarget, target);
|
|
||||||
} else {
|
|
||||||
const sourcePath = `${bundlePath}/${file}`;
|
|
||||||
await this.copyFile(sourcePath, target);
|
|
||||||
lastTarget = target;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -436,7 +380,9 @@ export class DownloadTask {
|
|||||||
try {
|
try {
|
||||||
const files = await fileIo.listFile(params.unzipDirectory);
|
const files = await fileIo.listFile(params.unzipDirectory);
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if (file.startsWith('.')) continue;
|
if (file.startsWith('.')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const filePath = `${params.unzipDirectory}/${file}`;
|
const filePath = `${params.unzipDirectory}/${file}`;
|
||||||
const stat = await fileIo.stat(filePath);
|
const stat = await fileIo.stat(filePath);
|
||||||
@@ -478,7 +424,7 @@ export class DownloadTask {
|
|||||||
await this.downloadFile(params);
|
await this.downloadFile(params);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unknown task type: ${params.type}`);
|
throw Error(`Unknown task type: ${params.type}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
params.listener?.onDownloadCompleted(params);
|
params.listener?.onDownloadCompleted(params);
|
||||||
|
|||||||
@@ -1,49 +1,44 @@
|
|||||||
import { HotReloadConfig, JSBundleProvider, JSBundleProviderError, JSPackagerClientConfig } from '@rnoh/react-native-openharmony';
|
import {
|
||||||
import fileIo from '@ohos.file.fs';
|
FileJSBundle,
|
||||||
|
HotReloadConfig,
|
||||||
|
JSBundleProvider,
|
||||||
|
JSBundleProviderError
|
||||||
|
} from '@rnoh/react-native-openharmony';
|
||||||
import common from '@ohos.app.ability.common';
|
import common from '@ohos.app.ability.common';
|
||||||
|
import fs from '@ohos.file.fs';
|
||||||
import { UpdateContext } from './UpdateContext';
|
import { UpdateContext } from './UpdateContext';
|
||||||
|
|
||||||
export class PushyFileJSBundleProvider extends JSBundleProvider {
|
export class PushyFileJSBundleProvider extends JSBundleProvider {
|
||||||
private updateContext: UpdateContext;
|
private updateContext: UpdateContext;
|
||||||
private filePath: string = ''
|
private path: string = ''
|
||||||
|
|
||||||
constructor(context: common.UIAbilityContext) {
|
constructor(context: common.UIAbilityContext) {
|
||||||
super();
|
super();
|
||||||
this.updateContext = new UpdateContext(context);
|
this.updateContext = new UpdateContext(context);
|
||||||
}
|
this.path = this.updateContext.getBundleUrl();
|
||||||
getURL(): string {
|
|
||||||
return this.updateContext.getBundleUrl().substring(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getBundle(): Promise<ArrayBuffer> {
|
getURL(): string {
|
||||||
|
return this.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBundle(): Promise<FileJSBundle> {
|
||||||
|
if (!this.path) {
|
||||||
|
throw new JSBundleProviderError({
|
||||||
|
whatHappened: 'No pushy bundle found. using default bundle',
|
||||||
|
howCanItBeFixed: ['']
|
||||||
|
})
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
this.filePath = this.updateContext.getBundleUrl();
|
await fs.access(this.path, fs.OpenMode.READ_ONLY);
|
||||||
const res = fileIo.accessSync(this.filePath);
|
return {
|
||||||
if (res) {
|
filePath: this.path
|
||||||
const file = fileIo.openSync(this.filePath, fileIo.OpenMode.READ_ONLY);
|
|
||||||
try {
|
|
||||||
const stat = await fileIo.stat(this.filePath);
|
|
||||||
const fileSize = stat.size;
|
|
||||||
const buffer = new ArrayBuffer(fileSize);
|
|
||||||
const bytesRead = fileIo.readSync(file.fd, buffer, {
|
|
||||||
offset: 0,
|
|
||||||
length: fileSize
|
|
||||||
});
|
|
||||||
|
|
||||||
if (bytesRead !== fileSize) {
|
|
||||||
throw new Error(`Failed to read entire file: read ${bytesRead} of ${fileSize} bytes`);
|
|
||||||
}
|
|
||||||
return buffer;
|
|
||||||
} finally {
|
|
||||||
fileIo.closeSync(file.fd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw new Error('Update bundle not found');
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new JSBundleProviderError({
|
throw new JSBundleProviderError({
|
||||||
whatHappened: `Couldn't load JSBundle from ${this.filePath}`,
|
whatHappened: `Couldn't load JSBundle from ${this.path}`,
|
||||||
extraData: error,
|
extraData: error,
|
||||||
howCanItBeFixed: [`Check if a bundle exists at "${this.filePath}" on your device.`]
|
howCanItBeFixed: [`Check if a bundle exists at "${this.path}" on your device.`]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,123 +1,150 @@
|
|||||||
import { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
import {
|
||||||
|
TurboModule,
|
||||||
|
TurboModuleContext,
|
||||||
|
} from '@rnoh/react-native-openharmony/ts';
|
||||||
import common from '@ohos.app.ability.common';
|
import common from '@ohos.app.ability.common';
|
||||||
import dataPreferences from '@ohos.data.preferences';
|
import dataPreferences from '@ohos.data.preferences';
|
||||||
import { bundleManager } from '@kit.AbilityKit';
|
import { bundleManager } from '@kit.AbilityKit';
|
||||||
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
|
|
||||||
import { BusinessError } from '@ohos.base';
|
|
||||||
import logger from './Logger';
|
import logger from './Logger';
|
||||||
import { UpdateModuleImpl } from './UpdateModuleImpl';
|
import { UpdateModuleImpl } from './UpdateModuleImpl';
|
||||||
import { UpdateContext } from './UpdateContext';
|
import { UpdateContext } from './UpdateContext';
|
||||||
import { EventHub } from './EventHub';
|
import { EventHub } from './EventHub';
|
||||||
|
|
||||||
const TAG = "PushyTurboModule"
|
const TAG = 'PushyTurboModule';
|
||||||
|
|
||||||
export class PushyTurboModule extends TurboModule {
|
export class PushyTurboModule extends TurboModule {
|
||||||
mUiCtx: common.UIAbilityContext
|
mUiCtx: common.UIAbilityContext;
|
||||||
context: UpdateContext
|
context: UpdateContext;
|
||||||
|
|
||||||
constructor(protected ctx: TurboModuleContext) {
|
constructor(protected ctx: TurboModuleContext) {
|
||||||
super(ctx);
|
super(ctx);
|
||||||
logger.debug(TAG, ",PushyTurboModule constructor");
|
logger.debug(TAG, ',PushyTurboModule constructor');
|
||||||
this.mUiCtx = ctx.uiAbilityContext
|
this.mUiCtx = ctx.uiAbilityContext;
|
||||||
this.context = new UpdateContext(this.mUiCtx)
|
this.context = new UpdateContext(this.mUiCtx);
|
||||||
EventHub.getInstance().setRNInstance(ctx.rnInstance)
|
EventHub.getInstance().setRNInstance(ctx.rnInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getConstants(): Object {
|
||||||
|
logger.debug(TAG, ',call getConstants');
|
||||||
|
const context = this.mUiCtx;
|
||||||
|
const preferencesManager = dataPreferences.getPreferencesSync(context, {
|
||||||
|
name: 'update',
|
||||||
|
});
|
||||||
|
const isFirstTime = preferencesManager.getSync(
|
||||||
|
'isFirstTime',
|
||||||
|
false,
|
||||||
|
) as boolean;
|
||||||
|
const rolledBackVersion = preferencesManager.getSync(
|
||||||
|
'rolledBackVersion',
|
||||||
|
'',
|
||||||
|
) as string;
|
||||||
|
const uuid = preferencesManager.getSync('uuid', '') as string;
|
||||||
|
const currentVersion = preferencesManager.getSync(
|
||||||
|
'currentVersion',
|
||||||
|
'',
|
||||||
|
) as string;
|
||||||
|
const currentVersionInfo = this.context.getKv(`hash_${currentVersion}`);
|
||||||
|
const buildTime = preferencesManager.getSync('buildTime', '') as string;
|
||||||
|
const isUsingBundleUrl = this.context.getIsUsingBundleUrl();
|
||||||
|
let bundleFlags =
|
||||||
|
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
|
||||||
|
let packageVersion = '';
|
||||||
|
try {
|
||||||
|
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
|
||||||
|
packageVersion = bundleInfo?.versionName || 'Unknown';
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to get bundle info:', error);
|
||||||
|
}
|
||||||
|
|
||||||
getConstants(): Object {
|
if (isFirstTime) {
|
||||||
logger.debug(TAG, ",call getConstants");
|
preferencesManager.deleteSync('isFirstTime');
|
||||||
const context = this.mUiCtx;
|
}
|
||||||
const preferencesManager = dataPreferences.getPreferencesSync(context,{ name: 'update' });
|
|
||||||
const isFirstTime = preferencesManager.getSync("isFirstTime", false) as boolean;
|
if (rolledBackVersion) {
|
||||||
const rolledBackVersion = preferencesManager.getSync("rolledBackVersion", "") as string;
|
preferencesManager.deleteSync('rolledBackVersion');
|
||||||
const uuid = preferencesManager.getSync("uuid", "") as string;
|
}
|
||||||
const currentVersion = preferencesManager.getSync("currentVersion", "") as string;
|
|
||||||
const buildTime = preferencesManager.getSync("buildTime", "") as string;
|
return {
|
||||||
const isUsingBundleUrl = this.context.getIsUsingBundleUrl();
|
downloadRootDir: `${context.filesDir}/_update`,
|
||||||
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
|
currentVersionInfo,
|
||||||
let packageVersion = '';
|
packageVersion,
|
||||||
try {
|
currentVersion,
|
||||||
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
|
buildTime,
|
||||||
packageVersion = bundleInfo?.versionName || "Unknown"
|
isUsingBundleUrl,
|
||||||
} catch (error) {
|
isFirstTime,
|
||||||
console.error("Failed to get bundle info:", error);
|
rolledBackVersion,
|
||||||
|
uuid,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFirstTime) {
|
setLocalHashInfo(hash: string, info: string): boolean {
|
||||||
preferencesManager.deleteSync("isFirstTime");
|
logger.debug(TAG, ',call setLocalHashInfo');
|
||||||
|
return UpdateModuleImpl.setLocalHashInfo(this.context, hash, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rolledBackVersion) {
|
getLocalHashInfo(hash: string): string {
|
||||||
preferencesManager.deleteSync("rolledBackVersion");
|
return UpdateModuleImpl.getLocalHashInfo(this.context, hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
async setUuid(uuid: string): Promise<boolean> {
|
||||||
downloadRootDir: `${context.filesDir}/_update`,
|
logger.debug(TAG, ',call setUuid');
|
||||||
packageVersion,
|
return UpdateModuleImpl.setUuid(this.context, uuid);
|
||||||
currentVersion,
|
|
||||||
buildTime,
|
|
||||||
isUsingBundleUrl,
|
|
||||||
isFirstTime,
|
|
||||||
rolledBackVersion,
|
|
||||||
uuid,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async setLocalHashInfo(hash: string, info: string): Promise<boolean> {
|
|
||||||
logger.debug(TAG, ",call setLocalHashInfo");
|
|
||||||
return UpdateModuleImpl.setLocalHashInfo(this.context,hash,info);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getLocalHashInfo(hash: string): Promise<string> {
|
|
||||||
return UpdateModuleImpl.getLocalHashInfo(this.context,hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
async setUuid(uuid: string): Promise<boolean> {
|
|
||||||
logger.debug(TAG, `,call setUuid`);
|
|
||||||
return UpdateModuleImpl.setUuid(this.context,uuid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async reloadUpdate(options: { hash: string }): Promise<void> {
|
async reloadUpdate(options: { hash: string }): Promise<void> {
|
||||||
logger.debug(TAG, `,call reloadUpdate`);
|
logger.debug(TAG, ',call reloadUpdate');
|
||||||
return UpdateModuleImpl.reloadUpdate(this.context, this.mUiCtx, options);
|
return UpdateModuleImpl.reloadUpdate(this.context, this.mUiCtx, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async setNeedUpdate(options: { hash: string }): Promise<boolean> {
|
async setNeedUpdate(options: { hash: string }): Promise<boolean> {
|
||||||
logger.debug(TAG, `,call setNeedUpdate`);
|
logger.debug(TAG, ',call setNeedUpdate');
|
||||||
return UpdateModuleImpl.setNeedUpdate(this.context, options);
|
return UpdateModuleImpl.setNeedUpdate(this.context, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async markSuccess(): Promise<boolean> {
|
async markSuccess(): Promise<boolean> {
|
||||||
logger.debug(TAG, `,call markSuccess`);
|
logger.debug(TAG, ',call markSuccess');
|
||||||
return UpdateModuleImpl.markSuccess(this.context);
|
return UpdateModuleImpl.markSuccess(this.context);
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadPatchFromPpk(options: { updateUrl: string; hash: string; originHash: string }): Promise<void> {
|
async downloadPatchFromPpk(options: {
|
||||||
logger.debug(TAG, `,call downloadPatchFromPpk`);
|
updateUrl: string;
|
||||||
|
hash: string;
|
||||||
|
originHash: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
logger.debug(TAG, ',call downloadPatchFromPpk');
|
||||||
return UpdateModuleImpl.downloadPatchFromPpk(this.context, options);
|
return UpdateModuleImpl.downloadPatchFromPpk(this.context, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadPatchFromPackage(options: { updateUrl: string; hash: string }): Promise<void> {
|
async downloadPatchFromPackage(options: {
|
||||||
logger.debug(TAG, `,call downloadPatchFromPackage`);
|
updateUrl: string;
|
||||||
|
hash: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
logger.debug(TAG, ',call downloadPatchFromPackage');
|
||||||
return UpdateModuleImpl.downloadPatchFromPackage(this.context, options);
|
return UpdateModuleImpl.downloadPatchFromPackage(this.context, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadFullUpdate(options: { updateUrl: string; hash: string }): Promise<void> {
|
async downloadFullUpdate(options: {
|
||||||
logger.debug(TAG, `,call downloadFullUpdate`);
|
updateUrl: string;
|
||||||
|
hash: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
logger.debug(TAG, ',call downloadFullUpdate');
|
||||||
return UpdateModuleImpl.downloadFullUpdate(this.context, options);
|
return UpdateModuleImpl.downloadFullUpdate(this.context, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadAndInstallApk(options: { url: string; target: string; hash: string }): Promise<void> {
|
async downloadAndInstallApk(options: {
|
||||||
logger.debug(TAG, `,call downloadAndInstallApk`);
|
url: string;
|
||||||
|
target: string;
|
||||||
|
hash: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
logger.debug(TAG, ',call downloadAndInstallApk');
|
||||||
return UpdateModuleImpl.downloadAndInstallApk(this.mUiCtx, options);
|
return UpdateModuleImpl.downloadAndInstallApk(this.mUiCtx, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
addListener(eventName: string): void {
|
addListener(_eventName: string): void {
|
||||||
logger.debug(TAG, `,call addListener`);
|
logger.debug(TAG, ',call addListener');
|
||||||
}
|
}
|
||||||
|
|
||||||
removeListeners(count: number): void {
|
removeListeners(_count: number): void {
|
||||||
logger.debug(TAG, `,call removeListeners`);
|
logger.debug(TAG, ',call removeListeners');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,249 +6,271 @@ import common from '@ohos.app.ability.common';
|
|||||||
import { DownloadTaskParams } from './DownloadTaskParams';
|
import { DownloadTaskParams } from './DownloadTaskParams';
|
||||||
|
|
||||||
export class UpdateContext {
|
export class UpdateContext {
|
||||||
private context: common.UIAbilityContext;
|
private context: common.UIAbilityContext;
|
||||||
private rootDir: string;
|
private rootDir: string;
|
||||||
private preferences: preferences.Preferences;
|
private preferences: preferences.Preferences;
|
||||||
private static DEBUG: boolean = false;
|
private static DEBUG: boolean = false;
|
||||||
private static isUsingBundleUrl: boolean = false;
|
private static isUsingBundleUrl: boolean = false;
|
||||||
|
|
||||||
constructor(context: common.UIAbilityContext) {
|
constructor(context: common.UIAbilityContext) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.rootDir = context.filesDir + '/_update';
|
this.rootDir = context.filesDir + '/_update';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!fileIo.accessSync(this.rootDir)) {
|
if (!fileIo.accessSync(this.rootDir)) {
|
||||||
fileIo.mkdirSync(this.rootDir);
|
fileIo.mkdirSync(this.rootDir);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to create root directory:', e);
|
console.error('Failed to create root directory:', e);
|
||||||
}
|
|
||||||
this.initPreferences();
|
|
||||||
}
|
}
|
||||||
|
this.initPreferences();
|
||||||
|
}
|
||||||
|
|
||||||
private initPreferences() {
|
private initPreferences() {
|
||||||
try {
|
try {
|
||||||
this.preferences = preferences.getPreferencesSync(this.context, {name:'update'});
|
this.preferences = preferences.getPreferencesSync(this.context, {
|
||||||
const packageVersion = this.getPackageVersion();
|
name: 'update',
|
||||||
const storedVersion = this.preferences.getSync('packageVersion', '');
|
});
|
||||||
if(!storedVersion){
|
const packageVersion = this.getPackageVersion();
|
||||||
this.preferences.putSync('packageVersion', packageVersion);
|
const storedVersion = this.preferences.getSync('packageVersion', '');
|
||||||
this.preferences.flush();
|
if (!storedVersion) {
|
||||||
} else if (storedVersion && packageVersion !== storedVersion) {
|
this.preferences.putSync('packageVersion', packageVersion);
|
||||||
this.preferences.clear();
|
|
||||||
this.preferences.putSync('packageVersion', packageVersion);
|
|
||||||
this.preferences.flush();
|
|
||||||
this.cleanUp();
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error('Failed to init preferences:', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public setKv(key: string, value: string): void {
|
|
||||||
this.preferences.putSync(key, value);
|
|
||||||
this.preferences.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
public getKv(key: string): string {
|
|
||||||
return this.preferences.getSync(key, '') as string;
|
|
||||||
}
|
|
||||||
|
|
||||||
public isFirstTime(): boolean {
|
|
||||||
return this.preferences.getSync('firstTime', false) as boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
public rolledBackVersion(): string {
|
|
||||||
return this.preferences.getSync('rolledBackVersion', '') as string;
|
|
||||||
}
|
|
||||||
|
|
||||||
public markSuccess(): void {
|
|
||||||
this.preferences.putSync('firstTimeOk', true);
|
|
||||||
const lastVersion = this.preferences.getSync('lastVersion', '') as string;
|
|
||||||
const curVersion = this.preferences.getSync('currentVersion', '') as string;
|
|
||||||
|
|
||||||
if (lastVersion && lastVersion !== curVersion) {
|
|
||||||
this.preferences.deleteSync('lastVersion');
|
|
||||||
this.preferences.deleteSync(`hash_${lastVersion}`);
|
|
||||||
}
|
|
||||||
this.preferences.flush();
|
this.preferences.flush();
|
||||||
|
} else if (storedVersion && packageVersion !== storedVersion) {
|
||||||
this.cleanUp();
|
this.cleanUp();
|
||||||
}
|
this.preferences.clear();
|
||||||
|
this.preferences.putSync('packageVersion', packageVersion);
|
||||||
public clearFirstTime(): void {
|
|
||||||
this.preferences.putSync('firstTime', false);
|
|
||||||
this.preferences.flush();
|
this.preferences.flush();
|
||||||
this.cleanUp();
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to init preferences:', e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public clearRollbackMark(): void {
|
public setKv(key: string, value: string): void {
|
||||||
this.preferences.putSync('rolledBackVersion', null);
|
this.preferences.putSync(key, value);
|
||||||
this.preferences.flush();
|
this.preferences.flush();
|
||||||
this.cleanUp();
|
}
|
||||||
|
|
||||||
|
public getKv(key: string): string {
|
||||||
|
return this.preferences.getSync(key, '') as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
public isFirstTime(): boolean {
|
||||||
|
return this.preferences.getSync('firstTime', false) as boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
public rolledBackVersion(): string {
|
||||||
|
return this.preferences.getSync('rolledBackVersion', '') as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
public markSuccess(): void {
|
||||||
|
this.preferences.putSync('firstTimeOk', true);
|
||||||
|
const lastVersion = this.preferences.getSync('lastVersion', '') as string;
|
||||||
|
const curVersion = this.preferences.getSync('currentVersion', '') as string;
|
||||||
|
|
||||||
|
if (lastVersion && lastVersion !== curVersion) {
|
||||||
|
this.preferences.deleteSync('lastVersion');
|
||||||
|
this.preferences.deleteSync(`hash_${lastVersion}`);
|
||||||
}
|
}
|
||||||
|
this.preferences.flush();
|
||||||
|
this.cleanUp();
|
||||||
|
}
|
||||||
|
|
||||||
public async downloadFullUpdate(url: string, hash: string, listener: DownloadFileListener): Promise<void> {
|
public clearFirstTime(): void {
|
||||||
try {
|
this.preferences.putSync('firstTime', false);
|
||||||
const params = new DownloadTaskParams();
|
this.preferences.flush();
|
||||||
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FULL;
|
this.cleanUp();
|
||||||
params.url = url;
|
}
|
||||||
params.hash = hash;
|
|
||||||
params.listener = listener;
|
public clearRollbackMark(): void {
|
||||||
params.targetFile = `${this.rootDir}/${hash}.ppk`;
|
this.preferences.putSync('rolledBackVersion', null);
|
||||||
const downloadTask = new DownloadTask(this.context);
|
this.preferences.flush();
|
||||||
await downloadTask.execute(params);
|
this.cleanUp();
|
||||||
} catch (e) {
|
}
|
||||||
console.error('Failed to download full update:', e);
|
|
||||||
|
public async downloadFullUpdate(
|
||||||
|
url: string,
|
||||||
|
hash: string,
|
||||||
|
listener: DownloadFileListener,
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const params = new DownloadTaskParams();
|
||||||
|
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FULL;
|
||||||
|
params.url = url;
|
||||||
|
params.hash = hash;
|
||||||
|
params.listener = listener;
|
||||||
|
params.targetFile = `${this.rootDir}/${hash}.ppk`;
|
||||||
|
params.unzipDirectory = `${this.rootDir}/${hash}`;
|
||||||
|
const downloadTask = new DownloadTask(this.context);
|
||||||
|
await downloadTask.execute(params);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to download full update:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async downloadFile(
|
||||||
|
url: string,
|
||||||
|
hash: string,
|
||||||
|
fileName: string,
|
||||||
|
listener: DownloadFileListener,
|
||||||
|
): Promise<void> {
|
||||||
|
const params = new DownloadTaskParams();
|
||||||
|
params.type = DownloadTaskParams.TASK_TYPE_PLAIN_DOWNLOAD;
|
||||||
|
params.url = url;
|
||||||
|
params.hash = hash;
|
||||||
|
params.listener = listener;
|
||||||
|
params.targetFile = this.rootDir + '/' + fileName;
|
||||||
|
|
||||||
|
const downloadTask = new DownloadTask(this.context);
|
||||||
|
await downloadTask.execute(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async downloadPatchFromPpk(
|
||||||
|
url: string,
|
||||||
|
hash: string,
|
||||||
|
originHash: string,
|
||||||
|
listener: DownloadFileListener,
|
||||||
|
): Promise<void> {
|
||||||
|
const params = new DownloadTaskParams();
|
||||||
|
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_PPK;
|
||||||
|
params.url = url;
|
||||||
|
params.hash = hash;
|
||||||
|
params.originHash = originHash;
|
||||||
|
params.listener = listener;
|
||||||
|
params.targetFile = `${this.rootDir}/${originHash}_${hash}.ppk.patch`;
|
||||||
|
params.unzipDirectory = `${this.rootDir}/${hash}`;
|
||||||
|
params.originDirectory = `${this.rootDir}/${params.originHash}`;
|
||||||
|
|
||||||
|
const downloadTask = new DownloadTask(this.context);
|
||||||
|
await downloadTask.execute(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async downloadPatchFromPackage(
|
||||||
|
url: string,
|
||||||
|
hash: string,
|
||||||
|
listener: DownloadFileListener,
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const params = new DownloadTaskParams();
|
||||||
|
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_APP;
|
||||||
|
params.url = url;
|
||||||
|
params.hash = hash;
|
||||||
|
params.listener = listener;
|
||||||
|
params.targetFile = `${this.rootDir}/${hash}.app.patch`;
|
||||||
|
params.unzipDirectory = `${this.rootDir}/${hash}`;
|
||||||
|
|
||||||
|
const downloadTask = new DownloadTask(this.context);
|
||||||
|
return await downloadTask.execute(params);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to download APK patch:', e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public switchVersion(hash: string): void {
|
||||||
|
try {
|
||||||
|
const bundlePath = `${this.rootDir}/${hash}/bundle.harmony.js`;
|
||||||
|
if (!fileIo.accessSync(bundlePath)) {
|
||||||
|
throw Error(`Bundle version ${hash} not found.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastVersion = this.getKv('currentVersion');
|
||||||
|
this.setKv('currentVersion', hash);
|
||||||
|
if (lastVersion && lastVersion !== hash) {
|
||||||
|
this.setKv('lastVersion', lastVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setKv('firstTime', 'true');
|
||||||
|
this.setKv('firstTimeOk', 'false');
|
||||||
|
this.setKv('rolledBackVersion', '');
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to switch version:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getBundleUrl() {
|
||||||
|
UpdateContext.isUsingBundleUrl = true;
|
||||||
|
const currentVersion = this.getCurrentVersion();
|
||||||
|
if (!currentVersion) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
if (!this.isFirstTime()) {
|
||||||
|
if (!this.preferences.getSync('firstTimeOk', true)) {
|
||||||
|
return this.rollBack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let version = currentVersion;
|
||||||
|
while (version) {
|
||||||
|
const bundleFile = `${this.rootDir}/${version}/bundle.harmony.js`;
|
||||||
|
try {
|
||||||
|
if (!fileIo.accessSync(bundleFile)) {
|
||||||
|
console.error(`Bundle version ${version} not found.`);
|
||||||
|
version = this.rollBack();
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
return bundleFile;
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to access bundle file:', e);
|
||||||
|
version = this.rollBack();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
public async downloadFile(url: string, hash: string, fileName: string, listener: DownloadFileListener): Promise<void> {
|
getPackageVersion(): string {
|
||||||
const params = new DownloadTaskParams();
|
let bundleFlags =
|
||||||
params.type = DownloadTaskParams.TASK_TYPE_PLAIN_DOWNLOAD;
|
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
|
||||||
params.url = url;
|
let packageVersion = '';
|
||||||
params.hash = hash;
|
try {
|
||||||
params.listener = listener;
|
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
|
||||||
params.targetFile = this.rootDir + '/' + fileName;
|
packageVersion = bundleInfo?.versionName || 'Unknown';
|
||||||
|
} catch (error) {
|
||||||
const downloadTask = new DownloadTask(this.context);
|
console.error('获取包信息失败:', error);
|
||||||
await downloadTask.execute(params);
|
|
||||||
}
|
}
|
||||||
|
return packageVersion;
|
||||||
|
}
|
||||||
|
|
||||||
public async downloadPatchFromPpk(url: string, hash: string, originHash: string, listener: DownloadFileListener): Promise<void> {
|
public getCurrentVersion(): string {
|
||||||
const params = new DownloadTaskParams();
|
const currentVersion = this.getKv('currentVersion');
|
||||||
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_PPK;
|
return currentVersion;
|
||||||
params.url = url;
|
}
|
||||||
params.hash = hash;
|
|
||||||
params.originHash = originHash;
|
private rollBack(): string {
|
||||||
params.listener = listener;
|
const lastVersion = this.preferences.getSync('lastVersion', '') as string;
|
||||||
params.targetFile = `${this.rootDir}/${originHash}_${hash}.ppk.patch`;
|
const currentVersion = this.preferences.getSync(
|
||||||
params.unzipDirectory = `${this.rootDir}/${hash}`;
|
'currentVersion',
|
||||||
params.originDirectory = `${this.rootDir}/${params.originHash}`;
|
'',
|
||||||
|
) as string;
|
||||||
const downloadTask = new DownloadTask(this.context);
|
if (!lastVersion) {
|
||||||
await downloadTask.execute(params);
|
this.preferences.deleteSync('currentVersion');
|
||||||
|
} else {
|
||||||
|
this.preferences.deleteSync('lastVersion');
|
||||||
|
this.preferences.putSync('currentVersion', lastVersion);
|
||||||
}
|
}
|
||||||
|
this.preferences.putSync('firstTimeOk', true);
|
||||||
|
this.preferences.putSync('firstTime', false);
|
||||||
|
this.preferences.putSync('rolledBackVersion', currentVersion);
|
||||||
|
this.preferences.flush();
|
||||||
|
return lastVersion;
|
||||||
|
}
|
||||||
|
|
||||||
public async downloadPatchFromPackage(url: string, hash: string, listener: DownloadFileListener): Promise<void> {
|
private cleanUp(): void {
|
||||||
try {
|
const params = new DownloadTaskParams();
|
||||||
const params = new DownloadTaskParams();
|
params.type = DownloadTaskParams.TASK_TYPE_CLEANUP;
|
||||||
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_APP;
|
params.hash = this.preferences.getSync('currentVersion', '') as string;
|
||||||
params.url = url;
|
params.originHash = this.preferences.getSync('lastVersion', '') as string;
|
||||||
params.hash = hash;
|
params.unzipDirectory = this.rootDir;
|
||||||
params.listener = listener;
|
const downloadTask = new DownloadTask(this.context);
|
||||||
params.targetFile = `${this.rootDir}/${hash}.app.patch`;
|
downloadTask.execute(params);
|
||||||
params.unzipDirectory = `${this.rootDir}/${hash}`;
|
}
|
||||||
|
|
||||||
const downloadTask = new DownloadTask(this.context);
|
public getIsUsingBundleUrl(): boolean {
|
||||||
return await downloadTask.execute(params);
|
return UpdateContext.isUsingBundleUrl;
|
||||||
} catch (e) {
|
}
|
||||||
throw e;
|
|
||||||
console.error('Failed to download APK patch:', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public switchVersion(hash: string): void {
|
|
||||||
try {
|
|
||||||
const bundlePath = `${this.rootDir}/${hash}/bundle.harmony.js`;
|
|
||||||
if (!fileIo.accessSync(bundlePath)) {
|
|
||||||
throw new Error(`Bundle version ${hash} not found.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const lastVersion = this.getKv('currentVersion');
|
|
||||||
this.setKv('currentVersion', hash);
|
|
||||||
if (lastVersion && lastVersion !== hash) {
|
|
||||||
this.setKv('lastVersion', lastVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setKv('firstTime', 'true');
|
|
||||||
this.setKv('firstTimeOk', 'false');
|
|
||||||
this.setKv('rolledBackVersion', "");
|
|
||||||
} catch (e) {
|
|
||||||
console.error('Failed to switch version:', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static getBundleUrl(context: common.UIAbilityContext, defaultAssetsUrl?: string): string {
|
|
||||||
return new UpdateContext(context).getBundleUrl(defaultAssetsUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getBundleUrl(defaultAssetsUrl?: string): string {
|
|
||||||
UpdateContext.isUsingBundleUrl = true;
|
|
||||||
const currentVersion = this.getCurrentVersion();
|
|
||||||
if (!currentVersion) {
|
|
||||||
return defaultAssetsUrl;
|
|
||||||
}
|
|
||||||
if (!this.isFirstTime()) {
|
|
||||||
if (!this.preferences.getSync('firstTimeOk', true)) {
|
|
||||||
return this.rollBack();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let version = currentVersion;
|
|
||||||
while (version) {
|
|
||||||
const bundleFile = `${this.rootDir}/${version}/bundle.harmony.js`;
|
|
||||||
try {
|
|
||||||
if (!fileIo.accessSync(bundleFile)) {
|
|
||||||
console.error(`Bundle version ${version} not found.`);
|
|
||||||
version = this.rollBack();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
return bundleFile;
|
|
||||||
} catch (e) {
|
|
||||||
console.error('Failed to access bundle file:', e);
|
|
||||||
version = this.rollBack();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultAssetsUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
getPackageVersion(): string {
|
|
||||||
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
|
|
||||||
let packageVersion = '';
|
|
||||||
try {
|
|
||||||
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
|
|
||||||
packageVersion = bundleInfo?.versionName || "Unknown";
|
|
||||||
} catch (error) {
|
|
||||||
console.error("获取包信息失败:", error);
|
|
||||||
}
|
|
||||||
return packageVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getCurrentVersion() : string {
|
|
||||||
const currentVersion = this.getKv('currentVersion');
|
|
||||||
return currentVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
private rollBack(): string {
|
|
||||||
const lastVersion = this.preferences.getSync('lastVersion', '') as string;
|
|
||||||
const currentVersion = this.preferences.getSync('currentVersion', '') as string;
|
|
||||||
if (!lastVersion) {
|
|
||||||
this.preferences.deleteSync('currentVersion');
|
|
||||||
} else {
|
|
||||||
this.preferences.putSync('currentVersion', lastVersion);
|
|
||||||
}
|
|
||||||
this.preferences.putSync('firstTimeOk', true);
|
|
||||||
this.preferences.putSync('firstTime', false);
|
|
||||||
this.preferences.putSync('rolledBackVersion', currentVersion);
|
|
||||||
this.preferences.flush();
|
|
||||||
return lastVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
private cleanUp(): void {
|
|
||||||
const params = new DownloadTaskParams();
|
|
||||||
params.type = DownloadTaskParams.TASK_TYPE_CLEANUP;
|
|
||||||
params.hash = this.preferences.getSync('currentVersion', '') as string;
|
|
||||||
params.originHash = this.preferences.getSync('lastVersion', '') as string;
|
|
||||||
params.unzipDirectory = this.rootDir;
|
|
||||||
const downloadTask = new DownloadTask(this.context);
|
|
||||||
downloadTask.execute(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getIsUsingBundleUrl(): boolean {
|
|
||||||
return UpdateContext.isUsingBundleUrl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DownloadFileListener {
|
export interface DownloadFileListener {
|
||||||
onDownloadCompleted(params: DownloadTaskParams): void;
|
onDownloadCompleted(params: DownloadTaskParams): void;
|
||||||
onDownloadFailed(error: Error): void;
|
onDownloadFailed(error: Error): void;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,14 @@ import { UpdateContext } from './UpdateContext';
|
|||||||
import { DownloadTaskParams } from './DownloadTaskParams';
|
import { DownloadTaskParams } from './DownloadTaskParams';
|
||||||
import logger from './Logger';
|
import logger from './Logger';
|
||||||
|
|
||||||
const TAG = "UpdateModuleImpl";
|
const TAG = 'UpdateModuleImpl';
|
||||||
|
|
||||||
export class UpdateModuleImpl {
|
export class UpdateModuleImpl {
|
||||||
static readonly NAME = "Pushy";
|
static readonly NAME = 'Pushy';
|
||||||
|
|
||||||
static async downloadFullUpdate(
|
static async downloadFullUpdate(
|
||||||
updateContext: UpdateContext,
|
updateContext: UpdateContext,
|
||||||
options: { updateUrl: string; hash: string }
|
options: { updateUrl: string; hash: string },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await updateContext.downloadFullUpdate(options.updateUrl, options.hash, {
|
await updateContext.downloadFullUpdate(options.updateUrl, options.hash, {
|
||||||
@@ -20,7 +20,7 @@ export class UpdateModuleImpl {
|
|||||||
},
|
},
|
||||||
onDownloadFailed: (error: Error) => {
|
onDownloadFailed: (error: Error) => {
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(TAG, `downloadFullUpdate failed: ${error}`);
|
logger.error(TAG, `downloadFullUpdate failed: ${error}`);
|
||||||
@@ -30,18 +30,18 @@ export class UpdateModuleImpl {
|
|||||||
|
|
||||||
static async downloadAndInstallApk(
|
static async downloadAndInstallApk(
|
||||||
context: common.UIAbilityContext,
|
context: common.UIAbilityContext,
|
||||||
options: { url: string; hash: string; target: string }
|
options: { url: string; hash: string; target: string },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const want = {
|
const want = {
|
||||||
action: 'action.system.home',
|
action: 'action.system.home',
|
||||||
parameters: {
|
parameters: {
|
||||||
uri: 'appmarket://details'
|
uri: 'appmarket://details',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!context) {
|
if (!context) {
|
||||||
throw new Error('获取context失败');
|
throw Error('获取context失败');
|
||||||
}
|
}
|
||||||
|
|
||||||
await context.startAbility(want);
|
await context.startAbility(want);
|
||||||
@@ -53,17 +53,21 @@ export class UpdateModuleImpl {
|
|||||||
|
|
||||||
static async downloadPatchFromPackage(
|
static async downloadPatchFromPackage(
|
||||||
updateContext: UpdateContext,
|
updateContext: UpdateContext,
|
||||||
options: { updateUrl: string; hash: string }
|
options: { updateUrl: string; hash: string },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
return await updateContext.downloadPatchFromPackage(options.updateUrl, options.hash, {
|
return await updateContext.downloadPatchFromPackage(
|
||||||
onDownloadCompleted: (params: DownloadTaskParams) => {
|
options.updateUrl,
|
||||||
return Promise.resolve();
|
options.hash,
|
||||||
|
{
|
||||||
|
onDownloadCompleted: (params: DownloadTaskParams) => {
|
||||||
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
onDownloadFailed: (error: Error) => {
|
||||||
|
return Promise.reject(error);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
onDownloadFailed: (error: Error) => {
|
);
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(TAG, `downloadPatchFromPackage failed: ${error}`);
|
logger.error(TAG, `downloadPatchFromPackage failed: ${error}`);
|
||||||
throw error;
|
throw error;
|
||||||
@@ -72,7 +76,7 @@ export class UpdateModuleImpl {
|
|||||||
|
|
||||||
static async downloadPatchFromPpk(
|
static async downloadPatchFromPpk(
|
||||||
updateContext: UpdateContext,
|
updateContext: UpdateContext,
|
||||||
options: { updateUrl: string; hash: string; originHash: string }
|
options: { updateUrl: string; hash: string; originHash: string },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await updateContext.downloadPatchFromPpk(
|
await updateContext.downloadPatchFromPpk(
|
||||||
@@ -85,49 +89,49 @@ export class UpdateModuleImpl {
|
|||||||
},
|
},
|
||||||
onDownloadFailed: (error: Error) => {
|
onDownloadFailed: (error: Error) => {
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(TAG, `downloadPatchFromPpk failed: ${error}`);
|
logger.error(TAG, `downloadPatchFromPpk failed: ${error}`);
|
||||||
throw new Error(`执行报错: ${error.message}`);
|
throw Error(`执行报错: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async reloadUpdate(
|
static async reloadUpdate(
|
||||||
updateContext: UpdateContext,
|
updateContext: UpdateContext,
|
||||||
context: common.UIAbilityContext,
|
context: common.UIAbilityContext,
|
||||||
options: { hash: string }
|
options: { hash: string },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const hash = options.hash;
|
const hash = options.hash;
|
||||||
if (!hash) {
|
if (!hash) {
|
||||||
throw new Error('hash不能为空');
|
throw Error('hash不能为空');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await updateContext.switchVersion(hash);
|
await updateContext.switchVersion(hash);
|
||||||
const bundleInfo = await bundleManager.getBundleInfoForSelf(
|
const bundleInfo = await bundleManager.getBundleInfoForSelf(
|
||||||
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION
|
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION,
|
||||||
);
|
);
|
||||||
await context.terminateSelf();
|
await context.terminateSelf();
|
||||||
const want = {
|
const want = {
|
||||||
bundleName: bundleInfo.name,
|
bundleName: bundleInfo.name,
|
||||||
abilityName: context.abilityInfo?.name
|
abilityName: context.abilityInfo?.name,
|
||||||
};
|
};
|
||||||
await context.startAbility(want);
|
await context.startAbility(want);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(TAG, `reloadUpdate failed: ${error}`);
|
logger.error(TAG, `reloadUpdate failed: ${error}`);
|
||||||
throw new Error(`pushy:switchVersion failed ${error.message}`);
|
throw Error(`pushy:switchVersion failed ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async setNeedUpdate(
|
static async setNeedUpdate(
|
||||||
updateContext: UpdateContext,
|
updateContext: UpdateContext,
|
||||||
options: { hash: string }
|
options: { hash: string },
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const hash = options.hash;
|
const hash = options.hash;
|
||||||
if (!hash) {
|
if (!hash) {
|
||||||
throw new Error('hash不能为空');
|
throw Error('hash不能为空');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -135,7 +139,7 @@ export class UpdateModuleImpl {
|
|||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(TAG, `setNeedUpdate failed: ${error}`);
|
logger.error(TAG, `setNeedUpdate failed: ${error}`);
|
||||||
throw new Error(`switchVersionLater failed: ${error.message}`);
|
throw Error(`switchVersionLater failed: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,20 +149,20 @@ export class UpdateModuleImpl {
|
|||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(TAG, `markSuccess failed: ${error}`);
|
logger.error(TAG, `markSuccess failed: ${error}`);
|
||||||
throw new Error(`执行报错: ${error.message}`);
|
throw Error(`执行报错: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async setUuid(
|
static async setUuid(
|
||||||
updateContext: UpdateContext,
|
updateContext: UpdateContext,
|
||||||
uuid: string
|
uuid: string,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
await updateContext.setKv('uuid', uuid);
|
await updateContext.setKv('uuid', uuid);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(TAG, `setUuid failed: ${error}`);
|
logger.error(TAG, `setUuid failed: ${error}`);
|
||||||
throw new Error(`执行报错: ${error.message}`);
|
throw Error(`执行报错: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,27 +175,17 @@ export class UpdateModuleImpl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async setLocalHashInfo(
|
static setLocalHashInfo(
|
||||||
updateContext: UpdateContext,
|
updateContext: UpdateContext,
|
||||||
hash: string,
|
hash: string,
|
||||||
info: string
|
info: string,
|
||||||
): Promise<boolean> {
|
): boolean {
|
||||||
if (!this.checkJson(info)) {
|
updateContext.setKv(`hash_${hash}`, info);
|
||||||
await updateContext.setKv(`hash_${hash}`, info);
|
|
||||||
throw new Error('校验报错:json字符串格式错误');
|
|
||||||
}
|
|
||||||
await updateContext.setKv(`hash_${hash}`, info);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getLocalHashInfo(
|
static getLocalHashInfo(updateContext: UpdateContext, hash: string): string {
|
||||||
updateContext: UpdateContext,
|
const value = updateContext.getKv(`hash_${hash}`);
|
||||||
hash: string
|
|
||||||
): Promise<string> {
|
|
||||||
const value = await updateContext.getKv(`hash_${hash}`);
|
|
||||||
if (!this.checkJson(value)) {
|
|
||||||
throw new Error('校验报错:json字符串格式错误');
|
|
||||||
}
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
static NSString *const keyPushyInfo = @"REACTNATIVECN_PUSHY_INFO_KEY";
|
static NSString *const keyPushyInfo = @"REACTNATIVECN_PUSHY_INFO_KEY";
|
||||||
static NSString *const paramPackageVersion = @"packageVersion";
|
static NSString *const paramPackageVersion = @"packageVersion";
|
||||||
|
static NSString *const paramBuildTime = @"buildTime";
|
||||||
static NSString *const paramLastVersion = @"lastVersion";
|
static NSString *const paramLastVersion = @"lastVersion";
|
||||||
static NSString *const paramCurrentVersion = @"currentVersion";
|
static NSString *const paramCurrentVersion = @"currentVersion";
|
||||||
static NSString *const paramIsFirstTime = @"isFirstTime";
|
static NSString *const paramIsFirstTime = @"isFirstTime";
|
||||||
@@ -70,20 +71,36 @@ RCT_EXPORT_MODULE(RCTPushy);
|
|||||||
{
|
{
|
||||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
|
|
||||||
|
// Check for version changes first
|
||||||
|
NSString *curPackageVersion = [RCTPushy packageVersion];
|
||||||
|
NSString *curBuildTime = [RCTPushy buildTime];
|
||||||
|
NSString *storedPackageVersion = [defaults stringForKey:paramPackageVersion];
|
||||||
|
NSString *storedBuildTime = [defaults stringForKey:paramBuildTime];
|
||||||
|
|
||||||
|
// If stored versions don't exist, write current versions first
|
||||||
|
if (!storedPackageVersion && !storedBuildTime) {
|
||||||
|
[defaults setObject:curPackageVersion forKey:paramPackageVersion];
|
||||||
|
[defaults setObject:curBuildTime forKey:paramBuildTime];
|
||||||
|
storedPackageVersion = curPackageVersion;
|
||||||
|
storedBuildTime = curBuildTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL packageVersionChanged = ![curPackageVersion isEqualToString:storedPackageVersion];
|
||||||
|
BOOL buildTimeChanged = curBuildTime && ![curBuildTime isEqualToString:storedBuildTime];
|
||||||
|
|
||||||
|
if (packageVersionChanged || buildTimeChanged) {
|
||||||
|
// Clear all update data and store new versions
|
||||||
|
[defaults setObject:nil forKey:keyPushyInfo];
|
||||||
|
[defaults setObject:nil forKey:keyHashInfo];
|
||||||
|
[defaults setObject:@(YES) forKey:KeyPackageUpdatedMarked];
|
||||||
|
[defaults setObject:curPackageVersion forKey:paramPackageVersion];
|
||||||
|
[defaults setObject:curBuildTime forKey:paramBuildTime];
|
||||||
|
|
||||||
|
// ...need clear files later
|
||||||
|
}
|
||||||
|
|
||||||
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
||||||
if (pushyInfo) {
|
if (pushyInfo) {
|
||||||
NSString *curPackageVersion = [RCTPushy packageVersion];
|
|
||||||
NSString *packageVersion = [pushyInfo objectForKey:paramPackageVersion];
|
|
||||||
|
|
||||||
BOOL needClearPushyInfo = ![curPackageVersion isEqualToString:packageVersion];
|
|
||||||
if (needClearPushyInfo) {
|
|
||||||
[defaults setObject:nil forKey:keyPushyInfo];
|
|
||||||
[defaults setObject:nil forKey:keyHashInfo];
|
|
||||||
[defaults setObject:@(YES) forKey:KeyPackageUpdatedMarked];
|
|
||||||
|
|
||||||
// ...need clear files later
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
NSString *curVersion = pushyInfo[paramCurrentVersion];
|
NSString *curVersion = pushyInfo[paramCurrentVersion];
|
||||||
|
|
||||||
BOOL isFirstTime = [pushyInfo[paramIsFirstTime] boolValue];
|
BOOL isFirstTime = [pushyInfo[paramIsFirstTime] boolValue];
|
||||||
@@ -115,7 +132,6 @@ RCT_EXPORT_MODULE(RCTPushy);
|
|||||||
loadVersion = [self rollback];
|
loadVersion = [self rollback];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [RCTPushy binaryBundleURL];
|
return [RCTPushy binaryBundleURL];
|
||||||
@@ -127,13 +143,11 @@ RCT_EXPORT_MODULE(RCTPushy);
|
|||||||
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
||||||
NSString *lastVersion = pushyInfo[paramLastVersion];
|
NSString *lastVersion = pushyInfo[paramLastVersion];
|
||||||
NSString *curVersion = pushyInfo[paramCurrentVersion];
|
NSString *curVersion = pushyInfo[paramCurrentVersion];
|
||||||
NSString *curPackageVersion = [RCTPushy packageVersion];
|
|
||||||
if (lastVersion.length) {
|
if (lastVersion.length) {
|
||||||
// roll back to last version
|
// roll back to last version
|
||||||
[defaults setObject:@{paramCurrentVersion:lastVersion,
|
[defaults setObject:@{paramCurrentVersion:lastVersion,
|
||||||
paramIsFirstTime:@(NO),
|
paramIsFirstTime:@(NO),
|
||||||
paramIsFirstLoadOk:@(YES),
|
paramIsFirstLoadOk:@(YES)}
|
||||||
paramPackageVersion:curPackageVersion}
|
|
||||||
forKey:keyPushyInfo];
|
forKey:keyPushyInfo];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -162,7 +176,11 @@ RCT_EXPORT_MODULE(RCTPushy);
|
|||||||
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked];
|
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked];
|
||||||
ret[@"uuid"] = [defaults objectForKey:keyUuid];
|
ret[@"uuid"] = [defaults objectForKey:keyUuid];
|
||||||
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
||||||
ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion];
|
NSString *currentVersion = [pushyInfo objectForKey:paramCurrentVersion];
|
||||||
|
ret[@"currentVersion"] = currentVersion;
|
||||||
|
if (currentVersion != nil) {
|
||||||
|
ret[@"currentVersionInfo"] = [defaults objectForKey:[keyHashInfo stringByAppendingString:currentVersion]];
|
||||||
|
}
|
||||||
|
|
||||||
// clear isFirstTimemarked
|
// clear isFirstTimemarked
|
||||||
if (ret[@"isFirstTime"]) {
|
if (ret[@"isFirstTime"]) {
|
||||||
@@ -286,8 +304,8 @@ RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options
|
|||||||
if (hash.length) {
|
if (hash.length) {
|
||||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
NSString *lastVersion = nil;
|
NSString *lastVersion = nil;
|
||||||
if ([defaults objectForKey:keyPushyInfo]) {
|
NSDictionary *pushyInfo = [defaults objectForKey:keyPushyInfo];
|
||||||
NSDictionary *pushyInfo = [defaults objectForKey:keyPushyInfo];
|
if (pushyInfo) {
|
||||||
lastVersion = pushyInfo[paramCurrentVersion];
|
lastVersion = pushyInfo[paramCurrentVersion];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,7 +314,6 @@ RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options
|
|||||||
newInfo[paramLastVersion] = lastVersion;
|
newInfo[paramLastVersion] = lastVersion;
|
||||||
newInfo[paramIsFirstTime] = @(YES);
|
newInfo[paramIsFirstTime] = @(YES);
|
||||||
newInfo[paramIsFirstLoadOk] = @(NO);
|
newInfo[paramIsFirstLoadOk] = @(NO);
|
||||||
newInfo[paramPackageVersion] = [RCTPushy packageVersion];
|
|
||||||
[defaults setObject:newInfo forKey:keyPushyInfo];
|
[defaults setObject:newInfo forKey:keyPushyInfo];
|
||||||
|
|
||||||
|
|
||||||
@@ -662,4 +679,4 @@ RCT_EXPORT_METHOD(markSuccess:(RCTPromiseResolveBlock)resolve
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update",
|
"name": "react-native-update",
|
||||||
"version": "10.29.9",
|
"version": "10.36.0",
|
||||||
"description": "react-native hot update",
|
"description": "react-native hot update",
|
||||||
"main": "src/index",
|
"main": "src/index",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
"lint": "eslint \"src/*.@(ts|tsx|js|jsx)\" && tsc --noEmit",
|
"lint": "eslint \"src/*.@(ts|tsx|js|jsx)\" && tsc --noEmit",
|
||||||
"submodule": "git submodule update --init --recursive",
|
"submodule": "git submodule update --init --recursive",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"build-lib": "bun submodule && $ANDROID_HOME/ndk/20.1.5948944/ndk-build NDK_PROJECT_PATH=android APP_BUILD_SCRIPT=android/jni/Android.mk NDK_APPLICATION_MK=android/jni/Application.mk NDK_LIBS_OUT=android/lib",
|
"build:so": "bun submodule && $ANDROID_HOME/ndk/28.2.13676358/ndk-build NDK_PROJECT_PATH=android APP_BUILD_SCRIPT=android/jni/Android.mk NDK_APPLICATION_MK=android/jni/Application.mk NDK_LIBS_OUT=android/lib",
|
||||||
"build:ios-debug": "cd Example/testHotUpdate && bun && detox build --configuration ios.sim.debug",
|
"build:ios-debug": "cd Example/testHotUpdate && bun && detox build --configuration ios.sim.debug",
|
||||||
"build:ios-release": "cd Example/testHotUpdate && bun && detox build --configuration ios.sim.release",
|
"build:ios-release": "cd Example/testHotUpdate && bun && detox build --configuration ios.sim.release",
|
||||||
"test:ios-debug": "cd Example/testHotUpdate && detox test --configuration ios.sim.debug",
|
"test:ios-debug": "cd Example/testHotUpdate && detox test --configuration ios.sim.debug",
|
||||||
|
|||||||
@@ -105,18 +105,17 @@ Pod::Spec.new do |s|
|
|||||||
|
|
||||||
# Conditionally add Expo dependency
|
# Conditionally add Expo dependency
|
||||||
if valid_expo_project
|
if valid_expo_project
|
||||||
s.public_header_files = ['ios/ImportReact.h']
|
|
||||||
s.dependency 'ExpoModulesCore'
|
s.dependency 'ExpoModulesCore'
|
||||||
end
|
end
|
||||||
|
|
||||||
s.subspec 'RCTPushy' do |ss|
|
s.subspec 'RCTPushy' do |ss|
|
||||||
ss.source_files = ['ios/RCTPushy/**/*.{h,m,mm,c}',
|
ss.source_files = ['ios/**/*.{h,m,mm,c}',
|
||||||
'android/jni/hpatch.{h,c}',
|
'android/jni/hpatch.{h,c}',
|
||||||
'android/jni/HDiffPatch/libHDiffPatch/HPatch/*.{h,c}',
|
'android/jni/HDiffPatch/libHDiffPatch/HPatch/*.{h,c}',
|
||||||
'android/jni/HDiffPatch/file_for_patch.{h,c}',
|
'android/jni/HDiffPatch/file_for_patch.{h,c}',
|
||||||
'android/jni/lzma/C/LzmaDec.{h,c}',
|
'android/jni/lzma/C/LzmaDec.{h,c}',
|
||||||
'android/jni/lzma/C/Lzma2Dec.{h,c}']
|
'android/jni/lzma/C/Lzma2Dec.{h,c}']
|
||||||
ss.public_header_files = ['ios/RCTPushy/**/*.h']
|
ss.public_header_files = ['ios/**/*.h']
|
||||||
end
|
end
|
||||||
|
|
||||||
# Conditionally add Expo subspec and check ExpoModulesCore version
|
# Conditionally add Expo subspec and check ExpoModulesCore version
|
||||||
|
|||||||
1
react-native.config.js
Normal file
1
react-native.config.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
module.exports = {};
|
||||||
@@ -10,6 +10,7 @@ export interface Spec extends TurboModule {
|
|||||||
buildTime: string;
|
buildTime: string;
|
||||||
uuid: string;
|
uuid: string;
|
||||||
isUsingBundleUrl: boolean;
|
isUsingBundleUrl: boolean;
|
||||||
|
currentVersionInfo: string;
|
||||||
};
|
};
|
||||||
setLocalHashInfo(hash: string, info: string): Promise<void>;
|
setLocalHashInfo(hash: string, info: string): Promise<void>;
|
||||||
getLocalHashInfo(hash: string): Promise<string>;
|
getLocalHashInfo(hash: string): Promise<string>;
|
||||||
|
|||||||
100
src/client.ts
100
src/client.ts
@@ -8,7 +8,7 @@ import {
|
|||||||
buildTime,
|
buildTime,
|
||||||
cInfo,
|
cInfo,
|
||||||
currentVersion,
|
currentVersion,
|
||||||
getCurrentVersionInfo,
|
currentVersionInfo,
|
||||||
isFirstTime,
|
isFirstTime,
|
||||||
isRolledBack,
|
isRolledBack,
|
||||||
packageVersion,
|
packageVersion,
|
||||||
@@ -28,6 +28,7 @@ import {
|
|||||||
promiseAny,
|
promiseAny,
|
||||||
testUrls,
|
testUrls,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
import i18n from './i18n';
|
||||||
|
|
||||||
const SERVER_PRESETS = {
|
const SERVER_PRESETS = {
|
||||||
// cn
|
// cn
|
||||||
@@ -91,7 +92,7 @@ export class Pushy {
|
|||||||
options = defaultClientOptions;
|
options = defaultClientOptions;
|
||||||
clientType: 'Pushy' | 'Cresc' = 'Pushy';
|
clientType: 'Pushy' | 'Cresc' = 'Pushy';
|
||||||
lastChecking?: number;
|
lastChecking?: number;
|
||||||
lastRespJson?: Promise<any>;
|
lastRespJson?: Promise<CheckResult>;
|
||||||
|
|
||||||
version = cInfo.rnu;
|
version = cInfo.rnu;
|
||||||
loggerPromise = (() => {
|
loggerPromise = (() => {
|
||||||
@@ -106,13 +107,17 @@ export class Pushy {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
constructor(options: ClientOptions, clientType?: 'Pushy' | 'Cresc') {
|
constructor(options: ClientOptions, clientType?: 'Pushy' | 'Cresc') {
|
||||||
if (Platform.OS === 'ios' || Platform.OS === 'android') {
|
|
||||||
if (!options.appKey) {
|
|
||||||
throw new Error('appKey is required');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.clientType = clientType || 'Pushy';
|
this.clientType = clientType || 'Pushy';
|
||||||
this.options.server = SERVER_PRESETS[this.clientType];
|
this.options.server = SERVER_PRESETS[this.clientType];
|
||||||
|
|
||||||
|
i18n.setLocale(options.locale ?? this.clientType === 'Pushy' ? 'zh' : 'en');
|
||||||
|
|
||||||
|
if (Platform.OS === 'ios' || Platform.OS === 'android') {
|
||||||
|
if (!options.appKey) {
|
||||||
|
throw Error(i18n.t('error_appkey_required'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.setOptions(options);
|
this.setOptions(options);
|
||||||
if (isRolledBack) {
|
if (isRolledBack) {
|
||||||
this.report({
|
this.report({
|
||||||
@@ -135,6 +140,16 @@ export class Pushy {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get translated text based on current clientType
|
||||||
|
* @param key - Translation key
|
||||||
|
* @param values - Values for interpolation (optional)
|
||||||
|
* @returns Translated string
|
||||||
|
*/
|
||||||
|
t = (key: string, values?: Record<string, string | number>) => {
|
||||||
|
return i18n.t(key as any, values);
|
||||||
|
};
|
||||||
|
|
||||||
report = async ({
|
report = async ({
|
||||||
type,
|
type,
|
||||||
message = '',
|
message = '',
|
||||||
@@ -147,7 +162,6 @@ export class Pushy {
|
|||||||
log(type + ' ' + message);
|
log(type + ' ' + message);
|
||||||
await this.loggerPromise.promise;
|
await this.loggerPromise.promise;
|
||||||
const { logger = noop, appKey } = this.options;
|
const { logger = noop, appKey } = this.options;
|
||||||
const info = await getCurrentVersionInfo();
|
|
||||||
const overridePackageVersion = this.options.overridePackageVersion;
|
const overridePackageVersion = this.options.overridePackageVersion;
|
||||||
logger({
|
logger({
|
||||||
type,
|
type,
|
||||||
@@ -159,7 +173,7 @@ export class Pushy {
|
|||||||
overridePackageVersion,
|
overridePackageVersion,
|
||||||
buildTime,
|
buildTime,
|
||||||
message,
|
message,
|
||||||
...info,
|
...currentVersionInfo,
|
||||||
...data,
|
...data,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -174,11 +188,7 @@ export class Pushy {
|
|||||||
};
|
};
|
||||||
assertDebug = (matter: string) => {
|
assertDebug = (matter: string) => {
|
||||||
if (__DEV__ && !this.options.debug) {
|
if (__DEV__ && !this.options.debug) {
|
||||||
console.info(
|
console.info(this.t('dev_debug_disabled', { matter }));
|
||||||
`You are currently in the development environment and have not enabled debug mode.
|
|
||||||
${matter} will not be performed.
|
|
||||||
If you need to debug ${matter} in the development environment, please set debug to true in the client.`,
|
|
||||||
);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -269,7 +279,7 @@ export class Pushy {
|
|||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this.report({
|
this.report({
|
||||||
type: 'errorChecking',
|
type: 'errorChecking',
|
||||||
message: `Can not connect to update server: ${e.message}. Trying backup endpoints.`,
|
message: this.t('error_cannot_connect_backup', { message: e.message }),
|
||||||
});
|
});
|
||||||
const backupEndpoints = await this.getBackupEndpoints();
|
const backupEndpoints = await this.getBackupEndpoints();
|
||||||
if (backupEndpoints) {
|
if (backupEndpoints) {
|
||||||
@@ -280,18 +290,32 @@ export class Pushy {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
this.throwIfEnabled(new Error('errorCheckingUseBackup'));
|
this.throwIfEnabled(Error('errorCheckingUseBackup'));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.throwIfEnabled(new Error('errorCheckingGetBackup'));
|
this.throwIfEnabled(Error('errorCheckingGetBackup'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!resp) {
|
if (!resp) {
|
||||||
this.report({
|
this.report({
|
||||||
type: 'errorChecking',
|
type: 'errorChecking',
|
||||||
message: 'Can not connect to update server. Please check your network.',
|
message: this.t('error_cannot_connect_server'),
|
||||||
});
|
});
|
||||||
this.throwIfEnabled(new Error('errorChecking'));
|
this.throwIfEnabled(Error('errorChecking'));
|
||||||
|
return this.lastRespJson ? await this.lastRespJson : emptyObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!resp.ok) {
|
||||||
|
const respText = await resp.text();
|
||||||
|
const errorMessage = this.t('error_http_status', {
|
||||||
|
status: resp.status,
|
||||||
|
statusText: respText,
|
||||||
|
});
|
||||||
|
this.report({
|
||||||
|
type: 'errorChecking',
|
||||||
|
message: errorMessage,
|
||||||
|
});
|
||||||
|
this.throwIfEnabled(Error(errorMessage));
|
||||||
return this.lastRespJson ? await this.lastRespJson : emptyObj;
|
return this.lastRespJson ? await this.lastRespJson : emptyObj;
|
||||||
}
|
}
|
||||||
this.lastRespJson = resp.json();
|
this.lastRespJson = resp.json();
|
||||||
@@ -300,14 +324,6 @@ export class Pushy {
|
|||||||
|
|
||||||
log('checking result:', result);
|
log('checking result:', result);
|
||||||
|
|
||||||
if (resp.status !== 200) {
|
|
||||||
this.report({
|
|
||||||
type: 'errorChecking',
|
|
||||||
message: result.message,
|
|
||||||
});
|
|
||||||
this.throwIfEnabled(new Error(result.message));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
getBackupEndpoints = async () => {
|
getBackupEndpoints = async () => {
|
||||||
@@ -412,9 +428,11 @@ export class Pushy {
|
|||||||
});
|
});
|
||||||
succeeded = 'diff';
|
succeeded = 'diff';
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
const errorMessage = `diff error: ${e.message}`;
|
const errorMessage = this.t('error_diff_failed', {
|
||||||
|
message: e.message,
|
||||||
|
});
|
||||||
errorMessages.push(errorMessage);
|
errorMessages.push(errorMessage);
|
||||||
lastError = new Error(errorMessage);
|
lastError = Error(errorMessage);
|
||||||
log(errorMessage);
|
log(errorMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -429,9 +447,11 @@ export class Pushy {
|
|||||||
});
|
});
|
||||||
succeeded = 'pdiff';
|
succeeded = 'pdiff';
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
const errorMessage = `pdiff error: ${e.message}`;
|
const errorMessage = this.t('error_pdiff_failed', {
|
||||||
|
message: e.message,
|
||||||
|
});
|
||||||
errorMessages.push(errorMessage);
|
errorMessages.push(errorMessage);
|
||||||
lastError = new Error(errorMessage);
|
lastError = Error(errorMessage);
|
||||||
log(errorMessage);
|
log(errorMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -447,17 +467,15 @@ export class Pushy {
|
|||||||
});
|
});
|
||||||
succeeded = 'full';
|
succeeded = 'full';
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
const errorMessage = `full patch error: ${e.message}`;
|
const errorMessage = this.t('error_full_patch_failed', {
|
||||||
|
message: e.message,
|
||||||
|
});
|
||||||
errorMessages.push(errorMessage);
|
errorMessages.push(errorMessage);
|
||||||
lastError = new Error(errorMessage);
|
lastError = Error(errorMessage);
|
||||||
log(errorMessage);
|
log(errorMessage);
|
||||||
}
|
}
|
||||||
} else if (__DEV__) {
|
} else if (__DEV__) {
|
||||||
log(
|
log(this.t('dev_incremental_update_disabled'));
|
||||||
`当前是开发环境,无法执行增量式热更新,重启不会生效。
|
|
||||||
如果需要在开发环境中测试可生效的全量热更新(但也会在再次重启后重新连接 metro),
|
|
||||||
请打开“忽略时间戳”开关再重试。`,
|
|
||||||
);
|
|
||||||
succeeded = 'full';
|
succeeded = 'full';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -511,7 +529,7 @@ export class Pushy {
|
|||||||
}
|
}
|
||||||
if (sharedState.apkStatus === 'downloaded') {
|
if (sharedState.apkStatus === 'downloaded') {
|
||||||
this.report({ type: 'errorInstallApk' });
|
this.report({ type: 'errorInstallApk' });
|
||||||
this.throwIfEnabled(new Error('errorInstallApk'));
|
this.throwIfEnabled(Error('errorInstallApk'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Platform.Version <= 23) {
|
if (Platform.Version <= 23) {
|
||||||
@@ -521,7 +539,7 @@ export class Pushy {
|
|||||||
);
|
);
|
||||||
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
|
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
|
||||||
this.report({ type: 'rejectStoragePermission' });
|
this.report({ type: 'rejectStoragePermission' });
|
||||||
this.throwIfEnabled(new Error('rejectStoragePermission'));
|
this.throwIfEnabled(Error('rejectStoragePermission'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@@ -554,7 +572,7 @@ export class Pushy {
|
|||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
sharedState.apkStatus = null;
|
sharedState.apkStatus = null;
|
||||||
this.report({ type: 'errorDownloadAndInstallApk' });
|
this.report({ type: 'errorDownloadAndInstallApk' });
|
||||||
this.throwIfEnabled(new Error('errorDownloadAndInstallApk'));
|
this.throwIfEnabled(Error('errorDownloadAndInstallApk'));
|
||||||
});
|
});
|
||||||
sharedState.apkStatus = 'downloaded';
|
sharedState.apkStatus = 'downloaded';
|
||||||
if (sharedState.progressHandlers[progressKey]) {
|
if (sharedState.progressHandlers[progressKey]) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { createContext, useContext } from 'react';
|
import { createContext, useContext } from 'react';
|
||||||
import { CheckResult, ProgressData } from './type';
|
import { CheckResult, ProgressData } from './type';
|
||||||
import { Pushy, Cresc } from './client';
|
import { Pushy, Cresc } from './client';
|
||||||
|
import i18n from './i18n';
|
||||||
|
|
||||||
const noop = () => {};
|
const noop = () => {};
|
||||||
const asyncNoop = () => Promise.resolve();
|
const asyncNoop = () => Promise.resolve();
|
||||||
@@ -18,6 +19,7 @@ export const defaultContext = {
|
|||||||
parseTestQrCode: () => false,
|
parseTestQrCode: () => false,
|
||||||
currentHash: '',
|
currentHash: '',
|
||||||
packageVersion: '',
|
packageVersion: '',
|
||||||
|
currentVersionInfo: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const UpdateContext = createContext<{
|
export const UpdateContext = createContext<{
|
||||||
@@ -28,11 +30,17 @@ export const UpdateContext = createContext<{
|
|||||||
dismissError: () => void;
|
dismissError: () => void;
|
||||||
downloadUpdate: () => Promise<boolean | void>;
|
downloadUpdate: () => Promise<boolean | void>;
|
||||||
downloadAndInstallApk: (url: string) => Promise<void>;
|
downloadAndInstallApk: (url: string) => Promise<void>;
|
||||||
|
// @deprecated use currentVersionInfo instead
|
||||||
getCurrentVersionInfo: () => Promise<{
|
getCurrentVersionInfo: () => Promise<{
|
||||||
name?: string;
|
name?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
metaInfo?: string;
|
metaInfo?: string;
|
||||||
}>;
|
}>;
|
||||||
|
currentVersionInfo: {
|
||||||
|
name?: string;
|
||||||
|
description?: string;
|
||||||
|
metaInfo?: string;
|
||||||
|
} | null;
|
||||||
parseTestQrCode: (code: string) => boolean;
|
parseTestQrCode: (code: string) => boolean;
|
||||||
restartApp: () => Promise<void>;
|
restartApp: () => Promise<void>;
|
||||||
currentHash: string;
|
currentHash: string;
|
||||||
@@ -43,7 +51,16 @@ export const UpdateContext = createContext<{
|
|||||||
lastError?: Error;
|
lastError?: Error;
|
||||||
}>(defaultContext);
|
}>(defaultContext);
|
||||||
|
|
||||||
export const useUpdate = () => useContext(UpdateContext);
|
export const useUpdate = __DEV__ ? () => {
|
||||||
|
const context = useContext(UpdateContext);
|
||||||
|
|
||||||
|
// 检查是否在 UpdateProvider 内部使用
|
||||||
|
if (!context.client) {
|
||||||
|
throw new Error(i18n.t('error_use_update_outside_provider'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return context;
|
||||||
|
} : () => useContext(UpdateContext);
|
||||||
|
|
||||||
/** @deprecated Please use `useUpdate` instead */
|
/** @deprecated Please use `useUpdate` instead */
|
||||||
export const usePushy = useUpdate;
|
export const usePushy = useUpdate;
|
||||||
|
|||||||
20
src/core.ts
20
src/core.ts
@@ -18,7 +18,7 @@ export const PushyModule =
|
|||||||
export const UpdateModule = PushyModule;
|
export const UpdateModule = PushyModule;
|
||||||
|
|
||||||
if (!PushyModule) {
|
if (!PushyModule) {
|
||||||
throw new Error(
|
throw Error(
|
||||||
'Failed to load react-native-update native module, please try to recompile',
|
'Failed to load react-native-update native module, please try to recompile',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -30,9 +30,24 @@ const PushyConstants = isTurboModuleEnabled
|
|||||||
export const downloadRootDir: string = PushyConstants.downloadRootDir;
|
export const downloadRootDir: string = PushyConstants.downloadRootDir;
|
||||||
export const packageVersion: string = PushyConstants.packageVersion;
|
export const packageVersion: string = PushyConstants.packageVersion;
|
||||||
export const currentVersion: string = PushyConstants.currentVersion;
|
export const currentVersion: string = PushyConstants.currentVersion;
|
||||||
|
|
||||||
|
const currentVersionInfoString: string = PushyConstants.currentVersionInfo;
|
||||||
|
let _currentVersionInfo = {};
|
||||||
|
if (currentVersionInfoString) {
|
||||||
|
try {
|
||||||
|
_currentVersionInfo = JSON.parse(currentVersionInfoString);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
'Failed to parse currentVersionInfo:',
|
||||||
|
currentVersionInfoString,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const currentVersionInfo = _currentVersionInfo;
|
||||||
|
|
||||||
export const isFirstTime: boolean = PushyConstants.isFirstTime;
|
export const isFirstTime: boolean = PushyConstants.isFirstTime;
|
||||||
export const rolledBackVersion: string = PushyConstants.rolledBackVersion;
|
export const rolledBackVersion: string = PushyConstants.rolledBackVersion;
|
||||||
export const isRolledBack: boolean = typeof rolledBackVersion === 'string';
|
export const isRolledBack: boolean = !!rolledBackVersion;
|
||||||
|
|
||||||
export const buildTime: string = PushyConstants.buildTime;
|
export const buildTime: string = PushyConstants.buildTime;
|
||||||
let uuid = PushyConstants.uuid;
|
let uuid = PushyConstants.uuid;
|
||||||
@@ -45,6 +60,7 @@ async function getLocalHashInfo(hash: string) {
|
|||||||
return JSON.parse(await PushyModule.getLocalHashInfo(hash));
|
return JSON.parse(await PushyModule.getLocalHashInfo(hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @deprecated use currentVersionInfo instead
|
||||||
export async function getCurrentVersionInfo(): Promise<{
|
export async function getCurrentVersionInfo(): Promise<{
|
||||||
name?: string;
|
name?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
|
|||||||
108
src/i18n.ts
Normal file
108
src/i18n.ts
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
import zhTranslations from './locales/zh';
|
||||||
|
import enTranslations from './locales/en';
|
||||||
|
|
||||||
|
type TranslationKey = keyof typeof zhTranslations | keyof typeof enTranslations;
|
||||||
|
type TranslationValues = Record<string, string | number>;
|
||||||
|
|
||||||
|
class I18n {
|
||||||
|
private currentLocale: 'zh' | 'en' = 'en';
|
||||||
|
private translations = {
|
||||||
|
zh: zhTranslations,
|
||||||
|
en: enTranslations,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set locale directly
|
||||||
|
* @param locale - 'zh' or 'en'
|
||||||
|
*/
|
||||||
|
setLocale(locale: 'zh' | 'en') {
|
||||||
|
this.currentLocale = locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current locale
|
||||||
|
*/
|
||||||
|
getLocale(): 'zh' | 'en' {
|
||||||
|
return this.currentLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate a key with optional interpolation
|
||||||
|
* @param key - Translation key
|
||||||
|
* @param values - Values for interpolation (optional)
|
||||||
|
* @returns Translated string with interpolated values
|
||||||
|
*/
|
||||||
|
t(key: TranslationKey, values?: TranslationValues): string {
|
||||||
|
const translation =
|
||||||
|
this.translations[this.currentLocale][
|
||||||
|
key as keyof (typeof this.translations)[typeof this.currentLocale]
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!translation) {
|
||||||
|
// Fallback to the other locale if key not found
|
||||||
|
const fallbackLocale = this.currentLocale === 'zh' ? 'en' : 'zh';
|
||||||
|
const fallbackTranslation =
|
||||||
|
this.translations[fallbackLocale][
|
||||||
|
key as keyof (typeof this.translations)[typeof fallbackLocale]
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!fallbackTranslation) {
|
||||||
|
// If still not found, return the key itself
|
||||||
|
return String(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.interpolate(fallbackTranslation, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.interpolate(translation, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interpolate values into a string template
|
||||||
|
* Supports {{key}} syntax
|
||||||
|
* @param template - String template with {{key}} placeholders
|
||||||
|
* @param values - Values to interpolate
|
||||||
|
* @returns Interpolated string
|
||||||
|
*/
|
||||||
|
private interpolate(template: string, values?: TranslationValues): string {
|
||||||
|
if (!values) {
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
||||||
|
const value = values[key];
|
||||||
|
return value !== undefined ? String(value) : match;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add or update translations for a specific locale
|
||||||
|
* @param locale - Target locale
|
||||||
|
* @param translations - Translation object to merge
|
||||||
|
*/
|
||||||
|
addTranslations(locale: 'zh' | 'en', translations: Record<string, string>) {
|
||||||
|
this.translations[locale] = {
|
||||||
|
...this.translations[locale],
|
||||||
|
...translations,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create singleton instance
|
||||||
|
const i18n = new I18n();
|
||||||
|
|
||||||
|
// Export both the instance and the class for flexibility
|
||||||
|
export { i18n, I18n };
|
||||||
|
export default i18n;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Usage examples:
|
||||||
|
*
|
||||||
|
* // Direct locale setting (new preferred method)
|
||||||
|
* i18n.setLocale('zh'); // Chinese
|
||||||
|
* i18n.setLocale('en'); // English
|
||||||
|
*
|
||||||
|
* // Get translations
|
||||||
|
* i18n.t('checking_update'); // Based on current locale
|
||||||
|
* i18n.t('download_progress', { progress: 50 }); // With interpolation
|
||||||
|
*/
|
||||||
78
src/locales/en.ts
Normal file
78
src/locales/en.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
export default {
|
||||||
|
// Common messages
|
||||||
|
checking_update: 'Checking for updates...',
|
||||||
|
downloading_update: 'Downloading update package...',
|
||||||
|
installing_update: 'Installing update...',
|
||||||
|
update_available: 'Update available',
|
||||||
|
update_downloaded: 'Update downloaded successfully',
|
||||||
|
update_installed: 'Update installed successfully',
|
||||||
|
no_update_available: 'You are up to date',
|
||||||
|
update_failed: 'Update failed',
|
||||||
|
network_error: 'Network connection error',
|
||||||
|
download_failed: 'Download failed',
|
||||||
|
install_failed: 'Installation failed',
|
||||||
|
|
||||||
|
// Progress messages with interpolation
|
||||||
|
download_progress: 'Download progress: {{progress}}%',
|
||||||
|
download_speed: 'Download speed: {{speed}}/s',
|
||||||
|
file_size: 'File size: {{size}}',
|
||||||
|
time_remaining: 'Time remaining: {{time}}',
|
||||||
|
|
||||||
|
// Error messages
|
||||||
|
error_code: 'Error code: {{code}}',
|
||||||
|
error_message: 'Error message: {{message}}',
|
||||||
|
retry_count: 'Retry attempt: {{count}}/{{max}}',
|
||||||
|
|
||||||
|
// Update info
|
||||||
|
version_info: 'Version {{version}} ({{build}})',
|
||||||
|
release_notes: 'Release notes: {{notes}}',
|
||||||
|
update_size: 'Update size: {{size}}MB',
|
||||||
|
|
||||||
|
// Alert messages
|
||||||
|
alert_title: 'Notice',
|
||||||
|
alert_update_ready: 'Download completed. Update now?',
|
||||||
|
alert_next_time: 'Later',
|
||||||
|
alert_update_now: 'Update Now',
|
||||||
|
alert_app_updated:
|
||||||
|
'Your app version has been updated. Click update to download and install the new version',
|
||||||
|
alert_update_button: 'Update',
|
||||||
|
alert_cancel: 'Cancel',
|
||||||
|
alert_confirm: 'OK',
|
||||||
|
alert_info: 'Info',
|
||||||
|
alert_no_update_wait:
|
||||||
|
'No update found, please wait 10s for the server to generate the patch package',
|
||||||
|
|
||||||
|
// Error messages
|
||||||
|
error_appkey_required: 'appKey is required',
|
||||||
|
error_update_check_failed: 'Update check failed',
|
||||||
|
error_cannot_connect_server:
|
||||||
|
'Can not connect to update server. Please check your network.',
|
||||||
|
error_cannot_connect_backup:
|
||||||
|
'Can not connect to update server: {{message}}. Trying backup endpoints.',
|
||||||
|
error_diff_failed: 'diff error: {{message}}',
|
||||||
|
error_pdiff_failed: 'pdiff error: {{message}}',
|
||||||
|
error_full_patch_failed: 'full patch error: {{message}}',
|
||||||
|
error_all_promises_rejected: 'All promises were rejected',
|
||||||
|
error_ping_failed: 'Ping failed',
|
||||||
|
error_ping_timeout: 'Ping timeout',
|
||||||
|
error_http_status: '{{status}} {{statusText}}',
|
||||||
|
|
||||||
|
// Development messages
|
||||||
|
dev_debug_disabled:
|
||||||
|
'You are currently in the development environment and have not enabled debug mode. {{matter}} will not be performed. If you need to debug {{matter}} in the development environment, please set debug to true in the client.',
|
||||||
|
dev_log_prefix: 'react-native-update: ',
|
||||||
|
dev_web_not_supported:
|
||||||
|
'react-native-update does not support the Web platform and will not perform any operations',
|
||||||
|
|
||||||
|
// More alert messages
|
||||||
|
alert_new_version_found:
|
||||||
|
'New version {{name}} found. Download now?\n{{description}}',
|
||||||
|
|
||||||
|
// Development environment messages
|
||||||
|
dev_incremental_update_disabled:
|
||||||
|
'Currently in development environment, incremental hot update cannot be executed and restart will not take effect. If you need to test effective full hot update in development environment (but will reconnect to metro after restart), please enable "ignore timestamp" switch and retry.',
|
||||||
|
|
||||||
|
// Context error messages
|
||||||
|
error_use_update_outside_provider:
|
||||||
|
'useUpdate must be used within an UpdateProvider. Please wrap your component tree with <UpdateProvider client={...}>.',
|
||||||
|
};
|
||||||
75
src/locales/zh.ts
Normal file
75
src/locales/zh.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
export default {
|
||||||
|
// Common messages
|
||||||
|
checking_update: '正在检查更新...',
|
||||||
|
downloading_update: '正在下载更新包...',
|
||||||
|
installing_update: '正在安装更新...',
|
||||||
|
update_available: '发现新版本',
|
||||||
|
update_downloaded: '更新包下载完成',
|
||||||
|
update_installed: '更新安装完成',
|
||||||
|
no_update_available: '已是最新版本',
|
||||||
|
update_failed: '更新失败',
|
||||||
|
network_error: '网络连接错误',
|
||||||
|
download_failed: '下载失败',
|
||||||
|
install_failed: '安装失败',
|
||||||
|
|
||||||
|
// Progress messages with interpolation
|
||||||
|
download_progress: '下载进度: {{progress}}%',
|
||||||
|
download_speed: '下载速度: {{speed}}/s',
|
||||||
|
file_size: '文件大小: {{size}}',
|
||||||
|
time_remaining: '剩余时间: {{time}}',
|
||||||
|
|
||||||
|
// Error messages
|
||||||
|
error_code: '错误代码: {{code}}',
|
||||||
|
error_message: '错误信息: {{message}}',
|
||||||
|
retry_count: '重试次数: {{count}}/{{max}}',
|
||||||
|
|
||||||
|
// Update info
|
||||||
|
version_info: '版本 {{version}} ({{build}})',
|
||||||
|
release_notes: '更新说明: {{notes}}',
|
||||||
|
update_size: '更新包大小: {{size}}MB',
|
||||||
|
|
||||||
|
// Alert messages
|
||||||
|
alert_title: '提示',
|
||||||
|
alert_update_ready: '下载完毕,是否立即更新?',
|
||||||
|
alert_next_time: '下次再说',
|
||||||
|
alert_update_now: '立即更新',
|
||||||
|
alert_app_updated: '您的应用版本已更新,点击更新下载安装新版本',
|
||||||
|
alert_update_button: '更新',
|
||||||
|
alert_cancel: '取消',
|
||||||
|
alert_confirm: '确定',
|
||||||
|
alert_info: '信息',
|
||||||
|
alert_no_update_wait: '未发现更新,请等待10秒让服务器生成补丁包',
|
||||||
|
|
||||||
|
// Error messages
|
||||||
|
error_appkey_required: '需要提供 appKey',
|
||||||
|
error_update_check_failed: '更新检查失败',
|
||||||
|
error_cannot_connect_server: '无法连接到更新服务器。请检查网络连接。',
|
||||||
|
error_cannot_connect_backup:
|
||||||
|
'无法连接到更新服务器: {{message}}。正在尝试备用端点。',
|
||||||
|
error_diff_failed: 'diff 错误: {{message}}',
|
||||||
|
error_pdiff_failed: 'pdiff 错误: {{message}}',
|
||||||
|
error_full_patch_failed: '完整补丁错误: {{message}}',
|
||||||
|
error_all_promises_rejected: '所有请求都被拒绝',
|
||||||
|
error_ping_failed: 'Ping 失败',
|
||||||
|
error_ping_timeout: 'Ping 超时',
|
||||||
|
error_http_status: '{{status}} {{statusText}}',
|
||||||
|
|
||||||
|
// Development messages
|
||||||
|
dev_debug_disabled:
|
||||||
|
'您当前处于开发环境且未启用调试模式。{{matter}} 将不会执行。如需在开发环境中调试 {{matter}},请在客户端中将 debug 设为 true。',
|
||||||
|
dev_log_prefix: 'react-native-update: ',
|
||||||
|
dev_web_not_supported:
|
||||||
|
'react-native-update 不支持 Web 平台,不会执行任何操作',
|
||||||
|
|
||||||
|
// More alert messages
|
||||||
|
alert_new_version_found:
|
||||||
|
'检查到新的版本{{name}},是否下载?\n{{description}}',
|
||||||
|
|
||||||
|
// Development environment messages
|
||||||
|
dev_incremental_update_disabled:
|
||||||
|
'当前是开发环境,无法执行增量式热更新,重启不会生效。如果需要在开发环境中测试可生效的全量热更新(但也会在再次重启后重新连接 metro),请打开"忽略时间戳"开关再重试。',
|
||||||
|
|
||||||
|
// Context error messages
|
||||||
|
error_use_update_outside_provider:
|
||||||
|
'useUpdate 必须在 UpdateProvider 内部使用。请使用 <UpdateProvider client={...}> 包裹您的组件树。',
|
||||||
|
};
|
||||||
185
src/provider.tsx
185
src/provider.tsx
@@ -13,12 +13,22 @@ import {
|
|||||||
Linking,
|
Linking,
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
import { Pushy, Cresc, sharedState } from './client';
|
import { Pushy, Cresc, sharedState } from './client';
|
||||||
import { currentVersion, packageVersion, getCurrentVersionInfo } from './core';
|
import {
|
||||||
import { CheckResult, ProgressData, UpdateTestPayload } from './type';
|
currentVersion,
|
||||||
|
packageVersion,
|
||||||
|
getCurrentVersionInfo,
|
||||||
|
currentVersionInfo,
|
||||||
|
} from './core';
|
||||||
|
import {
|
||||||
|
CheckResult,
|
||||||
|
ProgressData,
|
||||||
|
UpdateTestPayload,
|
||||||
|
VersionInfo,
|
||||||
|
} from './type';
|
||||||
import { UpdateContext } from './context';
|
import { UpdateContext } from './context';
|
||||||
import { URL } from 'react-native-url-polyfill';
|
import { URL } from 'react-native-url-polyfill';
|
||||||
import { isInRollout } from './isInRollout';
|
import { isInRollout } from './isInRollout';
|
||||||
import { log } from './utils';
|
import { assertWeb, log } from './utils';
|
||||||
|
|
||||||
export const UpdateProvider = ({
|
export const UpdateProvider = ({
|
||||||
client,
|
client,
|
||||||
@@ -30,7 +40,7 @@ export const UpdateProvider = ({
|
|||||||
client = useRef(client).current;
|
client = useRef(client).current;
|
||||||
const { options } = client;
|
const { options } = client;
|
||||||
|
|
||||||
const stateListener = useRef<NativeEventSubscription>();
|
const stateListener = useRef<NativeEventSubscription>(undefined);
|
||||||
const [updateInfo, setUpdateInfo] = useState<CheckResult>();
|
const [updateInfo, setUpdateInfo] = useState<CheckResult>();
|
||||||
const updateInfoRef = useRef(updateInfo);
|
const updateInfoRef = useRef(updateInfo);
|
||||||
const [progress, setProgress] = useState<ProgressData>();
|
const [progress, setProgress] = useState<ProgressData>();
|
||||||
@@ -115,16 +125,16 @@ export const UpdateProvider = ({
|
|||||||
client.switchVersionLater(hash);
|
client.switchVersionLater(hash);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
alertUpdate('提示', '下载完毕,是否立即更新?', [
|
alertUpdate(client.t('alert_title'), client.t('alert_update_ready'), [
|
||||||
{
|
{
|
||||||
text: '下次再说',
|
text: client.t('alert_next_time'),
|
||||||
style: 'cancel',
|
style: 'cancel',
|
||||||
onPress: () => {
|
onPress: () => {
|
||||||
client.switchVersionLater(hash);
|
client.switchVersionLater(hash);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: '立即更新',
|
text: client.t('alert_update_now'),
|
||||||
style: 'default',
|
style: 'default',
|
||||||
onPress: () => {
|
onPress: () => {
|
||||||
client.switchVersion(hash);
|
client.switchVersion(hash);
|
||||||
@@ -134,7 +144,7 @@ export const UpdateProvider = ({
|
|||||||
return true;
|
return true;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setLastError(e);
|
setLastError(e);
|
||||||
alertError('更新失败', e.message);
|
alertError(client.t('update_failed'), e.message);
|
||||||
throwErrorIfEnabled(e);
|
throwErrorIfEnabled(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -158,84 +168,104 @@ export const UpdateProvider = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
lastChecking.current = now;
|
lastChecking.current = now;
|
||||||
let info: CheckResult;
|
let rootInfo: CheckResult | undefined;
|
||||||
try {
|
try {
|
||||||
info = await client.checkUpdate(extra);
|
rootInfo = { ...(await client.checkUpdate(extra)) };
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setLastError(e);
|
setLastError(e);
|
||||||
alertError('更新检查失败', e.message);
|
alertError(client.t('error_update_check_failed'), e.message);
|
||||||
throwErrorIfEnabled(e);
|
throwErrorIfEnabled(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!info) {
|
if (!rootInfo) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const rollout = info.config?.rollout?.[packageVersion];
|
const versions = [rootInfo.expVersion, rootInfo].filter(
|
||||||
if (info.update && rollout) {
|
Boolean,
|
||||||
if (!isInRollout(rollout)) {
|
) as VersionInfo[];
|
||||||
log(`not in ${rollout}% rollout, ignored`);
|
delete rootInfo.expVersion;
|
||||||
return;
|
for (const versionInfo of versions) {
|
||||||
|
const info: CheckResult = {
|
||||||
|
...rootInfo,
|
||||||
|
...versionInfo,
|
||||||
|
};
|
||||||
|
const rollout = info.config?.rollout?.[packageVersion];
|
||||||
|
if (info.update && rollout) {
|
||||||
|
if (!isInRollout(rollout)) {
|
||||||
|
log(`${info.name} not in ${rollout}% rollout, ignored`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
log(`${info.name} in ${rollout}% rollout, continue`);
|
||||||
}
|
}
|
||||||
log(`in ${rollout}% rollout, continue`);
|
info.description = info.description ?? '';
|
||||||
}
|
updateInfoRef.current = info;
|
||||||
info.description = info.description ?? '';
|
setUpdateInfo(info);
|
||||||
updateInfoRef.current = info;
|
if (info.expired) {
|
||||||
setUpdateInfo(info);
|
if (
|
||||||
if (info.expired) {
|
options.onPackageExpired &&
|
||||||
if (
|
(await options.onPackageExpired(info)) === false
|
||||||
options.onPackageExpired &&
|
) {
|
||||||
(await options.onPackageExpired(info)) === false
|
log('onPackageExpired returned false, skipping');
|
||||||
) {
|
return;
|
||||||
log('onPackageExpired returned false, skipping');
|
}
|
||||||
return;
|
const { downloadUrl } = info;
|
||||||
}
|
if (downloadUrl && sharedState.apkStatus === null) {
|
||||||
const { downloadUrl } = info;
|
if (options.updateStrategy === 'silentAndNow') {
|
||||||
if (downloadUrl && sharedState.apkStatus === null) {
|
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
|
||||||
if (options.updateStrategy === 'silentAndNow') {
|
downloadAndInstallApk(downloadUrl);
|
||||||
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
|
} else {
|
||||||
downloadAndInstallApk(downloadUrl);
|
Linking.openURL(downloadUrl);
|
||||||
} else {
|
}
|
||||||
Linking.openURL(downloadUrl);
|
return info;
|
||||||
}
|
}
|
||||||
|
alertUpdate(
|
||||||
|
client.t('alert_title'),
|
||||||
|
client.t('alert_app_updated'),
|
||||||
|
[
|
||||||
|
{
|
||||||
|
text: client.t('alert_update_button'),
|
||||||
|
onPress: () => {
|
||||||
|
if (
|
||||||
|
Platform.OS === 'android' &&
|
||||||
|
downloadUrl.endsWith('.apk')
|
||||||
|
) {
|
||||||
|
downloadAndInstallApk(downloadUrl);
|
||||||
|
} else {
|
||||||
|
Linking.openURL(downloadUrl);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (info.update) {
|
||||||
|
if (
|
||||||
|
options.updateStrategy === 'silentAndNow' ||
|
||||||
|
options.updateStrategy === 'silentAndLater'
|
||||||
|
) {
|
||||||
|
downloadUpdate(info);
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
alertUpdate('提示', '您的应用版本已更新,点击更新下载安装新版本', [
|
alertUpdate(
|
||||||
{
|
client.t('alert_title'),
|
||||||
text: '更新',
|
client.t('alert_new_version_found', {
|
||||||
onPress: () => {
|
name: info.name!,
|
||||||
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
|
description: info.description,
|
||||||
downloadAndInstallApk(downloadUrl);
|
}),
|
||||||
} else {
|
[
|
||||||
Linking.openURL(downloadUrl);
|
{ text: client.t('alert_cancel'), style: 'cancel' },
|
||||||
}
|
{
|
||||||
|
text: client.t('alert_confirm'),
|
||||||
|
style: 'default',
|
||||||
|
onPress: () => {
|
||||||
|
downloadUpdate();
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
]);
|
);
|
||||||
}
|
}
|
||||||
} else if (info.update) {
|
return info;
|
||||||
if (
|
|
||||||
options.updateStrategy === 'silentAndNow' ||
|
|
||||||
options.updateStrategy === 'silentAndLater'
|
|
||||||
) {
|
|
||||||
downloadUpdate(info);
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
alertUpdate(
|
|
||||||
'提示',
|
|
||||||
'检查到新的版本' + info.name + ',是否下载?\n' + info.description,
|
|
||||||
[
|
|
||||||
{ text: '取消', style: 'cancel' },
|
|
||||||
{
|
|
||||||
text: '确定',
|
|
||||||
style: 'default',
|
|
||||||
onPress: () => {
|
|
||||||
downloadUpdate();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return info;
|
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
client,
|
client,
|
||||||
@@ -254,6 +284,9 @@ export const UpdateProvider = ({
|
|||||||
if (!client.assertDebug('checkUpdate()')) {
|
if (!client.assertDebug('checkUpdate()')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!assertWeb()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options;
|
const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options;
|
||||||
if (autoMarkSuccess) {
|
if (autoMarkSuccess) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -297,8 +330,8 @@ export const UpdateProvider = ({
|
|||||||
checkUpdate({ extra: { toHash: payload.data } }).then(() => {
|
checkUpdate({ extra: { toHash: payload.data } }).then(() => {
|
||||||
if (updateInfoRef.current && updateInfoRef.current.upToDate) {
|
if (updateInfoRef.current && updateInfoRef.current.upToDate) {
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
'Info',
|
client.t('alert_info'),
|
||||||
'No update found, please wait 10s for the server to generate the patch package',
|
client.t('alert_no_update_wait'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
options.logger = logger;
|
options.logger = logger;
|
||||||
@@ -308,7 +341,7 @@ export const UpdateProvider = ({
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
[checkUpdate, options],
|
[checkUpdate, options, client],
|
||||||
);
|
);
|
||||||
|
|
||||||
const parseTestQrCode = useCallback(
|
const parseTestQrCode = useCallback(
|
||||||
@@ -328,6 +361,9 @@ export const UpdateProvider = ({
|
|||||||
}, [client]);
|
}, [client]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!assertWeb()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const parseLinking = (url: string | null) => {
|
const parseLinking = (url: string | null) => {
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return;
|
return;
|
||||||
@@ -371,6 +407,7 @@ export const UpdateProvider = ({
|
|||||||
progress,
|
progress,
|
||||||
downloadAndInstallApk,
|
downloadAndInstallApk,
|
||||||
getCurrentVersionInfo,
|
getCurrentVersionInfo,
|
||||||
|
currentVersionInfo,
|
||||||
parseTestQrCode,
|
parseTestQrCode,
|
||||||
restartApp,
|
restartApp,
|
||||||
}}>
|
}}>
|
||||||
|
|||||||
33
src/type.ts
33
src/type.ts
@@ -1,14 +1,10 @@
|
|||||||
export interface CheckResult {
|
export interface VersionInfo {
|
||||||
upToDate?: true;
|
name: string;
|
||||||
expired?: true;
|
hash: string;
|
||||||
downloadUrl?: string;
|
description: string;
|
||||||
update?: true;
|
metaInfo: string;
|
||||||
name?: string; // version name
|
config: {
|
||||||
hash?: string;
|
rollout: {
|
||||||
description?: string;
|
|
||||||
metaInfo?: string;
|
|
||||||
config?: {
|
|
||||||
rollout?: {
|
|
||||||
[packageVersion: string]: number;
|
[packageVersion: string]: number;
|
||||||
};
|
};
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
@@ -16,11 +12,23 @@ export interface CheckResult {
|
|||||||
pdiff?: string;
|
pdiff?: string;
|
||||||
diff?: string;
|
diff?: string;
|
||||||
full?: string;
|
full?: string;
|
||||||
paths?: string[];
|
}
|
||||||
|
|
||||||
|
interface RootResult {
|
||||||
|
upToDate?: true;
|
||||||
|
expired?: true;
|
||||||
|
downloadUrl?: string;
|
||||||
|
update?: true;
|
||||||
paused?: 'app' | 'package';
|
paused?: 'app' | 'package';
|
||||||
message?: string;
|
message?: string;
|
||||||
|
paths?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CheckResult = RootResult &
|
||||||
|
Partial<VersionInfo> & {
|
||||||
|
expVersion?: VersionInfo;
|
||||||
|
};
|
||||||
|
|
||||||
export interface ProgressData {
|
export interface ProgressData {
|
||||||
hash: string;
|
hash: string;
|
||||||
received: number;
|
received: number;
|
||||||
@@ -78,6 +86,7 @@ export interface ClientOptions {
|
|||||||
appKey: string;
|
appKey: string;
|
||||||
server?: UpdateServerConfig;
|
server?: UpdateServerConfig;
|
||||||
logger?: UpdateEventsLogger;
|
logger?: UpdateEventsLogger;
|
||||||
|
locale?: 'zh' | 'en';
|
||||||
updateStrategy?:
|
updateStrategy?:
|
||||||
| 'alwaysAlert'
|
| 'alwaysAlert'
|
||||||
| 'alertUpdateAndIgnoreError'
|
| 'alertUpdateAndIgnoreError'
|
||||||
|
|||||||
102
src/utils.ts
102
src/utils.ts
@@ -1,9 +1,12 @@
|
|||||||
import { Platform } from 'react-native';
|
import { Platform } from 'react-native';
|
||||||
|
import i18n from './i18n';
|
||||||
|
|
||||||
export function log(...args: any[]) {
|
export function log(...args: any[]) {
|
||||||
console.log('react-native-update: ', ...args);
|
console.log(i18n.t('dev_log_prefix'), ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const isWeb = Platform.OS === 'web';
|
||||||
|
|
||||||
export function promiseAny<T>(promises: Promise<T>[]) {
|
export function promiseAny<T>(promises: Promise<T>[]) {
|
||||||
return new Promise<T>((resolve, reject) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
@@ -14,7 +17,7 @@ export function promiseAny<T>(promises: Promise<T>[]) {
|
|||||||
.catch(() => {
|
.catch(() => {
|
||||||
count++;
|
count++;
|
||||||
if (count === promises.length) {
|
if (count === promises.length) {
|
||||||
reject(new Error('All promises were rejected'));
|
reject(Error(i18n.t('error_all_promises_rejected')));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -34,38 +37,37 @@ class EmptyModule {
|
|||||||
}
|
}
|
||||||
export const emptyModule = new EmptyModule();
|
export const emptyModule = new EmptyModule();
|
||||||
|
|
||||||
const ping =
|
const ping = isWeb
|
||||||
Platform.OS === 'web'
|
? Promise.resolve
|
||||||
? Promise.resolve
|
: async (url: string) => {
|
||||||
: async (url: string) => {
|
let pingFinished = false;
|
||||||
let pingFinished = false;
|
return Promise.race([
|
||||||
return Promise.race([
|
enhancedFetch(url, {
|
||||||
enhancedFetch(url, {
|
method: 'HEAD',
|
||||||
method: 'HEAD',
|
})
|
||||||
|
.then(({ status, statusText, url: finalUrl }) => {
|
||||||
|
pingFinished = true;
|
||||||
|
if (status === 200) {
|
||||||
|
return finalUrl;
|
||||||
|
}
|
||||||
|
log('ping failed', url, status, statusText);
|
||||||
|
throw Error(i18n.t('error_ping_failed'));
|
||||||
})
|
})
|
||||||
.then(({ status, statusText, url: finalUrl }) => {
|
.catch(e => {
|
||||||
pingFinished = true;
|
pingFinished = true;
|
||||||
if (status === 200) {
|
log('ping error', url, e);
|
||||||
return finalUrl;
|
throw e;
|
||||||
}
|
}),
|
||||||
log('ping failed', url, status, statusText);
|
new Promise((_, reject) =>
|
||||||
throw new Error('Ping failed');
|
setTimeout(() => {
|
||||||
})
|
reject(Error(i18n.t('error_ping_timeout')));
|
||||||
.catch(e => {
|
if (!pingFinished) {
|
||||||
pingFinished = true;
|
log('ping timeout', url);
|
||||||
log('ping error', url, e);
|
}
|
||||||
throw e;
|
}, 5000),
|
||||||
}),
|
),
|
||||||
new Promise((_, reject) =>
|
]);
|
||||||
setTimeout(() => {
|
};
|
||||||
reject(new Error('Ping timeout'));
|
|
||||||
if (!pingFinished) {
|
|
||||||
log('ping timeout', url);
|
|
||||||
}
|
|
||||||
}, 5000),
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
|
|
||||||
export function joinUrls(paths: string[], fileName?: string) {
|
export function joinUrls(paths: string[], fileName?: string) {
|
||||||
if (fileName) {
|
if (fileName) {
|
||||||
@@ -90,10 +92,8 @@ export const testUrls = async (urls?: string[]) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const assertWeb = () => {
|
export const assertWeb = () => {
|
||||||
if (Platform.OS === 'web') {
|
if (isWeb) {
|
||||||
console.warn(
|
console.warn(i18n.t('dev_web_not_supported'));
|
||||||
'react-native-update does not support the Web platform and will not perform any operations',
|
|
||||||
);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -108,10 +108,26 @@ export const assertWeb = () => {
|
|||||||
export const enhancedFetch = async (
|
export const enhancedFetch = async (
|
||||||
url: string,
|
url: string,
|
||||||
params: Parameters<typeof fetch>[1],
|
params: Parameters<typeof fetch>[1],
|
||||||
) => {
|
isRetry = false,
|
||||||
return fetch(url, params).catch(e => {
|
): Promise<Response> => {
|
||||||
log('fetch error', url, e);
|
return fetch(url, params)
|
||||||
log('trying fallback to http');
|
.then(r => {
|
||||||
return fetch(url.replace('https', 'http'), params);
|
if (r.ok) {
|
||||||
});
|
return r;
|
||||||
|
}
|
||||||
|
throw Error(
|
||||||
|
i18n.t('error_http_status', {
|
||||||
|
status: r.status,
|
||||||
|
statusText: r.statusText,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
log('fetch error', url, e);
|
||||||
|
if (isRetry) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
log('trying fallback to http');
|
||||||
|
return enhancedFetch(url.replace('https', 'http'), params, true);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"extends": "@react-native/typescript-config/tsconfig.json",
|
"extends": "@react-native/typescript-config",
|
||||||
"include": ["src/**/*"]
|
"include": ["**/*.ts", "**/*.tsx"],
|
||||||
|
"exclude": ["**/node_modules", "**/Pods", "**/harmony", "**/Example"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user