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 changelog: { 54 version: component.displayVersion, 55 size: FormatUtils.formatFileSize(component.size), 56 displayType: ChangelogType.PICTURE_AND_TEXT, 57 content: description 58 }, 59 countDownDialogInfo: { 60 dialogText: isABInstall ? 61 $r('app.string.count_down_message_ab', component.displayVersion, countDownTimes) : 62 $r('app.string.count_down_message_recovery', component.displayVersion, countDownTimes), 63 dialogType: isABInstall ? CountDownDialogType.OTA_AB : CountDownDialogType.OTA 64 } 65 }; 66 } 67 68 /** 69 * 取当前版本数据 70 * 71 * @param versionComponents 升级包 72 * @param componentDescriptions 更新日志 73 * @return VersionPageInfo 具体的当前版本数据 74 */ 75 public getCurrentVersionPageInfo(versionComponents: Array<update.VersionComponent>, 76 componentDescriptions: Array<update.ComponentDescription>): VersionPageInfo { 77 let component: update.VersionComponent = versionComponents.filter((component: update.VersionComponent) => { 78 return component.componentType == update.ComponentType.OTA; 79 })?.[0]; 80 let componentId: string = component?.componentId; 81 let description: string = ''; 82 if (componentDescriptions) { 83 description = UpdateUtils.obtainDescription(componentDescriptions, componentId); 84 } 85 return { 86 version: VersionUtils.getCurrentDisplayVersion(), 87 changelog: { 88 version: VersionUtils.getCurrentDisplayVersion(), 89 content: description 90 } 91 }; 92 } 93}