1 package org.robolectric.res;
2 
3 import org.robolectric.util.Logger;
4 
5 /**
6  * DrawableResourceLoader
7  */
8 public class DrawableResourceLoader {
9   private final PackageResourceTable resourceTable;
10 
DrawableResourceLoader(PackageResourceTable resourceTable)11   DrawableResourceLoader(PackageResourceTable resourceTable) {
12     this.resourceTable = resourceTable;
13   }
14 
15   /**
16    * Returns a collection of resource IDs for all nine-patch drawables in the project.
17    *
18    * @param resourcePath Resource path.
19    */
findDrawableResources(ResourcePath resourcePath)20   void findDrawableResources(ResourcePath resourcePath) {
21     FsFile[] files = resourcePath.getResourceBase().listFiles();
22     if (files != null) {
23       for (FsFile f : files) {
24         if (f.isDirectory() && f.getName().startsWith("drawable")) {
25           listDrawableResources(f, "drawable");
26         } else if (f.isDirectory() && f.getName().startsWith("mipmap")) {
27           listDrawableResources(f, "mipmap");
28         }
29       }
30     }
31   }
32 
listDrawableResources(FsFile dir, String type)33   private void listDrawableResources(FsFile dir, String type) {
34     FsFile[] files = dir.listFiles();
35     if (files != null) {
36       Qualifiers qualifiers = null;
37       try {
38         qualifiers = Qualifiers.fromParentDir(dir);
39       } catch (IllegalArgumentException e) {
40         Logger.warn(dir + ": " + e.getMessage());
41         return;
42       }
43 
44       for (FsFile f : files) {
45         String name = f.getName();
46         if (name.startsWith(".")) continue;
47 
48         String shortName;
49         boolean isNinePatch;
50         if (name.endsWith(".xml")) {
51           // already handled, do nothing...
52           continue;
53         } else if (name.endsWith(".9.png")) {
54           String[] tokens = name.split("\\.9\\.png$", -1);
55           shortName = tokens[0];
56           isNinePatch = true;
57         } else {
58           shortName = f.getBaseName();
59           isNinePatch = false;
60         }
61 
62         XmlContext fakeXmlContext = new XmlContext(resourceTable.getPackageName(), f, qualifiers);
63         resourceTable.addResource(type, shortName, new FileTypedResource.Image(f, isNinePatch, fakeXmlContext));
64       }
65     }
66   }
67 }
68