• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.res.android;
2 
3 import org.robolectric.res.android.CppAssetManager.FileType;
4 
5 // transliterated from
6 // https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/libs/androidfw/AssetDir.cpp and
7 // https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/include/androidfw/AssetDir.h
8 public class AssetDir {
9 
10   private SortedVector<FileInfo> mFileInfo;
11 
AssetDir()12   AssetDir() {
13     mFileInfo = null;
14   }
15 
AssetDir(AssetDir src)16   AssetDir(AssetDir src) {}
17 
18   /*
19    * Vector-style access.
20    */
getFileCount()21   public int getFileCount() {
22     if (mFileInfo == null) {
23       return 0;
24     }
25     return mFileInfo.size();
26   }
27 
getFileName(int idx)28   public String8 getFileName(int idx) {
29     return mFileInfo.itemAt(idx).getFileName();
30   }
31 
32   //    const String8& getSourceName(int idx) {
33   //    return mFileInfo->itemAt(idx).getSourceName();
34   //  }
35 
36   /*
37    * Get the type of a file (usually regular or directory).
38    */
39   //  FileType getFileType(int idx) {
40   //    return mFileInfo->itemAt(idx).getFileType();
41   //  }
42 
43   /** This holds information about files in the asset hierarchy. */
44   static class FileInfo implements Comparable<FileInfo> {
45     private String8 mFileName; // filename only
46     private FileType mFileType; // regular, directory, etc
47     private String8 mSourceName; // currently debug-only
48 
FileInfo()49     FileInfo() {}
50 
FileInfo(String8 path)51     FileInfo(String8 path) { // useful for e.g. svect.indexOf
52       mFileName = path;
53       mFileType = FileType.kFileTypeUnknown;
54     }
55 
FileInfo(FileInfo src)56     FileInfo(FileInfo src) {
57       copyMembers(src);
58     }
59 
60     //        const FileInfo& operator= (const FileInfo& src) {
61     //      if (this != &src)
62     //        copyMembers(src);
63     //      return *this;
64     //    }
65 
copyMembers(final FileInfo src)66     void copyMembers(final FileInfo src) {
67       mFileName = src.mFileName;
68       mFileType = src.mFileType;
69       mSourceName = src.mSourceName;
70     }
71 
72     /* need this for SortedVector; must compare only on file name */
73     //    bool operator< (const FileInfo& rhs) const {
74     //      return mFileName < rhs.mFileName;
75     //    }
76     //
77     //    /* used by AssetManager */
78     //    bool operator== (const FileInfo& rhs) const {
79     //      return mFileName == rhs.mFileName;
80     //    }
81 
set(final String8 path, FileType type)82     void set(final String8 path, FileType type) {
83       mFileName = path;
84       mFileType = type;
85     }
86 
getFileName()87     String8 getFileName() {
88       return mFileName;
89     }
90 
setFileName(String8 path)91     void setFileName(String8 path) {
92       mFileName = path;
93     }
94 
getFileType()95     FileType getFileType() {
96       return mFileType;
97     }
98 
setFileType(FileType type)99     void setFileType(FileType type) {
100       mFileType = type;
101     }
102 
getSourceName()103     String8 getSourceName() {
104       return mSourceName;
105     }
106 
setSourceName(String8 path)107     void setSourceName(String8 path) {
108       mSourceName = path;
109     }
110 
isLessThan(FileInfo fileInfo)111     public boolean isLessThan(FileInfo fileInfo) {
112       return mFileName.string().compareTo(fileInfo.mFileName.string()) < 0;
113     }
114 
115     @Override
compareTo(FileInfo other)116     public int compareTo(FileInfo other) {
117       return mFileName.string().compareTo(other.mFileName.string());
118     }
119 
120     /*
121      * Handy utility for finding an entry in a sorted vector of FileInfo.
122      * Returns the index of the matching entry, or -1 if none found.
123      */
findEntry(SortedVector<FileInfo> pVector, String8 fileName)124     static int findEntry(SortedVector<FileInfo> pVector, String8 fileName) {
125       FileInfo tmpInfo = new FileInfo();
126 
127       tmpInfo.setFileName(fileName);
128       return pVector.indexOf(tmpInfo);
129     }
130   }
131   ;
132 
133   /* AssetManager uses this to initialize us */
setFileList(SortedVector<FileInfo> list)134   void setFileList(SortedVector<FileInfo> list) {
135     mFileInfo = list;
136   }
137 }
138