Skip to content

Commit c159454

Browse files
committed
fix harmony error
1 parent 37d9a94 commit c159454

4 files changed

Lines changed: 28 additions & 117 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ export class DownloadTask {
433433
throw Error(`Unknown task type: ${params.type}`);
434434
}
435435

436-
params.listener?.onDownloadCompleted(params);
437436
} catch (error) {
438437
console.error('Task execution failed:', error.message);
439438
if (params.type !== DownloadTaskParams.TASK_TYPE_CLEANUP) {
@@ -447,7 +446,7 @@ export class DownloadTask {
447446
console.error('Cleanup after error failed:', cleanupError.message);
448447
}
449448
}
450-
params.listener?.onDownloadFailed(error);
449+
throw error;
451450
}
452451
}
453452
}
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
export interface DownloadTaskListener {
2-
onDownloadCompleted(params: DownloadTaskParams): void;
3-
onDownloadFailed(error: Error): void;
4-
}
5-
61
/**
72
* 下载任务参数类
83
*/
94
export class DownloadTaskParams {
105
// 任务类型常量
11-
static readonly TASK_TYPE_CLEANUP: number = 0; // 保留hash和originHash
12-
static readonly TASK_TYPE_PATCH_FULL: number = 1; // 全量补丁
6+
static readonly TASK_TYPE_CLEANUP: number = 0; // 保留hash和originHash
7+
static readonly TASK_TYPE_PATCH_FULL: number = 1; // 全量补丁
138
static readonly TASK_TYPE_PATCH_FROM_APP: number = 2; // 从APP补丁
149
static readonly TASK_TYPE_PATCH_FROM_PPK: number = 3; // 从PPK补丁
1510
static readonly TASK_TYPE_PLAIN_DOWNLOAD: number = 4; // 普通下载
1611

17-
type: number; // 任务类型
18-
url: string; // 下载URL
19-
hash: string; // 文件哈希值
20-
originHash: string; // 原始文件哈希值
21-
targetFile: string; // 目标文件路径
22-
unzipDirectory: string; // 解压目录路径
23-
originDirectory: string; // 原始文件目录路径
24-
listener: DownloadTaskListener; // 下载监听器
25-
}
12+
type: number; // 任务类型
13+
url: string; // 下载URL
14+
hash: string; // 文件哈希值
15+
originHash: string; // 原始文件哈希值
16+
targetFile: string; // 目标文件路径
17+
unzipDirectory: string; // 解压目录路径
18+
originDirectory: string; // 原始文件目录路径
19+
}

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,37 +89,31 @@ export class UpdateContext {
8989
this.cleanUp();
9090
}
9191

