diff --git a/harmony/pushy/src/main/ets/PushyFileJSBundleProvider.ets b/harmony/pushy/src/main/ets/PushyFileJSBundleProvider.ets index eba7cdb..19476da 100644 --- a/harmony/pushy/src/main/ets/PushyFileJSBundleProvider.ets +++ b/harmony/pushy/src/main/ets/PushyFileJSBundleProvider.ets @@ -1,49 +1,41 @@ -import { HotReloadConfig, JSBundleProvider, JSBundleProviderError, JSPackagerClientConfig } from '@rnoh/react-native-openharmony'; -import fileIo from '@ohos.file.fs'; +import { + FileJSBundle, + HotReloadConfig, + JSBundleProvider, + JSBundleProviderError +} from '@rnoh/react-native-openharmony'; import common from '@ohos.app.ability.common'; +import fs from '@ohos.file.fs'; import { UpdateContext } from './UpdateContext'; export class PushyFileJSBundleProvider extends JSBundleProvider { private updateContext: UpdateContext; - private filePath: string = '' + private path: string = '' constructor(context: common.UIAbilityContext) { super(); this.updateContext = new UpdateContext(context); - } - getURL(): string { - return this.updateContext.getBundleUrl()?.substring(1); + this.path = this.updateContext.getBundleUrl(); } - async getBundle(): Promise { + getURL(): string { + return this.path; + } + + async getBundle(): Promise { try { - this.filePath = this.updateContext.getBundleUrl(); - const res = fileIo.accessSync(this.filePath); - if (res) { - const file = fileIo.openSync(this.filePath, fileIo.OpenMode.READ_ONLY); - try { - const stat = await fileIo.stat(this.filePath); - const fileSize = stat.size; - const buffer = new ArrayBuffer(fileSize); - const bytesRead = fileIo.readSync(file.fd, buffer, { - offset: 0, - length: fileSize - }); - - if (bytesRead !== fileSize) { - throw new Error(`Failed to read entire file: read ${bytesRead} of ${fileSize} bytes`); - } - return buffer; - } finally { - fileIo.closeSync(file.fd); + const status = await fs.access(this.path, fs.OpenMode.READ_ONLY); + if (status) { + return { + filePath: this.path } } throw new Error('Update bundle not found'); } catch (error) { throw new JSBundleProviderError({ - whatHappened: `Couldn't load JSBundle from ${this.filePath}`, + whatHappened: `Couldn't load JSBundle from ${this.path}`, extraData: error, - howCanItBeFixed: [`Check if a bundle exists at "${this.filePath}" on your device.`] + howCanItBeFixed: [`Check if a bundle exists at "${this.path}" on your device.`] }) } } diff --git a/harmony/pushy/src/main/ets/PushyTurboModule.ts b/harmony/pushy/src/main/ets/PushyTurboModule.ts index 5db9950..392d880 100644 --- a/harmony/pushy/src/main/ets/PushyTurboModule.ts +++ b/harmony/pushy/src/main/ets/PushyTurboModule.ts @@ -1,9 +1,10 @@ -import { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts'; +import { + TurboModule, + TurboModuleContext, +} from '@rnoh/react-native-openharmony/ts'; import common from '@ohos.app.ability.common'; import dataPreferences from '@ohos.data.preferences'; import { bundleManager } from '@kit.AbilityKit'; -import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; -import { BusinessError } from '@ohos.base'; import logger from './Logger'; import { UpdateModuleImpl } from './UpdateModuleImpl'; import { UpdateContext } from './UpdateContext'; @@ -23,50 +24,60 @@ export class PushyTurboModule extends TurboModule { EventHub.getInstance().setRNInstance(ctx.rnInstance); } + getConstants(): Object { + logger.debug(TAG, ',call getConstants'); + const context = this.mUiCtx; + const preferencesManager = dataPreferences.getPreferencesSync(context, { + name: 'update', + }); + const isFirstTime = preferencesManager.getSync( + 'isFirstTime', + false, + ) as boolean; + const rolledBackVersion = preferencesManager.getSync( + 'rolledBackVersion', + '', + ) as string; + const uuid = preferencesManager.getSync('uuid', '') as string; + const currentVersion = preferencesManager.getSync( + 'currentVersion', + '', + ) as string; + const currentVersionInfo = this.context.getKv(`hash_${currentVersion}`); + const buildTime = preferencesManager.getSync('buildTime', '') as string; + const isUsingBundleUrl = this.context.getIsUsingBundleUrl(); + let bundleFlags = + bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION; + let packageVersion = ''; + try { + const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags); + packageVersion = bundleInfo?.versionName || 'Unknown'; + } catch (error) { + console.error('Failed to get bundle info:', error); + } -getConstants(): Object { - logger.debug(TAG, ',call getConstants'); - const context = this.mUiCtx; - const preferencesManager = dataPreferences.getPreferencesSync(context,{ name: 'update' }); - const isFirstTime = preferencesManager.getSync('isFirstTime', false) as boolean; - const rolledBackVersion = preferencesManager.getSync('rolledBackVersion', '') as string; - const uuid = preferencesManager.getSync('uuid', '') as string; - const currentVersion = preferencesManager.getSync('currentVersion', '') as string; - const currentVersionInfo = this.context.getKv(`hash_${currentVersion}`); - const buildTime = preferencesManager.getSync('buildTime', '') as string; - const isUsingBundleUrl = this.context.getIsUsingBundleUrl(); - let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION; - let packageVersion = ''; - try { - const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags); - packageVersion = bundleInfo?.versionName || 'Unknown'; - } catch (error) { - console.error('Failed to get bundle info:', error); + if (isFirstTime) { + preferencesManager.deleteSync('isFirstTime'); + } + + if (rolledBackVersion) { + preferencesManager.deleteSync('rolledBackVersion'); + } + + return { + downloadRootDir: `${context.filesDir}/_update`, + currentVersionInfo, + packageVersion, + currentVersion, + buildTime, + isUsingBundleUrl, + isFirstTime, + rolledBackVersion, + uuid, + }; } - if (isFirstTime) { - preferencesManager.deleteSync('isFirstTime'); - } - - if (rolledBackVersion) { - preferencesManager.deleteSync('rolledBackVersion'); - } - - return { - downloadRootDir: `${context.filesDir}/_update`, - currentVersionInfo, - packageVersion, - currentVersion, - buildTime, - isUsingBundleUrl, - isFirstTime, - rolledBackVersion, - uuid, - }; -} - - - setLocalHashInfo(hash: string, info: string): boolean { + setLocalHashInfo(hash: string, info: string): boolean { logger.debug(TAG, ',call setLocalHashInfo'); return UpdateModuleImpl.setLocalHashInfo(this.context, hash, info); } @@ -75,9 +86,9 @@ getConstants(): Object { return UpdateModuleImpl.getLocalHashInfo(this.context, hash); } - async setUuid(uuid: string): Promise { + async setUuid(uuid: string): Promise { logger.debug(TAG, ',call setUuid'); - return UpdateModuleImpl.setUuid(this.context,uuid); + return UpdateModuleImpl.setUuid(this.context, uuid); } async reloadUpdate(options: { hash: string }): Promise { @@ -95,31 +106,45 @@ getConstants(): Object { return UpdateModuleImpl.markSuccess(this.context); } - async downloadPatchFromPpk(options: { updateUrl: string; hash: string; originHash: string }): Promise { + async downloadPatchFromPpk(options: { + updateUrl: string; + hash: string; + originHash: string; + }): Promise { logger.debug(TAG, ',call downloadPatchFromPpk'); return UpdateModuleImpl.downloadPatchFromPpk(this.context, options); } - async downloadPatchFromPackage(options: { updateUrl: string; hash: string }): Promise { + async downloadPatchFromPackage(options: { + updateUrl: string; + hash: string; + }): Promise { logger.debug(TAG, ',call downloadPatchFromPackage'); return UpdateModuleImpl.downloadPatchFromPackage(this.context, options); } - async downloadFullUpdate(options: { updateUrl: string; hash: string }): Promise { + async downloadFullUpdate(options: { + updateUrl: string; + hash: string; + }): Promise { logger.debug(TAG, ',call downloadFullUpdate'); return UpdateModuleImpl.downloadFullUpdate(this.context, options); } - async downloadAndInstallApk(options: { url: string; target: string; hash: string }): Promise { + async downloadAndInstallApk(options: { + url: string; + target: string; + hash: string; + }): Promise { logger.debug(TAG, ',call downloadAndInstallApk'); return UpdateModuleImpl.downloadAndInstallApk(this.mUiCtx, options); } - addListener(eventName: string): void { + addListener(_eventName: string): void { logger.debug(TAG, ',call addListener'); } - removeListeners(count: number): void { + removeListeners(_count: number): void { logger.debug(TAG, ',call removeListeners'); } } diff --git a/harmony/pushy/src/main/ets/UpdateContext.ts b/harmony/pushy/src/main/ets/UpdateContext.ts index ed5a190..3485703 100644 --- a/harmony/pushy/src/main/ets/UpdateContext.ts +++ b/harmony/pushy/src/main/ets/UpdateContext.ts @@ -192,11 +192,11 @@ export class UpdateContext { public static getBundleUrl( context: common.UIAbilityContext, defaultAssetsUrl?: string, - ): string { + ) { return new UpdateContext(context).getBundleUrl(defaultAssetsUrl); } - public getBundleUrl(defaultAssetsUrl?: string): string { + public getBundleUrl(defaultAssetsUrl?: string) { UpdateContext.isUsingBundleUrl = true; const currentVersion = this.getCurrentVersion(); if (!currentVersion) {