import bundleManager from '@ohos.bundle.bundleManager' import common from '@ohos.app.ability.common' import request from '@ohos.request' import type { BusinessError } from '@ohos.base' import { HttpClient } from '../core/HttpClient' import type { AppVersionInfo, InstalledRnBundleInfo, RnBundleInfo } from '../core/Types' import { SDKContext } from '../core/SDKContext' export interface AppUpdateResult { hasUpdate: boolean info?: AppVersionInfo } export interface RnUpdateResult { hasUpdate: boolean info?: RnBundleInfo } export class UpdateSDK { static async checkAppUpdate(appKey: string): Promise { const bundleInfo = bundleManager.getBundleInfoForSelfSync( bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT ) const currentVersionCode = bundleInfo.versionCode const data = await HttpClient.get( `/api/v1/updates/app/check?appKey=${appKey}&versionCode=${currentVersionCode}&platform=HARMONY` ) if (data.latestVersionCode <= currentVersionCode) { return { hasUpdate: false } } return { hasUpdate: true, info: data } } static async openAppMarket(context: common.UIAbilityContext, url?: string): Promise { if (!url) { return } await context.openLink(url, { appLinkingOnly: false }) } static async checkRnUpdate( appKey: string, bundleName: string, currentBundleVersion?: number, packageName?: string ): Promise { const localBundleVersion = currentBundleVersion ?? await SDKContext.getRnBundleVersion(bundleName) ?? 0 const data = await HttpClient.get( `/api/v1/rn/update/check?appKey=${appKey}&bundleName=${bundleName}&bundleVersion=${localBundleVersion}${packageName ? `&packageName=${encodeURIComponent(packageName)}` : ''}` ) if (data.bundleVersion <= localBundleVersion) { return { hasUpdate: false } } if (packageName && data.packageMatched === false) { return { hasUpdate: false, info: data } } return { hasUpdate: true, info: data } } static async downloadRnBundle( context: common.UIAbilityContext, downloadUrl: string, destFilename: string, bundleName?: string, bundleInfo?: RnBundleInfo, ): Promise { const destPath = context.cacheDir + '/' + destFilename await new Promise((resolve, reject) => { request.downloadFile(context, { url: downloadUrl, filePath: destPath, }, (err: BusinessError | null, task: request.DownloadTask) => { if (err) { reject(err); return } task.on('complete', resolve) task.on('fail', (error: number) => reject(new Error(`Download failed: ${error}`))) }) }) if (bundleName && bundleInfo) { const installedInfo: InstalledRnBundleInfo = { bundleVersion: bundleInfo.bundleVersion, packageName: bundleInfo.packageName, minCommonVersion: bundleInfo.minCommonVersion, installedAt: Date.now(), } await SDKContext.setRnBundleInfo(bundleName, installedInfo) } if (SDKContext.getConfig().debug) { console.log('[UpdateSDK] RN bundle downloaded to', destPath) } return destPath } static async rememberRnBundleInfo( bundleName: string, bundleVersion: number, packageName?: string, minCommonVersion?: string, ): Promise { await SDKContext.setRnBundleInfo(bundleName, { bundleVersion, packageName, minCommonVersion, installedAt: Date.now(), }) } static async getInstalledRnBundleInfo(bundleName: string): Promise { return SDKContext.getRnBundleInfo(bundleName) } }