@@ -3,10 +3,13 @@ import {
33 TurboModuleContext ,
44} from '@rnoh/react-native-openharmony/ts' ;
55import common from '@ohos.app.ability.common' ;
6+ import dataPreferences from '@ohos.data.preferences' ;
7+ import { bundleManager } from '@kit.AbilityKit' ;
68import logger from './Logger' ;
79import { UpdateModuleImpl } from './UpdateModuleImpl' ;
810import { UpdateContext } from './UpdateContext' ;
911import { EventHub } from './EventHub' ;
12+ import { util } from '@kit.ArkTS' ;
1013
1114const TAG = 'PushyTurboModule' ;
1215
@@ -18,25 +21,86 @@ export class PushyTurboModule extends TurboModule {
1821 super ( ctx ) ;
1922 logger . debug ( TAG , ',PushyTurboModule constructor' ) ;
2023 this . mUiCtx = ctx . uiAbilityContext ;
21- this . context = UpdateContext . getInstance ( this . mUiCtx ) ;
24+ this . context = new UpdateContext ( this . mUiCtx ) ;
2225 EventHub . getInstance ( ) . setRNInstance ( ctx . rnInstance ) ;
2326 }
2427
2528 getConstants ( ) : Object {
2629 logger . debug ( TAG , ',call getConstants' ) ;
27- const { isFirstTime, rolledBackVersion } =
28- this . context . consumeLaunchMarks ( ) ;
29- const uuid = this . context . getKv ( 'uuid' ) ;
30- const currentVersion = this . context . getCurrentVersion ( ) ;
30+ const context = this . mUiCtx ;
31+ const preferencesManager = dataPreferences . getPreferencesSync ( context , {
32+ name : 'update' ,
33+ } ) ;
34+ const isFirstTime = preferencesManager . getSync (
35+ 'isFirstTime' ,
36+ false ,
37+ ) as boolean ;
38+ const rolledBackVersion = preferencesManager . getSync (
39+ 'rolledBackVersion' ,
40+ '' ,
41+ ) as string ;
42+ const uuid = preferencesManager . getSync ( 'uuid' , '' ) as string ;
43+ const currentVersion = preferencesManager . getSync (
44+ 'currentVersion' ,
45+ '' ,
46+ ) as string ;
3147 const currentVersionInfo = this . context . getKv ( `hash_${ currentVersion } ` ) ;
3248
49+ const isUsingBundleUrl = this . context . getIsUsingBundleUrl ( ) ;
50+ let bundleFlags =
51+ bundleManager . BundleFlag . GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION ;
52+ let packageVersion = '' ;
53+ try {
54+ const bundleInfo = bundleManager . getBundleInfoForSelfSync ( bundleFlags ) ;
55+ packageVersion = bundleInfo ?. versionName || 'Unknown' ;
56+ } catch ( error ) {
57+ console . error ( 'Failed to get bundle info:' , error ) ;
58+ }
59+ const storedPackageVersion = preferencesManager . getSync (
60+ 'packageVersion' ,
61+ '' ,
62+ ) as string ;
63+ const storedBuildTime = preferencesManager . getSync (
64+ 'buildTime' ,
65+ '' ,
66+ ) as string ;
67+ let buildTime = '' ;
68+ try {
69+ const resourceManager = this . mUiCtx . resourceManager ;
70+ const content = resourceManager . getRawFileContentSync ( 'meta.json' ) ;
71+ const metaData = JSON . parse (
72+ new util . TextDecoder ( ) . decodeToString ( content ) ,
73+ ) ;
74+ if ( metaData . pushy_build_time ) {
75+ buildTime = String ( metaData . pushy_build_time ) ;
76+ }
77+ } catch { }
78+
79+ const packageVersionChanged =
80+ ! storedPackageVersion || packageVersion !== storedPackageVersion ;
81+ const buildTimeChanged = ! storedBuildTime || buildTime !== storedBuildTime ;
82+
83+ if ( packageVersionChanged || buildTimeChanged ) {
84+ this . context . cleanUp ( ) ;
85+ preferencesManager . putSync ( 'packageVersion' , packageVersion ) ;
86+ preferencesManager . putSync ( 'buildTime' , buildTime ) ;
87+ }
88+
89+ if ( isFirstTime ) {
90+ preferencesManager . deleteSync ( 'isFirstTime' ) ;
91+ }
92+
93+ if ( rolledBackVersion ) {
94+ preferencesManager . deleteSync ( 'rolledBackVersion' ) ;
95+ }
96+
3397 return {
34- downloadRootDir : this . context . getRootDir ( ) ,
98+ downloadRootDir : ` ${ context . filesDir } /_update` ,
3599 currentVersionInfo,
36- packageVersion : this . context . getPackageVersion ( ) ,
100+ packageVersion,
37101 currentVersion,
38- buildTime : this . context . getBuildTime ( ) ,
39- isUsingBundleUrl : this . context . getIsUsingBundleUrl ( ) ,
102+ buildTime,
103+ isUsingBundleUrl,
40104 isFirstTime,
41105 rolledBackVersion,
42106 uuid,
0 commit comments