Skip to content

Commit f865a16

Browse files
committed
fix(harmony): persist update state before restart
1 parent 44f9a80 commit f865a16

2 files changed

Lines changed: 59 additions & 32 deletions

File tree

harmony/pushy/src/main/ets/PushyTurboModule.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { bundleManager } from '@kit.AbilityKit';
77
import logger from './Logger';
88
import { UpdateContext } from './UpdateContext';
99
import { EventHub } from './EventHub';
10-
import { util } from '@kit.ArkTS';
1110

1211
const TAG = 'PushyTurboModule';
1312

@@ -47,33 +46,6 @@ export class PushyTurboModule extends UITurboModule {
4746
return bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
4847
}
4948

50-
private getPackageVersion(): string {
51-
try {
52-
const bundleInfo = bundleManager.getBundleInfoForSelfSync(
53-
this.getBundleFlags(),
54-
);
55-
return bundleInfo?.versionName || 'Unknown';
56-
} catch (error) {
57-
console.error('Failed to get bundle info:', error);
58-
return '';
59-
}
60-
}
61-
62-
private getBuildTime(): string {
63-
try {
64-
const content = this.mUiCtx.resourceManager.getRawFileContentSync(
65-
'meta.json',
66-
);
67-
const metaData = JSON.parse(
68-
new util.TextDecoder().decodeToString(content),
69-
) as Record<string, string | number | boolean | null | undefined>;
70-
if (metaData.pushy_build_time) {
71-
return String(metaData.pushy_build_time);
72-
}
73-
} catch {}
74-
return '';
75-
}
76-
7749
private requireHash(hash: string, methodName: string): string {
7850
if (!hash) {
7951
throw Error(`${methodName}: empty hash`);
@@ -95,8 +67,8 @@ export class PushyTurboModule extends UITurboModule {
9567

9668
getConstants(): Object {
9769
logger.debug(TAG, ',call getConstants');
98-
const packageVersion = this.getPackageVersion();
99-
const buildTime = this.getBuildTime();
70+
const packageVersion = this.context.getPackageVersion();
71+
const buildTime = this.context.getBuildTime();
10072
this.context.syncStateWithBinaryVersion(packageVersion, buildTime);
10173

10274
const currentVersion = this.context.getCurrentVersion();

harmony/pushy/src/main/ets/UpdateContext.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import fileIo from '@ohos.file.fs';
33
import { DownloadTask } from './DownloadTask';
44
import common from '@ohos.app.ability.common';
55
import { DownloadTaskParams } from './DownloadTaskParams';
6+
import { bundleManager } from '@kit.AbilityKit';
7+
import { util } from '@kit.ArkTS';
68
import 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+
1622
export 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

Comments
 (0)