Debian Codegraph @

sdk-common/src/main/java/com/android/ide/common/resources/ResourceFolder.java

raw
1 /*
2 * Copyright (C) 2007 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 com.android.ide.common.resources;
18
19 import com.android.SdkConstants;
20 import com.android.annotations.VisibleForTesting;
21 import com.android.annotations.VisibleForTesting.Visibility;
22 import com.android.ide.common.resources.configuration.Configurable;
23 import com.android.ide.common.resources.configuration.FolderConfiguration;
24 import com.android.io.IAbstractFile;
25 import com.android.io.IAbstractFolder;
26 import com.android.resources.FolderTypeRelationship;
27 import com.android.resources.ResourceFolderType;
28 import com.android.resources.ResourceType;
29 import com.android.utils.SdkUtils;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 /**
38 * Resource Folder class. Contains list of {@link ResourceFile}s,
39 * the {@link FolderConfiguration}, and a link to the {@link IAbstractFolder} object.
40 */
41 public final class ResourceFolder implements Configurable {
42 final ResourceFolderType mType;
43 final FolderConfiguration mConfiguration;
44 IAbstractFolder mFolder;
45 List<ResourceFile> mFiles = null;
46 Map<String, ResourceFile> mNames = null;
47 private final ResourceRepository mRepository;
48
49 /**
50 * Creates a new {@link ResourceFolder}
51 * @param type The type of the folder
52 * @param config The configuration of the folder
53 * @param folder The associated {@link IAbstractFolder} object.
54 * @param repository The associated {@link ResourceRepository}
55 */
56 protected ResourceFolder(ResourceFolderType type, FolderConfiguration config,
57 IAbstractFolder folder, ResourceRepository repository) {
58 mType = type;
59 mConfiguration = config;
60 mFolder = folder;
61 mRepository = repository;
62 }
63
64 /**
65 * Processes a file and adds it to its parent folder resource.
66 *
67 * @param file the underlying resource file.
68 * @param kind the file change kind.
69 * @param context a context object with state for the current update, such
70 * as a place to stash errors encountered
71 * @return the {@link ResourceFile} that was created.
72 */
73 public ResourceFile processFile(IAbstractFile file, ResourceDeltaKind kind,
74 ScanningContext context) {
75 // look for this file if it's already been created
76 ResourceFile resFile = getFile(file, context);
77
78 if (resFile == null) {
79 if (kind != ResourceDeltaKind.REMOVED) {
80 // create a ResourceFile for it.
81
82 resFile = createResourceFile(file);
83 resFile.load(context);
84
85 // add it to the folder
86 addFile(resFile);
87 }
88 } else {
89 if (kind == ResourceDeltaKind.REMOVED) {
90 removeFile(resFile, context);
91 } else {
92 resFile.update(context);
93 }
94 }
95
96 return resFile;
97 }
98
99 private ResourceFile createResourceFile(IAbstractFile file) {
100 // check if that's a single or multi resource type folder. We have a special case
101 // for ID generating resource types (layout/menu, and XML drawables, etc.).
102 // MultiResourceFile handles the case when several resource types come from a single file
103 // (values files).
104
105 ResourceFile resFile;
106 if (mType != ResourceFolderType.VALUES) {
107 if (FolderTypeRelationship.isIdGeneratingFolderType(mType) &&
108 SdkUtils.endsWithIgnoreCase(file.getName(), SdkConstants.DOT_XML)) {
109 List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(mType);
110 ResourceType primaryType = types.get(0);
111 resFile = new IdGeneratingResourceFile(file, this, primaryType);
112 } else {
113 resFile = new SingleResourceFile(file, this);
114 }
115 } else {
116 resFile = new MultiResourceFile(file, this);
117 }
118 return resFile;
119 }
120
121 /**
122 * Adds a {@link ResourceFile} to the folder.
123 * @param file The {@link ResourceFile}.
124 */
125 @VisibleForTesting(visibility=Visibility.PROTECTED)
126 public void addFile(ResourceFile file) {
127 if (mFiles == null) {
128 int initialSize = 16;
129 if (mRepository.isFrameworkRepository()) {
130 String name = mFolder.getName();
131 // Pick some reasonable initial sizes for framework data structures
132 // since they are typically (a) large and (b) their sizes are roughly known
133 // in advance
134 switch (mType) {
135 case DRAWABLE: {
136 // See if it's one of the -mdpi, -hdpi etc folders which
137 // are large (~1250 items)
138 int index = name.indexOf('-');
139 if (index == -1) {
140 initialSize = 230; // "drawable" folder
141 } else {
142 index = name.indexOf('-', index + 1);
143 if (index == -1) {
144 // One of the "drawable-<density>" folders
145 initialSize = 1260;
146 } else {
147 // "drawable-sw600dp-hdpi" etc
148 initialSize = 30;
149 }
150 }
151 break;
152 }
153 case LAYOUT: {
154 // The main layout folder has about ~185 layouts in it;
155 // the others are small
156 if (name.indexOf('-') == -1) {
157 initialSize = 200;
158 }
159 break;
160 }
161 case VALUES: {
162 if (name.indexOf('-') == -1) {
163 initialSize = 32;
164 } else {
165 initialSize = 4;
166 }
167 break;
168 }
169 case ANIM: initialSize = 85; break;
170 case COLOR: initialSize = 32; break;
171 case RAW: initialSize = 4; break;
172 default:
173 // Stick with the 16 default
174 break;
175 }
176 }
177
178 mFiles = new ArrayList<ResourceFile>(initialSize);
179 mNames = new HashMap<String, ResourceFile>(initialSize, 2.0f);
180 }
181
182 mFiles.add(file);
183 mNames.put(file.getFile().getName(), file);
184 }
185
186 protected void removeFile(ResourceFile file, ScanningContext context) {
187 file.dispose(context);
188 mFiles.remove(file);
189 mNames.remove(file.getFile().getName());
190 }
191
192 protected void dispose(ScanningContext context) {
193 if (mFiles != null) {
194 for (ResourceFile file : mFiles) {
195 file.dispose(context);
196 }
197
198 mFiles.clear();
199 mNames.clear();
200 }
201 }
202
203 /**
204 * Returns the {@link IAbstractFolder} associated with this object.
205 */
206 public IAbstractFolder getFolder() {
207 return mFolder;
208 }
209
210 /**
211 * Returns the {@link ResourceFolderType} of this object.
212 */
213 public ResourceFolderType getType() {
214 return mType;
215 }
216
217 public ResourceRepository getRepository() {
218 return mRepository;
219 }
220
221 /**
222 * Returns the list of {@link ResourceType}s generated by the files inside this folder.
223 */
224 public Collection<ResourceType> getResourceTypes() {
225 ArrayList<ResourceType> list = new ArrayList<ResourceType>();
226
227 if (mFiles != null) {
228 for (ResourceFile file : mFiles) {
229 Collection<ResourceType> types = file.getResourceTypes();
230
231 // loop through those and add them to the main list,
232 // if they are not already present
233 for (ResourceType resType : types) {
234 if (list.indexOf(resType) == -1) {
235 list.add(resType);
236 }
237 }
238 }
239 }
240
241 return list;
242 }
243
244 @Override
245 public FolderConfiguration getConfiguration() {
246 return mConfiguration;
247 }
248
249 /**
250 * Returns whether the folder contains a file with the given name.
251 * @param name the name of the file.
252 */
253 public boolean hasFile(String name) {
254 if (mNames != null && mNames.containsKey(name)) {
255 return true;
256 }
257
258 // Note: mNames.containsKey(name) is faster, but doesn't give the same result; this
259 // method seems to be called on this ResourceFolder before it has been processed,
260 // so we need to use the file system check instead:
261 return mFolder.hasFile(name);
262 }
263
264 /**
265 * Returns the {@link ResourceFile} matching a {@link IAbstractFile} object.
266 *
267 * @param file The {@link IAbstractFile} object.
268 * @param context a context object with state for the current update, such
269 * as a place to stash errors encountered
270 * @return the {@link ResourceFile} or null if no match was found.
271 */
272 private ResourceFile getFile(IAbstractFile file, ScanningContext context) {
273 assert mFolder.equals(file.getParentFolder());
274
275 if (mNames != null) {
276 ResourceFile resFile = mNames.get(file.getName());
277 if (resFile != null) {
278 return resFile;
279 }
280 }
281
282 // If the file actually exists, the resource folder may not have been
283 // scanned yet; add it lazily
284 if (file.exists()) {
285 ResourceFile resFile = createResourceFile(file);
286 resFile.load(context);
287 addFile(resFile);
288 return resFile;
289 }
290
291 return null;
292 }
293
294 /**
295 * Returns the {@link ResourceFile} matching a given name.
296 * @param filename The name of the file to return.
297 * @return the {@link ResourceFile} or <code>null</code> if no match was found.
298 */
299 public ResourceFile getFile(String filename) {
300 if (mNames != null) {
301 ResourceFile resFile = mNames.get(filename);
302 if (resFile != null) {
303 return resFile;
304 }
305 }
306
307 // If the file actually exists, the resource folder may not have been
308 // scanned yet; add it lazily
309 IAbstractFile file = mFolder.getFile(filename);
310 if (file != null && file.exists()) {
311 ResourceFile resFile = createResourceFile(file);
312 resFile.load(new ScanningContext(mRepository));
313 addFile(resFile);
314 return resFile;
315 }
316
317 return null;
318 }
319
320 /**
321 * Returns whether a file in the folder is generating a resource of a specified type.
322 * @param type The {@link ResourceType} being looked up.
323 */
324 public boolean hasResources(ResourceType type) {
325 // Check if the folder type is able to generate resource of the type that was asked.
326 // this is a first check to avoid going through the files.
327 List<ResourceFolderType> folderTypes = FolderTypeRelationship.getRelatedFolders(type);
328
329 boolean valid = false;
330 for (ResourceFolderType rft : folderTypes) {
331 if (rft == mType) {
332 valid = true;
333 break;
334 }
335 }
336
337 if (valid) {
338 if (mFiles != null) {
339 for (ResourceFile f : mFiles) {
340 if (f.hasResources(type)) {
341 return true;
342 }
343 }
344 }
345 }
346 return false;
347 }
348
349 @Override
350 public String toString() {
351 return mFolder.toString();
352 }
353 }