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 { ChangelogType, CountDownDialogType } from '@ohos/common/src/main/ets/const/update_const'; 18import { IPage, VersionPageInfo } from '@ohos/common/src/main/ets/manager/UpgradeInterface'; 19import { UpdateUtils } from '@ohos/common/src/main/ets/util/UpdateUtils'; 20import { FormatUtils } from '@ohos/common/src/main/ets/util/FormatUtils'; 21import VersionUtils from './util/VersionUtils'; 22 23/** 24 * ota的ux显示数据 25 * 26 * @since 2022-12-01 27 */ 28export class OtaPage implements IPage { 29 /** 30 * 取新版本数据 31 * 32 * @param versionComponents 升级包 33 * @param componentDescriptions 更新日志 34 * @return Promise<VersionPageInfo> 具体的新版本数据 35 */ 36 public async getNewVersionPageInfo(versionComponents: Array<update.VersionComponent>, 37 componentDescriptions?: Array<update.ComponentDescription>): Promise<VersionPageInfo> { 38 let component: update.VersionComponent = versionComponents.filter((component: update.VersionComponent) => { 39 return component.componentType == update.ComponentType.OTA; 40 })?.[0]; 41 let componentId: string = component?.componentId; 42 let description: string = ''; 43 if (componentDescriptions) { 44 description = UpdateUtils.obtainDescription(componentDescriptions, componentId); 45 } 46 let isABInstall = await VersionUtils.isABInstall(); 47 const countDownTimes = 20; 48 49 return { 50 version: component.displayVersion, 51 size: component.size, 52 effectiveMode: component.effectiveMode, 53 otaMode:component.otaMode, 54 changelog: { 55 version: component.displayVersion, 56 size: FormatUtils.formatFileSize(component.size), 57 displayType: ChangelogType.PICTURE_AND_TEXT, 58 content: description 59 }, 60 countDownDialogInfo: { 61 dialogText: isABInstall ? 62 $r('app.string.count_down_message_ab', component.displayVersion, countDownTimes) : 63 $r('app.string.count_down_message_recovery', component.displayVersion, countDownTimes), 64 dialogType: isABInstall ? CountDownDialogType.OTA_AB : CountDownDialogType.OTA 65 } 66 }; 67 } 68 69 /** 70 * 取当前版本数据 71 * 72 * @param versionComponents 升级包 73 * @param componentDescriptions 更新日志 74 * @return VersionPageInfo 具体的当前版本数据 75 */ 76 public getCurrentVersionPageInfo(versionComponents: Array<update.VersionComponent>, 77 componentDescriptions: Array<update.ComponentDescription>): VersionPageInfo { 78 let component: update.VersionComponent = versionComponents.filter((component: update.VersionComponent) => { 79 return component.componentType == update.ComponentType.OTA; 80 })?.[0]; 81 let componentId: string = component?.componentId; 82 let description: string = ''; 83 if (componentDescriptions) { 84 description = UpdateUtils.obtainDescription(componentDescriptions, componentId); 85 } 86 return { 87 version: VersionUtils.getCurrentDisplayVersion(), 88 changelog: { 89 version: VersionUtils.getCurrentDisplayVersion(), 90 content: description 91 } 92 }; 93 } 94}