OSDN Git Service

b96eb694897b9e62a277a5cb0c1d63fa9b0e1b4a
[android-x86/frameworks-base.git] / services / core / java / com / android / server / storage / FileCollector.java
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE2.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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16
17 package com.android.server.storage;
18
19 import android.annotation.IntDef;
20 import android.os.storage.StorageManager;
21 import android.util.ArrayMap;
22
23 import java.io.File;
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.Map;
27
28 /**
29  * FileCollector walks over a directory and categorizes storage usage by their type.
30  */
31 public class FileCollector {
32     private static final int UNRECOGNIZED = -1;
33     private static final int IMAGES = 0;
34     private static final int VIDEO = 1;
35     private static final int AUDIO = 2;
36     @Retention(RetentionPolicy.SOURCE)
37     @IntDef({
38             UNRECOGNIZED,
39             IMAGES,
40             VIDEO,
41             AUDIO })
42     private @interface FileTypes {}
43
44
45     private static final Map<String, Integer> EXTENSION_MAP = new ArrayMap<String, Integer>();
46     static {
47         // Audio
48         EXTENSION_MAP.put("aac", AUDIO);
49         EXTENSION_MAP.put("amr", AUDIO);
50         EXTENSION_MAP.put("awb", AUDIO);
51         EXTENSION_MAP.put("snd", AUDIO);
52         EXTENSION_MAP.put("flac", AUDIO);
53         EXTENSION_MAP.put("mp3", AUDIO);
54         EXTENSION_MAP.put("mpga", AUDIO);
55         EXTENSION_MAP.put("mpega", AUDIO);
56         EXTENSION_MAP.put("mp2", AUDIO);
57         EXTENSION_MAP.put("m4a", AUDIO);
58         EXTENSION_MAP.put("aif", AUDIO);
59         EXTENSION_MAP.put("aiff", AUDIO);
60         EXTENSION_MAP.put("aifc", AUDIO);
61         EXTENSION_MAP.put("gsm", AUDIO);
62         EXTENSION_MAP.put("mka", AUDIO);
63         EXTENSION_MAP.put("m3u", AUDIO);
64         EXTENSION_MAP.put("wma", AUDIO);
65         EXTENSION_MAP.put("wax", AUDIO);
66         EXTENSION_MAP.put("ra", AUDIO);
67         EXTENSION_MAP.put("rm", AUDIO);
68         EXTENSION_MAP.put("ram", AUDIO);
69         EXTENSION_MAP.put("pls", AUDIO);
70         EXTENSION_MAP.put("sd2", AUDIO);
71         EXTENSION_MAP.put("wav", AUDIO);
72         EXTENSION_MAP.put("ogg", AUDIO);
73         EXTENSION_MAP.put("oga", AUDIO);
74         // Video
75         EXTENSION_MAP.put("3gpp", VIDEO);
76         EXTENSION_MAP.put("3gp", VIDEO);
77         EXTENSION_MAP.put("3gpp2", VIDEO);
78         EXTENSION_MAP.put("3g2", VIDEO);
79         EXTENSION_MAP.put("avi", VIDEO);
80         EXTENSION_MAP.put("dl", VIDEO);
81         EXTENSION_MAP.put("dif", VIDEO);
82         EXTENSION_MAP.put("dv", VIDEO);
83         EXTENSION_MAP.put("fli", VIDEO);
84         EXTENSION_MAP.put("m4v", VIDEO);
85         EXTENSION_MAP.put("ts", VIDEO);
86         EXTENSION_MAP.put("mpeg", VIDEO);
87         EXTENSION_MAP.put("mpg", VIDEO);
88         EXTENSION_MAP.put("mpe", VIDEO);
89         EXTENSION_MAP.put("mp4", VIDEO);
90         EXTENSION_MAP.put("vob", VIDEO);
91         EXTENSION_MAP.put("qt", VIDEO);
92         EXTENSION_MAP.put("mov", VIDEO);
93         EXTENSION_MAP.put("mxu", VIDEO);
94         EXTENSION_MAP.put("webm", VIDEO);
95         EXTENSION_MAP.put("lsf", VIDEO);
96         EXTENSION_MAP.put("lsx", VIDEO);
97         EXTENSION_MAP.put("mkv", VIDEO);
98         EXTENSION_MAP.put("mng", VIDEO);
99         EXTENSION_MAP.put("asf", VIDEO);
100         EXTENSION_MAP.put("asx", VIDEO);
101         EXTENSION_MAP.put("wm", VIDEO);
102         EXTENSION_MAP.put("wmv", VIDEO);
103         EXTENSION_MAP.put("wmx", VIDEO);
104         EXTENSION_MAP.put("wvx", VIDEO);
105         EXTENSION_MAP.put("movie", VIDEO);
106         EXTENSION_MAP.put("wrf", VIDEO);
107         // Images
108         EXTENSION_MAP.put("bmp", IMAGES);
109         EXTENSION_MAP.put("gif", IMAGES);
110         EXTENSION_MAP.put("jpg", IMAGES);
111         EXTENSION_MAP.put("jpeg", IMAGES);
112         EXTENSION_MAP.put("jpe", IMAGES);
113         EXTENSION_MAP.put("pcx", IMAGES);
114         EXTENSION_MAP.put("png", IMAGES);
115         EXTENSION_MAP.put("svg", IMAGES);
116         EXTENSION_MAP.put("svgz", IMAGES);
117         EXTENSION_MAP.put("tiff", IMAGES);
118         EXTENSION_MAP.put("tif", IMAGES);
119         EXTENSION_MAP.put("wbmp", IMAGES);
120         EXTENSION_MAP.put("webp", IMAGES);
121         EXTENSION_MAP.put("dng", IMAGES);
122         EXTENSION_MAP.put("cr2", IMAGES);
123         EXTENSION_MAP.put("ras", IMAGES);
124         EXTENSION_MAP.put("art", IMAGES);
125         EXTENSION_MAP.put("jng", IMAGES);
126         EXTENSION_MAP.put("nef", IMAGES);
127         EXTENSION_MAP.put("nrw", IMAGES);
128         EXTENSION_MAP.put("orf", IMAGES);
129         EXTENSION_MAP.put("rw2", IMAGES);
130         EXTENSION_MAP.put("pef", IMAGES);
131         EXTENSION_MAP.put("psd", IMAGES);
132         EXTENSION_MAP.put("pnm", IMAGES);
133         EXTENSION_MAP.put("pbm", IMAGES);
134         EXTENSION_MAP.put("pgm", IMAGES);
135         EXTENSION_MAP.put("ppm", IMAGES);
136         EXTENSION_MAP.put("srw", IMAGES);
137         EXTENSION_MAP.put("arw", IMAGES);
138         EXTENSION_MAP.put("rgb", IMAGES);
139         EXTENSION_MAP.put("xbm", IMAGES);
140         EXTENSION_MAP.put("xpm", IMAGES);
141         EXTENSION_MAP.put("xwd", IMAGES);
142     }
143
144     /**
145      * Returns the file categorization measurement result.
146      * @param path Directory to collect and categorize storage in.
147      */
148     public static MeasurementResult getMeasurementResult(File path) {
149         return collectFiles(StorageManager.maybeTranslateEmulatedPathToInternal(path),
150                 new MeasurementResult());
151     }
152
153     private static MeasurementResult collectFiles(File file, MeasurementResult result) {
154         File[] files = file.listFiles();
155
156         if (files == null) {
157             return result;
158         }
159
160         for (File f : files) {
161             if (f.isDirectory()) {
162                 try {
163                     collectFiles(f, result);
164                 } catch (StackOverflowError e) {
165                     return result;
166                 }
167             } else {
168                 handleFile(result, f);
169             }
170         }
171
172         return result;
173     }
174
175     private static void handleFile(MeasurementResult result, File f) {
176         long fileSize = f.length();
177         int fileType = EXTENSION_MAP.getOrDefault(getExtensionForFile(f), UNRECOGNIZED);
178         switch (fileType) {
179             case AUDIO:
180                 result.audioSize += fileSize;
181                 break;
182             case VIDEO:
183                 result.videosSize += fileSize;
184                 break;
185             case IMAGES:
186                 result.imagesSize += fileSize;
187                 break;
188             default:
189                 result.miscSize += fileSize;
190         }
191     }
192
193     private static String getExtensionForFile(File file) {
194         String fileName = file.getName();
195         int index = fileName.lastIndexOf('.');
196         if (index == -1) {
197             return "";
198         }
199         return fileName.substring(index + 1).toLowerCase();
200     }
201
202     /**
203      * MeasurementResult contains a storage categorization result.
204      */
205     public static class MeasurementResult {
206         public long imagesSize;
207         public long videosSize;
208         public long miscSize;
209         public long audioSize;
210
211         /**
212          * Sums up the storage taken by all of the categorizable sizes in the measurement.
213          */
214         public long totalAccountedSize() {
215             return imagesSize + videosSize + miscSize + audioSize;
216         }
217     }
218 }