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 if (mFileInfo == null) { 24 return 0; 25 } 26 return mFileInfo.size(); 27 } 28 getFileName(int idx)29 public String8 getFileName(int idx) { 30 return mFileInfo.itemAt(idx).getFileName(); 31 } 32 33 // const String8& getSourceName(int idx) { 34 // return mFileInfo->itemAt(idx).getSourceName(); 35 // } 36 37 /* 38 * Get the type of a file (usually regular or directory). 39 */ 40 // FileType getFileType(int idx) { 41 // return mFileInfo->itemAt(idx).getFileType(); 42 // } 43 44 /** 45 * This holds information about files in the asset hierarchy. 46 */ 47 static class FileInfo implements Comparable<FileInfo> { 48 private String8 mFileName; // filename only 49 private FileType mFileType; // regular, directory, etc 50 private String8 mSourceName; // currently debug-only 51 FileInfo()52 FileInfo() {} 53 FileInfo(String8 path)54 FileInfo(String8 path) { // useful for e.g. svect.indexOf 55 mFileName = path; 56 mFileType = FileType.kFileTypeUnknown; 57 } 58 FileInfo(FileInfo src)59 FileInfo(FileInfo src) { 60 copyMembers(src); 61 } 62 // const FileInfo& operator= (const FileInfo& src) { 63 // if (this != &src) 64 // copyMembers(src); 65 // return *this; 66 // } 67 copyMembers(final FileInfo src)68 void copyMembers(final FileInfo src) { 69 mFileName = src.mFileName; 70 mFileType = src.mFileType; 71 mSourceName = src.mSourceName; 72 } 73 74 /* need this for SortedVector; must compare only on file name */ 75 // bool operator< (const FileInfo& rhs) const { 76 // return mFileName < rhs.mFileName; 77 // } 78 // 79 // /* used by AssetManager */ 80 // bool operator== (const FileInfo& rhs) const { 81 // return mFileName == rhs.mFileName; 82 // } 83 set(final String8 path, FileType type)84 void set(final String8 path, FileType type) { 85 mFileName = path; 86 mFileType = type; 87 } 88 getFileName()89 String8 getFileName() { return mFileName; } setFileName(String8 path)90 void setFileName(String8 path) { mFileName = path; } 91 getFileType()92 FileType getFileType() { return mFileType; } setFileType(FileType type)93 void setFileType(FileType type) { mFileType = type; } 94 getSourceName()95 String8 getSourceName() { return mSourceName; } setSourceName(String8 path)96 void setSourceName(String8 path) { mSourceName = path; } 97 isLessThan(FileInfo fileInfo)98 public boolean isLessThan(FileInfo fileInfo) { 99 return mFileName.string().compareTo(fileInfo.mFileName.string()) < 0; 100 } 101 102 @Override compareTo(FileInfo other)103 public int compareTo(FileInfo other) { 104 return mFileName.string().compareTo(other.mFileName.string()); 105 } 106 107 /* 108 * Handy utility for finding an entry in a sorted vector of FileInfo. 109 * Returns the index of the matching entry, or -1 if none found. 110 */ findEntry(SortedVector<FileInfo> pVector, String8 fileName)111 static int findEntry(SortedVector<FileInfo> pVector, 112 String8 fileName) { 113 FileInfo tmpInfo = new FileInfo(); 114 115 tmpInfo.setFileName(fileName); 116 return pVector.indexOf(tmpInfo); 117 } 118 119 120 }; 121 122 /* AssetManager uses this to initialize us */ setFileList(SortedVector<FileInfo> list)123 void setFileList(SortedVector<FileInfo> list) { mFileInfo = list; } 124 125 } 126