@@ -3,6 +3,8 @@ import fileIo from '@ohos.file.fs';
33import { DownloadTask } from './DownloadTask' ;
44import common from '@ohos.app.ability.common' ;
55import { DownloadTaskParams } from './DownloadTaskParams' ;
6+ import { bundleManager } from '@kit.AbilityKit' ;
7+ import { util } from '@kit.ArkTS' ;
68import NativePatchCore , {
79 STATE_OP_CLEAR_FIRST_TIME ,
810 STATE_OP_CLEAR_ROLLBACK_MARK ,
@@ -13,6 +15,10 @@ import NativePatchCore, {
1315 StateCoreResult ,
1416} from './NativePatchCore' ;
1517
18+ type FlushablePreferences = preferences . Preferences & {
19+ flushSync ?: ( ) => void ;
20+ } ;
21+
1622export class UpdateContext {
1723 private context : common . UIAbilityContext ;
1824 private rootDir : string ;
@@ -32,6 +38,10 @@ export class UpdateContext {
3238 console . error ( 'Failed to create root directory:' , e ) ;
3339 }
3440 this . initPreferences ( ) ;
41+ this . syncStateWithBinaryVersion (
42+ this . getPackageVersion ( ) ,
43+ this . getBuildTime ( ) ,
44+ ) ;
3545 }
3646
3747 private initPreferences ( ) {
@@ -44,6 +54,37 @@ export class UpdateContext {
4454 }
4555 }
4656
57+ private getBundleFlags ( ) : bundleManager . BundleFlag {
58+ return bundleManager . BundleFlag . GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION ;
59+ }
60+
61+ public getPackageVersion ( ) : string {
62+ try {
63+ const bundleInfo = bundleManager . getBundleInfoForSelfSync (
64+ this . getBundleFlags ( ) ,
65+ ) ;
66+ return bundleInfo ?. versionName || 'Unknown' ;
67+ } catch ( error ) {
68+ console . error ( 'Failed to get bundle info:' , error ) ;
69+ return '' ;
70+ }
71+ }
72+
73+ public getBuildTime ( ) : string {
74+ try {
75+ const content = this . context . resourceManager . getRawFileContentSync (
76+ 'meta.json' ,
77+ ) ;
78+ const metaData = JSON . parse (
79+ new util . TextDecoder ( ) . decodeToString ( content ) ,
80+ ) as Record < string , string | number | boolean | null | undefined > ;
81+ if ( metaData . pushy_build_time ) {
82+ return String ( metaData . pushy_build_time ) ;
83+ }
84+ } catch { }
85+ return '' ;
86+ }
87+
4788 private readString ( key : string ) : string {
4889 const value = this . preferences . getSync ( key , '' ) as
4990 | string
@@ -87,6 +128,20 @@ export class UpdateContext {
87128 return defaultValue ;
88129 }
89130
131+ private flushPreferences ( reason : string ) : void {
132+ try {
133+ const flushablePreferences = this . preferences as FlushablePreferences ;
134+ if ( typeof flushablePreferences . flushSync === 'function' ) {
135+ flushablePreferences . flushSync ( ) ;
136+ return ;
137+ }
138+ throw Error ( 'preferences.flushSync is not available' ) ;
139+ } catch ( error ) {
140+ console . error ( `Failed to flush preferences for ${ reason } :` , error ) ;
141+ throw error ;
142+ }
143+ }
144+
90145 private putNullableString ( key : string , value ?: string ) : void {
91146 if ( value ) {
92147 this . preferences . putSync ( key , value ) ;
@@ -136,7 +191,7 @@ export class UpdateContext {
136191 if ( options . removeStaleHash && state . staleVersionToDelete ) {
137192 this . preferences . deleteSync ( `hash_${ state . staleVersionToDelete } ` ) ;
138193 }
139- this . preferences . flush ( ) ;
194+ this . flushPreferences ( 'persist state' ) ;
140195 if ( options . cleanUp ) {
141196 this . cleanUp ( ) ;
142197 }
@@ -196,7 +251,7 @@ export class UpdateContext {
196251
197252 public setKv ( key : string , value : string ) : void {
198253 this . preferences . putSync ( key , value ) ;
199- this . preferences . flush ( ) ;
254+ this . flushPreferences ( `set key ${ key } ` ) ;
200255 }
201256
202257 public getKv ( key : string ) : string {
0 commit comments