2 // Copyright 2006 The Android Open Source Project
4 // Information about assets being operated on.
6 #ifndef __AAPT_ASSETS_H
7 #define __AAPT_ASSETS_H
10 #include <utils/AssetManager.h>
11 #include <utils/KeyedVector.h>
12 #include <utils/String8.h>
13 #include <utils/ResourceTypes.h>
14 #include <utils/SortedVector.h>
15 #include <utils/String8.h>
16 #include <utils/Vector.h>
17 #include <utils/RefBase.h>
18 #include <utils/ZipFile.h>
21 #include "SourcePos.h"
23 using namespace android
;
25 bool valid_symbol_name(const String8
& str
);
44 * This structure contains a specific variation of a single file out
45 * of all the variations it can have that we can have.
51 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
52 : locale(_locale
), vendor(_vendor
) { }
67 bool initFromDirName(const char* dir
, String8
* resType
);
69 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
71 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
72 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
73 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
74 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
75 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
76 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
77 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
78 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
79 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
80 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
81 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
83 int compare(const AaptGroupEntry
& o
) const;
85 ResTable_config
toParams() const;
87 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
88 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
89 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
90 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
91 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
92 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
94 String8
toString() const;
95 String8
toDirName(const String8
& resType
) const;
98 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
100 return lhs
.compare(rhs
);
103 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
105 return compare_type(lhs
, rhs
) < 0;
111 * A single asset file we know about.
113 class AaptFile
: public RefBase
116 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
117 const String8
& resType
)
118 : mGroupEntry(groupEntry
)
119 , mResourceType(resType
)
120 , mSourceFile(sourceFile
)
124 , mCompression(ZipEntry::kCompressStored
)
126 //printf("new AaptFile created %s\n", (const char*)sourceFile);
128 virtual ~AaptFile() {
132 const String8
& getPath() const { return mPath
; }
133 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
135 // Data API. If there is data attached to the file,
136 // getSourceFile() is not used.
137 bool hasData() const { return mData
!= NULL
; }
138 const void* getData() const { return mData
; }
139 size_t getSize() const { return mDataSize
; }
140 void* editData(size_t size
);
141 void* editData(size_t* outSize
= NULL
);
142 void* padData(size_t wordSize
);
143 status_t
writeData(const void* data
, size_t size
);
146 const String8
& getResourceType() const { return mResourceType
; }
148 // File API. If the file does not hold raw data, this is
149 // a full path to a file on the filesystem that holds its data.
150 const String8
& getSourceFile() const { return mSourceFile
; }
152 String8
getPrintableSource() const;
154 // Desired compression method, as per utils/ZipEntry.h. For example,
155 // no compression is ZipEntry::kCompressStored.
156 int getCompressionMethod() const { return mCompression
; }
157 void setCompressionMethod(int c
) { mCompression
= c
; }
159 friend class AaptGroup
;
162 AaptGroupEntry mGroupEntry
;
163 String8 mResourceType
;
172 * A group of related files (the same file, with different
173 * vendor/locale variations).
175 class AaptGroup
: public RefBase
178 AaptGroup(const String8
& leaf
, const String8
& path
)
179 : mLeaf(leaf
), mPath(path
) { }
180 virtual ~AaptGroup() { }
182 const String8
& getLeaf() const { return mLeaf
; }
184 // Returns the relative path after the AaptGroupEntry dirs.
185 const String8
& getPath() const { return mPath
; }
187 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
190 status_t
addFile(const sp
<AaptFile
>& file
);
191 void removeFile(size_t index
);
195 String8
getPrintableSource() const;
201 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
205 * A single directory of assets, which can contain for files and other
208 class AaptDir
: public RefBase
211 AaptDir(const String8
& leaf
, const String8
& path
)
212 : mLeaf(leaf
), mPath(path
) { }
213 virtual ~AaptDir() { }
215 const String8
& getLeaf() const { return mLeaf
; }
217 const String8
& getPath() const { return mPath
; }
219 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
220 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
222 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
223 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
225 sp
<AaptDir
> makeDir(const String8
& name
);
227 void removeFile(const String8
& name
);
228 void removeDir(const String8
& name
);
230 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
232 status_t
addLeafFile(const String8
& leafName
,
233 const sp
<AaptFile
>& file
);
235 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
236 const String8
& srcDir
,
237 const AaptGroupEntry
& kind
,
238 const String8
& resType
);
241 * Perform some sanity checks on the names of files and directories here.
243 * - Check for illegal chars in filenames.
244 * - Check filename length.
245 * - Check for presence of ".gz" and non-".gz" copies of same file.
246 * - Check for multiple files whose names match in a case-insensitive
247 * fashion (problematic for some systems).
249 * Comparing names against all other names is O(n^2). We could speed
250 * it up some by sorting the entries and being smarter about what we
251 * compare against, but I'm not expecting to have enough files in a
252 * single directory to make a noticeable difference in speed.
254 * Note that sorting here is not enough to guarantee that the package
255 * contents are sorted -- subsequent updates can rearrange things.
257 status_t
validate() const;
261 String8
getPrintableSource() const;
267 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
268 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
272 * All information we know about a particular symbol.
274 class AaptSymbolEntry
278 : isPublic(false), typeCode(TYPE_UNKNOWN
)
281 AaptSymbolEntry(const String8
& _name
)
282 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
285 AaptSymbolEntry(const AaptSymbolEntry
& o
)
286 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
287 , comment(o
.comment
), typeComment(o
.typeComment
)
288 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
291 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
293 sourcePos
= o
.sourcePos
;
294 isPublic
= o
.isPublic
;
296 typeComment
= o
.typeComment
;
297 typeCode
= o
.typeCode
;
298 int32Val
= o
.int32Val
;
299 stringVal
= o
.stringVal
;
309 String16 typeComment
;
319 // Value. May be one of these.
325 * A group of related symbols (such as indices into a string block)
326 * that have been generated from the assets.
328 class AaptSymbols
: public RefBase
332 virtual ~AaptSymbols() { }
334 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
335 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
338 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
339 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
340 sym
.int32Val
= value
;
344 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
345 const SourcePos
& pos
) {
346 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
349 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
350 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
351 sym
.stringVal
= value
;
355 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
356 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
359 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
364 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
365 if (comment
.size() <= 0) {
368 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
369 if (sym
.comment
.size() == 0) {
370 sym
.comment
= comment
;
372 sym
.comment
.append(String16("\n"));
373 sym
.comment
.append(comment
);
377 void appendTypeComment(const String8
& name
, const String16
& comment
) {
378 if (comment
.size() <= 0) {
381 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
382 if (sym
.typeComment
.size() == 0) {
383 sym
.typeComment
= comment
;
385 sym
.typeComment
.append(String16("\n"));
386 sym
.typeComment
.append(comment
);
390 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
391 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
395 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
397 sym
= new AaptSymbols();
398 mNestedSymbols
.add(name
, sym
);
404 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
406 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
407 { return mNestedSymbols
; }
409 const String16
& getComment(const String8
& name
) const
410 { return get_symbol(name
).comment
; }
411 const String16
& getTypeComment(const String8
& name
) const
412 { return get_symbol(name
).typeComment
; }
415 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
416 if (valid_symbol_name(symbol
)) {
419 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
422 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
423 ssize_t i
= mSymbols
.indexOfKey(symbol
);
425 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
427 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
428 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
429 sym
.sourcePos
= *pos
;
433 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
434 ssize_t i
= mSymbols
.indexOfKey(symbol
);
436 return mSymbols
.valueAt(i
);
441 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
442 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
443 AaptSymbolEntry mDefSymbol
;
446 class ResourceTypeSet
: public RefBase
,
447 public KeyedVector
<String8
,sp
<AaptGroup
> >
455 * Asset hierarchy being operated on.
457 class AaptAssets
: public AaptDir
460 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL
) { }
461 virtual ~AaptAssets() { delete mRes
; }
463 const String8
& getPackage() const { return mPackage
; }
464 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
466 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
468 sp
<AaptFile
> addFile(const String8
& filePath
,
469 const AaptGroupEntry
& entry
,
470 const String8
& srcDir
,
471 sp
<AaptGroup
>* outGroup
,
472 const String8
& resType
);
474 void addResource(const String8
& leafName
,
476 const sp
<AaptFile
>& file
,
477 const String8
& resType
);
479 ssize_t
slurpFromArgs(Bundle
* bundle
);
481 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
482 const String8
& srcDir
,
483 const AaptGroupEntry
& kind
,
484 const String8
& resType
);
486 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
487 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
489 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
491 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
493 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
494 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
496 status_t
buildIncludedResources(Bundle
* bundle
);
497 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
498 const ResTable
& getIncludedResources() const;
502 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
504 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
505 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
507 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
509 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
513 SortedVector
<AaptGroupEntry
> mGroupEntries
;
514 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
515 String8 mSymbolsPrivatePackage
;
517 Vector
<sp
<AaptDir
> > mDirs
;
519 bool mHaveIncludedAssets
;
520 AssetManager mIncludedAssets
;
522 sp
<AaptAssets
> mOverlay
;
523 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
526 #endif // __AAPT_ASSETS_H