1/* 2 * Copyright (c) 2022-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 storageStatistics from '@ohos.file.storageStatistics' 17import volumeManager from '@ohos.file.volumeManager' 18import CheckEmptyUtils from '../utils/CheckEmptyUtils' 19import Logger from '../utils/Logger' 20import { VolumeData } from '../model/VolumeData' 21 22const TAG: string = 'QueryVolumeData' 23 24export class QueryVolumeData { 25 public volumeSizeList: VolumeData[] = [] 26 private volumeDataList: volumeManager.Volume[] = [] 27 28 private async initVolumeDataList() { 29 this.volumeDataList = [] 30 this.volumeDataList = await volumeManager.getAllVolumes() 31 Logger.info(TAG, `volumeDataList ${this.volumeDataList}`) 32 } 33 34 private async initVolumeSizeList() { 35 this.volumeSizeList = [] 36 for (let i = 0; i < this.volumeDataList.length; i++) { 37 let volumeData: volumeManager.Volume = this.volumeDataList[i] 38 this.volumeSizeList.push({ 39 totalSize: await storageStatistics.getTotalSizeOfVolume(volumeData.uuid), 40 freeSize: await storageStatistics.getFreeSizeOfVolume(volumeData.uuid), 41 description: volumeData.description 42 }) 43 } 44 Logger.info(TAG, `volumeSizeList ${this.volumeSizeList}`) 45 } 46 47 public async initVolumeData(callback: (hasInit: boolean) => void){ 48 try { 49 await this.initVolumeDataList() 50 await this.initVolumeSizeList() 51 } catch (error) { 52 Logger.error(TAG, `initVolumeData failed! error: ${error}`) 53 callback(false) 54 return 55 } 56 57 if (!CheckEmptyUtils.isEmptyArr(this.volumeDataList)) { 58 Logger.info(TAG, `VolumeDataList ${this.volumeDataList}`) 59 callback(true) 60 return 61 } 62 63 Logger.info(TAG, `No volume currently available`) 64 callback(false) 65 return 66 } 67}