aboutsummaryrefslogtreecommitdiff
path: root/resources/src/main/java/org/robolectric/res/android/AssetDir.java
blob: b5a6901721418587a4f7969134bb935f9beadea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.robolectric.res.android;

import org.robolectric.res.android.CppAssetManager.FileType;

// transliterated from https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/libs/androidfw/AssetDir.cpp and
// https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/include/androidfw/AssetDir.h
public class AssetDir {

  private SortedVector<FileInfo> mFileInfo;

  AssetDir() {
    mFileInfo = null;
  }

  AssetDir(AssetDir src) {

  }

  /*
 * Vector-style access.
 */
  public int getFileCount() {
    return mFileInfo.size();
  }

  public String8 getFileName(int idx) {
    return mFileInfo.itemAt(idx).getFileName();
  }

//    const String8& getSourceName(int idx) {
//    return mFileInfo->itemAt(idx).getSourceName();
//  }

  /*
   * Get the type of a file (usually regular or directory).
   */
//  FileType getFileType(int idx) {
//    return mFileInfo->itemAt(idx).getFileType();
//  }

  /**
   * This holds information about files in the asset hierarchy.
   */
  static class FileInfo implements Comparable<FileInfo> {
    private String8    mFileName;    // filename only
    private FileType mFileType;      // regular, directory, etc
    private String8    mSourceName;  // currently debug-only

    FileInfo() {}

    FileInfo(String8 path) {      // useful for e.g. svect.indexOf
            mFileName = path;
            mFileType = FileType.kFileTypeUnknown;
    }

    FileInfo(FileInfo src) {
      copyMembers(src);
    }
//        const FileInfo& operator= (const FileInfo& src) {
//      if (this != &src)
//        copyMembers(src);
//      return *this;
//    }

    void copyMembers(final FileInfo src) {
      mFileName = src.mFileName;
      mFileType = src.mFileType;
      mSourceName = src.mSourceName;
    }

    /* need this for SortedVector; must compare only on file name */
//    bool operator< (const FileInfo& rhs) const {
//      return mFileName < rhs.mFileName;
//    }
//
//    /* used by AssetManager */
//    bool operator== (const FileInfo& rhs) const {
//      return mFileName == rhs.mFileName;
//    }

    void set(final String8 path, FileType type) {
      mFileName = path;
      mFileType = type;
    }

    String8 getFileName()  { return mFileName; }
    void setFileName(String8 path) { mFileName = path; }

    FileType getFileType() { return mFileType; }
    void setFileType(FileType type) { mFileType = type; }

    String8 getSourceName() { return mSourceName; }
    void setSourceName(String8 path) { mSourceName = path; }

    public boolean isLessThan(FileInfo fileInfo) {
      return mFileName.string().compareTo(fileInfo.mFileName.string()) < 0;
    }

    @Override
    public int compareTo(FileInfo other) {
      return mFileName.string().compareTo(other.mFileName.string());
    }

    /*
     * Handy utility for finding an entry in a sorted vector of FileInfo.
     * Returns the index of the matching entry, or -1 if none found.
     */
    static int findEntry(SortedVector<FileInfo> pVector,
             String8 fileName) {
      FileInfo tmpInfo = new FileInfo();

      tmpInfo.setFileName(fileName);
      return pVector.indexOf(tmpInfo);
    }


  };

  /* AssetManager uses this to initialize us */
  void setFileList(SortedVector<FileInfo> list) { mFileInfo = list; }

}