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