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>
21 #include "SourcePos.h"
23 using namespace android
;
25 bool valid_symbol_name(const String8
& str
);
33 AXIS_SCREENLAYOUTSIZE
,
34 AXIS_SCREENLAYOUTLONG
,
46 * This structure contains a specific variation of a single file out
47 * of all the variations it can have that we can have.
53 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
54 : locale(_locale
), vendor(_vendor
) { }
60 String8 screenLayoutSize
;
61 String8 screenLayoutLong
;
71 bool initFromDirName(const char* dir
, String8
* resType
);
73 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
75 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
76 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
77 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
78 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
79 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
80 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
81 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
82 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
83 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
84 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
85 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
86 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
87 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
89 int compare(const AaptGroupEntry
& o
) const;
91 ResTable_config
toParams() const;
93 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
94 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
95 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
96 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
97 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
98 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
100 String8
toString() const;
101 String8
toDirName(const String8
& resType
) const;
104 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
106 return lhs
.compare(rhs
);
109 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
111 return compare_type(lhs
, rhs
) < 0;
117 * A single asset file we know about.
119 class AaptFile
: public RefBase
122 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
123 const String8
& resType
)
124 : mGroupEntry(groupEntry
)
125 , mResourceType(resType
)
126 , mSourceFile(sourceFile
)
130 , mCompression(ZipEntry::kCompressStored
)
132 //printf("new AaptFile created %s\n", (const char*)sourceFile);
134 virtual ~AaptFile() {
138 const String8
& getPath() const { return mPath
; }
139 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
141 // Data API. If there is data attached to the file,
142 // getSourceFile() is not used.
143 bool hasData() const { return mData
!= NULL
; }
144 const void* getData() const { return mData
; }
145 size_t getSize() const { return mDataSize
; }
146 void* editData(size_t size
);
147 void* editData(size_t* outSize
= NULL
);
148 void* padData(size_t wordSize
);
149 status_t
writeData(const void* data
, size_t size
);
152 const String8
& getResourceType() const { return mResourceType
; }
154 // File API. If the file does not hold raw data, this is
155 // a full path to a file on the filesystem that holds its data.
156 const String8
& getSourceFile() const { return mSourceFile
; }
158 String8
getPrintableSource() const;
160 // Desired compression method, as per utils/ZipEntry.h. For example,
161 // no compression is ZipEntry::kCompressStored.
162 int getCompressionMethod() const { return mCompression
; }
163 void setCompressionMethod(int c
) { mCompression
= c
; }
165 friend class AaptGroup
;
168 AaptGroupEntry mGroupEntry
;
169 String8 mResourceType
;
178 * A group of related files (the same file, with different
179 * vendor/locale variations).
181 class AaptGroup
: public RefBase
184 AaptGroup(const String8
& leaf
, const String8
& path
)
185 : mLeaf(leaf
), mPath(path
) { }
186 virtual ~AaptGroup() { }
188 const String8
& getLeaf() const { return mLeaf
; }
190 // Returns the relative path after the AaptGroupEntry dirs.
191 const String8
& getPath() const { return mPath
; }
193 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
196 status_t
addFile(const sp
<AaptFile
>& file
);
197 void removeFile(size_t index
);
201 String8
getPrintableSource() const;
207 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
211 * A single directory of assets, which can contain for files and other
214 class AaptDir
: public RefBase
217 AaptDir(const String8
& leaf
, const String8
& path
)
218 : mLeaf(leaf
), mPath(path
) { }
219 virtual ~AaptDir() { }
221 const String8
& getLeaf() const { return mLeaf
; }
223 const String8
& getPath() const { return mPath
; }
225 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
226 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
228 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
229 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
231 sp
<AaptDir
> makeDir(const String8
& name
);
233 void removeFile(const String8
& name
);
234 void removeDir(const String8
& name
);
236 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
238 status_t
addLeafFile(const String8
& leafName
,
239 const sp
<AaptFile
>& file
);
241 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
242 const String8
& srcDir
,
243 const AaptGroupEntry
& kind
,
244 const String8
& resType
);
247 * Perform some sanity checks on the names of files and directories here.
249 * - Check for illegal chars in filenames.
250 * - Check filename length.
251 * - Check for presence of ".gz" and non-".gz" copies of same file.
252 * - Check for multiple files whose names match in a case-insensitive
253 * fashion (problematic for some systems).
255 * Comparing names against all other names is O(n^2). We could speed
256 * it up some by sorting the entries and being smarter about what we
257 * compare against, but I'm not expecting to have enough files in a
258 * single directory to make a noticeable difference in speed.
260 * Note that sorting here is not enough to guarantee that the package
261 * contents are sorted -- subsequent updates can rearrange things.
263 status_t
validate() const;
267 String8
getPrintableSource() const;
273 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
274 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
278 * All information we know about a particular symbol.
280 class AaptSymbolEntry
284 : isPublic(false), typeCode(TYPE_UNKNOWN
)
287 AaptSymbolEntry(const String8
& _name
)
288 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
291 AaptSymbolEntry(const AaptSymbolEntry
& o
)
292 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
293 , comment(o
.comment
), typeComment(o
.typeComment
)
294 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
297 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
299 sourcePos
= o
.sourcePos
;
300 isPublic
= o
.isPublic
;
302 typeComment
= o
.typeComment
;
303 typeCode
= o
.typeCode
;
304 int32Val
= o
.int32Val
;
305 stringVal
= o
.stringVal
;
315 String16 typeComment
;
325 // Value. May be one of these.
331 * A group of related symbols (such as indices into a string block)
332 * that have been generated from the assets.
334 class AaptSymbols
: public RefBase
338 virtual ~AaptSymbols() { }
340 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
341 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
344 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
345 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
346 sym
.int32Val
= value
;
350 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
351 const SourcePos
& pos
) {
352 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
355 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
356 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
357 sym
.stringVal
= value
;
361 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
362 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
365 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
370 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
371 if (comment
.size() <= 0) {
374 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
375 if (sym
.comment
.size() == 0) {
376 sym
.comment
= comment
;
378 sym
.comment
.append(String16("\n"));
379 sym
.comment
.append(comment
);
383 void appendTypeComment(const String8
& name
, const String16
& comment
) {
384 if (comment
.size() <= 0) {
387 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
388 if (sym
.typeComment
.size() == 0) {
389 sym
.typeComment
= comment
;
391 sym
.typeComment
.append(String16("\n"));
392 sym
.typeComment
.append(comment
);
396 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
397 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
401 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
403 sym
= new AaptSymbols();
404 mNestedSymbols
.add(name
, sym
);
410 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
412 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
413 { return mNestedSymbols
; }
415 const String16
& getComment(const String8
& name
) const
416 { return get_symbol(name
).comment
; }
417 const String16
& getTypeComment(const String8
& name
) const
418 { return get_symbol(name
).typeComment
; }
421 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
422 if (valid_symbol_name(symbol
)) {
425 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
428 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
429 ssize_t i
= mSymbols
.indexOfKey(symbol
);
431 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
433 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
434 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
435 sym
.sourcePos
= *pos
;
439 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
440 ssize_t i
= mSymbols
.indexOfKey(symbol
);
442 return mSymbols
.valueAt(i
);
447 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
448 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
449 AaptSymbolEntry mDefSymbol
;
452 class ResourceTypeSet
: public RefBase
,
453 public KeyedVector
<String8
,sp
<AaptGroup
> >
461 * Asset hierarchy being operated on.
463 class AaptAssets
: public AaptDir
466 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL
) { }
467 virtual ~AaptAssets() { delete mRes
; }
469 const String8
& getPackage() const { return mPackage
; }
470 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
472 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
474 sp
<AaptFile
> addFile(const String8
& filePath
,
475 const AaptGroupEntry
& entry
,
476 const String8
& srcDir
,
477 sp
<AaptGroup
>* outGroup
,
478 const String8
& resType
);
480 void addResource(const String8
& leafName
,
482 const sp
<AaptFile
>& file
,
483 const String8
& resType
);
485 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
487 ssize_t
slurpFromArgs(Bundle
* bundle
);
489 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
490 const String8
& srcDir
,
491 const AaptGroupEntry
& kind
,
492 const String8
& resType
);
494 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
495 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
497 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
499 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
501 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
502 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
504 status_t
buildIncludedResources(Bundle
* bundle
);
505 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
506 const ResTable
& getIncludedResources() const;
510 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
512 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
513 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
515 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
517 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
521 SortedVector
<AaptGroupEntry
> mGroupEntries
;
522 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
523 String8 mSymbolsPrivatePackage
;
525 Vector
<sp
<AaptDir
> > mDirs
;
527 bool mHaveIncludedAssets
;
528 AssetManager mIncludedAssets
;
530 sp
<AaptAssets
> mOverlay
;
531 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
534 #endif // __AAPT_ASSETS_H