• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.sdksandbox;
18 
19 import java.io.File;
20 import java.util.List;
21 import java.util.Objects;
22 
23 /**
24  * Utility class for performing file related operations
25  *
26  * @hide
27  */
28 public class FileUtil {
29 
30     public static final String TAG = "SdkSandboxManager";
31     public static final int CONVERSION_FACTOR_FROM_BYTES_TO_KB = 1024;
32 
33     /** Calculate the storage of SDK iteratively */
getStorageInKbForPaths(List<String> paths)34     public static int getStorageInKbForPaths(List<String> paths) {
35         float storageSize = 0;
36         for (int i = 0; i < paths.size(); i++) {
37             final File dir = new File(paths.get(i));
38 
39             if (Objects.nonNull(dir)) {
40                 storageSize += getStorageForFiles(dir.listFiles());
41             }
42         }
43         return convertByteToKb(storageSize);
44     }
45 
getStorageForFiles(File[] files)46     private static float getStorageForFiles(File[] files) {
47         if (Objects.isNull(files)) {
48             return 0;
49         }
50 
51         float sizeInBytes = 0;
52 
53         for (File file : files) {
54             if (file.isDirectory()) {
55                 sizeInBytes += getStorageForFiles(file.listFiles());
56             } else {
57                 sizeInBytes += file.length();
58             }
59         }
60         return sizeInBytes;
61     }
62 
convertByteToKb(float storageSize)63     private static int convertByteToKb(float storageSize) {
64         return (int) (storageSize / CONVERSION_FACTOR_FROM_BYTES_TO_KB);
65     }
66 }
67