1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import update from '@ohos.update'; 17import type { UpgradeData } from '@ohos/common/src/main/ets/const/update_const'; 18import { UpgradeCallResult } from '@ohos/common/src/main/ets/const/update_const'; 19import { DeviceUtils } from '@ohos/common/src/main/ets/util/DeviceUtils'; 20import { LogUtils } from '@ohos/common/src/main/ets/util/LogUtils'; 21import { UpdateUtils } from '@ohos/common/src/main/ets/util/UpdateUtils'; 22import { OtaUpdateManager } from '../manager/OtaUpdateManager'; 23import { UpgradeAdapter } from '../UpgradeAdapter'; 24 25const TAG = 'VersionUtils'; 26 27/** 28 * 版本工具 29 * 30 * @since 2022-06-06 31 */ 32export namespace VersionUtils { 33 /** 34 * 计算升级包大小 35 * 36 * @param newVersionInfo 新版本信息 37 * @return 升级包大小 38 */ 39 export function calculatePackageSize(newVersion: update.NewVersionInfo): number { 40 let totalSize: number = 0; 41 for (let index = 0; index < newVersion?.versionComponents?.length; index++) { 42 totalSize += Number(newVersion?.versionComponents?.[index]?.size); 43 } 44 LogUtils.info(TAG, 'calculatePackageSize, totalSize: ' + totalSize); 45 return totalSize; 46 } 47 48 /** 49 * 获取新版本名称 50 * 51 * @param info 新版本信息 52 * @return 新版本名称 53 */ 54 export async function obtainNewVersionName(info: update.NewVersionInfo | update.TaskBody): Promise<string> { 55 let component: update.VersionComponent = sortComponents(info?.versionComponents)?.[0]; 56 return component?.displayVersion ?? ''; 57 } 58 59 /** 60 * 获取当前版本号 61 * 62 * @return 当前版本号 63 */ 64 export function getCurrentDisplayVersion(): string { 65 return DeviceUtils.getDisplayVersion(); 66 } 67 68 /** 69 * 获取主页的显示版本号 70 * 71 * @return 显示版本号 72 */ 73 export function getDisplayVersionForIndex(): string { 74 return DeviceUtils.getDisplayVersion(); 75 } 76 77 /** 78 * 排序 79 * 80 * @param components 升级包集合 81 * @return 升级包集合 82 */ 83 export function sortComponents(components: Array<update.VersionComponent>): Array<update.VersionComponent> { 84 if (components) { 85 return components.sort((component: update.VersionComponent, nextComponent: update.VersionComponent) => { 86 return component.componentType - nextComponent.componentType; 87 }); 88 } 89 return null; 90 } 91 92 export async function getNewVersionDigest(): Promise<string> { 93 let newVersionInfo: update.NewVersionInfo = globalThis.cachedNewVersionInfo || 94 await OtaUpdateManager.getInstance().getNewVersion().then((upgradeData: UpgradeData<update.NewVersionInfo>) => { 95 return upgradeData.callResult === UpgradeCallResult.OK ? upgradeData.data : null; 96 }); 97 98 if (newVersionInfo) { 99 LogUtils.info(TAG, 'getNewVersionDigest, versionDigestInfo: ' + newVersionInfo.versionDigestInfo.versionDigest); 100 return newVersionInfo.versionDigestInfo.versionDigest; 101 } 102 103 return ''; 104 } 105 106 /** 107 * 是否在新版本界面 108 * 109 * @return 是否在新版本界面 110 */ 111 export function isInNewVersionPage(): boolean { 112 return globalThis.AbilityStatus === 'ON_FOREGROUND' && globalThis.currentPage === 'pages/newVersion'; 113 } 114 115 /** 116 * 是否是AB升级 117 * 118 * @return 是否是AB升级 119 */ 120 export async function isABInstall(): Promise<boolean> { 121 let newVersionInfo = globalThis.cachedNewVersionInfo || await OtaUpdateManager.getInstance().getNewVersion() 122 .then(upgradeData => { 123 this.log(`isABInstall upgradeData: ${upgradeData}`); 124 return upgradeData.callResult == UpgradeCallResult.OK ? upgradeData.data : null; 125 }); 126 let components: update.VersionComponent[] = VersionUtils.sortComponents(newVersionInfo?.versionComponents); 127 let component: update.VersionComponent = components?.filter((component: update.VersionComponent) => { 128 return component.componentType == update.ComponentType.OTA; 129 })?.[0]; 130 return component?.effectiveMode === update.EffectiveMode.LIVE_AND_COLD; 131 } 132} 133 134export default VersionUtils;