92-
public async downloadFullUpdate(
93-
url: string,
94-
hash: string,
95-
listener: DownloadFileListener,
96-
): Promise<void> {
92+
public async downloadFullUpdate(url: string, hash: string): Promise<void> {
9793
try {
9894
const params = new DownloadTaskParams();
9995
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FULL;
10096
params.url = url;
10197
params.hash = hash;
102-
params.listener = listener;
10398
params.targetFile = `${this.rootDir}/${hash}.ppk`;
10499
params.unzipDirectory = `${this.rootDir}/${hash}`;
105100
const downloadTask = new DownloadTask(this.context);
106101
await downloadTask.execute(params);
107102
} catch (e) {
108103
console.error('Failed to download full update:', e);
104+
throw e;
109105
}
110106
}
111107

112108
public async downloadFile(
113109
url: string,
114110
hash: string,
115111
fileName: string,
116-
listener: DownloadFileListener,
117112
): Promise<void> {
118113
const params = new DownloadTaskParams();
119114
params.type = DownloadTaskParams.TASK_TYPE_PLAIN_DOWNLOAD;
120115
params.url = url;
121116
params.hash = hash;
122-
params.listener = listener;
123117
params.targetFile = this.rootDir + '/' + fileName;
124118

125119
const downloadTask = new DownloadTask(this.context);
@@ -130,14 +124,12 @@ export class UpdateContext {
130124
url: string,
131125
hash: string,
132126
originHash: string,
133-
listener: DownloadFileListener,
134127
): Promise<void> {
135128
const params = new DownloadTaskParams();
136129
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_PPK;
137130
params.url = url;
138131
params.hash = hash;
139132
params.originHash = originHash;
140-
params.listener = listener;
141133
params.targetFile = `${this.rootDir}/${originHash}_${hash}.ppk.patch`;
142134
params.unzipDirectory = `${this.rootDir}/${hash}`;
143135
params.originDirectory = `${this.rootDir}/${params.originHash}`;
@@ -149,21 +141,19 @@ export class UpdateContext {
149141
public async downloadPatchFromPackage(
150142
url: string,
151143
hash: string,
152-
listener: DownloadFileListener,
153144
): Promise<void> {
154145
try {
155146
const params = new DownloadTaskParams();
156147
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_APP;
157148
params.url = url;
158149
params.hash = hash;
159-
params.listener = listener;
160150
params.targetFile = `${this.rootDir}/${hash}.app.patch`;
161151
params.unzipDirectory = `${this.rootDir}/${hash}`;
162152

163153
const downloadTask = new DownloadTask(this.context);
164154
return await downloadTask.execute(params);
165155
} catch (e) {
166-
console.error('Failed to download APK patch:', e);
156+
console.error('Failed to download package patch:', e);
167157
throw e;
168158
}
169159
}
@@ -186,6 +176,7 @@ export class UpdateContext {
186176
this.setKv('rolledBackVersion', '');
187177
} catch (e) {
188178
console.error('Failed to switch version:', e);
179+
throw e;
189180
}
190181
}
191182

@@ -269,8 +260,3 @@ export class UpdateContext {
269260
return UpdateContext.isUsingBundleUrl;
270261
}
271262
}
272-
273-
export interface DownloadFileListener {
274-
onDownloadCompleted(params: DownloadTaskParams): void;
275-
onDownloadFailed(error: Error): void;
276-
}

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

Lines changed: 13 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import bundleManager from '@ohos.bundle.bundleManager';
22
import common from '@ohos.app.ability.common';
33
import { UpdateContext } from './UpdateContext';
4-
import { DownloadTaskParams } from './DownloadTaskParams';
54
import logger from './Logger';
65

76
const TAG = 'UpdateModuleImpl';
@@ -13,89 +12,28 @@ export class UpdateModuleImpl {
1312
updateContext: UpdateContext,
1413
options: { updateUrl: string; hash: string },
1514
): Promise<void> {
16-
try {
17-
await updateContext.downloadFullUpdate(options.updateUrl, options.hash, {
18-
onDownloadCompleted: (params: DownloadTaskParams) => {
19-
return Promise.resolve();
20-
},
21-
onDownloadFailed: (error: Error) => {
22-
return Promise.reject(error);
23-
},
24-
});
25-
} catch (error) {
26-
logger.error(TAG, `downloadFullUpdate failed: ${error}`);
27-
throw error;
28-
}
29-
}
30-
31-
static async downloadAndInstallApk(
32-
context: common.UIAbilityContext,
33-
options: { url: string; hash: string; target: string },
34-
): Promise<void> {
35-
try {
36-
const want = {
37-
action: 'action.system.home',
38-
parameters: {
39-
uri: 'appmarket://details',
40-
},
41-
};
42-
43-
if (!context) {
44-
throw Error('获取context失败');
45-
}
46-
47-
await context.startAbility(want);
48-
} catch (error) {
49-
logger.error(TAG, `installApk failed: ${error}`);
50-
throw error;
51-
}
15+
return updateContext.downloadFullUpdate(options.updateUrl, options.hash);
5216
}
5317

5418
static async downloadPatchFromPackage(
5519
updateContext: UpdateContext,
5620
options: { updateUrl: string; hash: string },
5721
): Promise<void> {
58-
try {
59-
return await updateContext.downloadPatchFromPackage(
60-
options.updateUrl,
61-
options.hash,
62-
{
63-
onDownloadCompleted: (params: DownloadTaskParams) => {
64-
return Promise.resolve();
65-
},
66-
onDownloadFailed: (error: Error) => {
67-
return Promise.reject(error);
68-
},
69-
},
70-
);
71-
} catch (error) {
72-
logger.error(TAG, `downloadPatchFromPackage failed: ${error}`);
73-
throw error;
74-
}
22+
return updateContext.downloadPatchFromPackage(
23+
options.updateUrl,
24+
options.hash,
25+
);
7526
}
7627

7728
static async downloadPatchFromPpk(
7829
updateContext: UpdateContext,
7930
options: { updateUrl: string; hash: string; originHash: string },
8031
): Promise<void> {
81-
try {
82-
await updateContext.downloadPatchFromPpk(
83-
options.updateUrl,
84-
options.hash,
85-
options.originHash,
86-
{
87-
onDownloadCompleted: (params: DownloadTaskParams) => {
88-
return Promise.resolve();
89-
},
90-
onDownloadFailed: (error: Error) => {
91-
return Promise.reject(error);
92-
},
93-
},
94-
);
95-
} catch (error) {
96-
logger.error(TAG, `downloadPatchFromPpk failed: ${error}`);
97-
throw Error(`执行报错: ${error.message}`);
98-
}
32+
return updateContext.downloadPatchFromPpk(
33+
options.updateUrl,
34+
options.hash,
35+
options.originHash,
36+
);
9937
}
10038

10139
static async reloadUpdate(
@@ -121,7 +59,7 @@ export class UpdateModuleImpl {
12159
await context.startAbility(want);
12260
} catch (error) {
12361
logger.error(TAG, `reloadUpdate failed: ${error}`);
124-
throw Error(`pushy:switchVersion failed ${error.message}`);
62+
throw Error(`switchVersion failed ${error.message}`);
12563
}
12664
}
12765

@@ -156,14 +94,8 @@ export class UpdateModuleImpl {
15694
static async setUuid(
15795
updateContext: UpdateContext,
15896
uuid: string,
159-
): Promise<boolean> {
160-
try {
161-
await updateContext.setKv('uuid', uuid);
162-
return true;
163-
} catch (error) {
164-
logger.error(TAG, `setUuid failed: ${error}`);
165-
throw Error(`执行报错: ${error.message}`);
166-
}
97+
): Promise<void> {
98+
return updateContext.setKv('uuid', uuid);
16799
}
168100

169101
static checkJson(json: string): boolean {

0 commit comments

Comments
 (0)