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
,
49 * This structure contains a specific variation of a single file out
50 * of all the variations it can have that we can have.
56 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
57 : locale(_locale
), vendor(_vendor
) { }
63 String8 screenLayoutSize
;
64 String8 screenLayoutLong
;
77 bool initFromDirName(const char* dir
, String8
* resType
);
79 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
81 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
82 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
83 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
84 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
85 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
86 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
87 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
88 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
89 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
90 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
91 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
92 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
93 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
94 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
95 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
96 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
98 int compare(const AaptGroupEntry
& o
) const;
100 ResTable_config
toParams() const;
102 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
103 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
104 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
105 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
106 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
107 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
109 String8
toString() const;
110 String8
toDirName(const String8
& resType
) const;
113 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
115 return lhs
.compare(rhs
);
118 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
120 return compare_type(lhs
, rhs
) < 0;
126 * A single asset file we know about.
128 class AaptFile
: public RefBase
131 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
132 const String8
& resType
)
133 : mGroupEntry(groupEntry
)
134 , mResourceType(resType
)
135 , mSourceFile(sourceFile
)
139 , mCompression(ZipEntry::kCompressStored
)
141 //printf("new AaptFile created %s\n", (const char*)sourceFile);
143 virtual ~AaptFile() {
147 const String8
& getPath() const { return mPath
; }
148 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
150 // Data API. If there is data attached to the file,
151 // getSourceFile() is not used.
152 bool hasData() const { return mData
!= NULL
; }
153 const void* getData() const { return mData
; }
154 size_t getSize() const { return mDataSize
; }
155 void* editData(size_t size
);
156 void* editData(size_t* outSize
= NULL
);
157 void* padData(size_t wordSize
);
158 status_t
writeData(const void* data
, size_t size
);
161 const String8
& getResourceType() const { return mResourceType
; }
163 // File API. If the file does not hold raw data, this is
164 // a full path to a file on the filesystem that holds its data.
165 const String8
& getSourceFile() const { return mSourceFile
; }
167 String8
getPrintableSource() const;
169 // Desired compression method, as per utils/ZipEntry.h. For example,
170 // no compression is ZipEntry::kCompressStored.
171 int getCompressionMethod() const { return mCompression
; }
172 void setCompressionMethod(int c
) { mCompression
= c
; }
174 friend class AaptGroup
;
177 AaptGroupEntry mGroupEntry
;
178 String8 mResourceType
;
187 * A group of related files (the same file, with different
188 * vendor/locale variations).
190 class AaptGroup
: public RefBase
193 AaptGroup(const String8
& leaf
, const String8
& path
)
194 : mLeaf(leaf
), mPath(path
) { }
195 virtual ~AaptGroup() { }
197 const String8
& getLeaf() const { return mLeaf
; }
199 // Returns the relative path after the AaptGroupEntry dirs.
200 const String8
& getPath() const { return mPath
; }
202 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
205 status_t
addFile(const sp
<AaptFile
>& file
);
206 void removeFile(size_t index
);
210 String8
getPrintableSource() const;
216 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
220 * A single directory of assets, which can contain for files and other
223 class AaptDir
: public RefBase
226 AaptDir(const String8
& leaf
, const String8
& path
)
227 : mLeaf(leaf
), mPath(path
) { }
228 virtual ~AaptDir() { }
230 const String8
& getLeaf() const { return mLeaf
; }
232 const String8
& getPath() const { return mPath
; }
234 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
235 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
237 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
238 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
240 sp
<AaptDir
> makeDir(const String8
& name
);
242 void removeFile(const String8
& name
);
243 void removeDir(const String8
& name
);
245 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
247 status_t
addLeafFile(const String8
& leafName
,
248 const sp
<AaptFile
>& file
);
250 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
251 const String8
& srcDir
,
252 const AaptGroupEntry
& kind
,
253 const String8
& resType
);
256 * Perform some sanity checks on the names of files and directories here.
258 * - Check for illegal chars in filenames.
259 * - Check filename length.
260 * - Check for presence of ".gz" and non-".gz" copies of same file.
261 * - Check for multiple files whose names match in a case-insensitive
262 * fashion (problematic for some systems).
264 * Comparing names against all other names is O(n^2). We could speed
265 * it up some by sorting the entries and being smarter about what we
266 * compare against, but I'm not expecting to have enough files in a
267 * single directory to make a noticeable difference in speed.
269 * Note that sorting here is not enough to guarantee that the package
270 * contents are sorted -- subsequent updates can rearrange things.
272 status_t
validate() const;
276 String8
getPrintableSource() const;
282 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
283 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
287 * All information we know about a particular symbol.
289 class AaptSymbolEntry
293 : isPublic(false), typeCode(TYPE_UNKNOWN
)
296 AaptSymbolEntry(const String8
& _name
)
297 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
300 AaptSymbolEntry(const AaptSymbolEntry
& o
)
301 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
302 , comment(o
.comment
), typeComment(o
.typeComment
)
303 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
306 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
308 sourcePos
= o
.sourcePos
;
309 isPublic
= o
.isPublic
;
311 typeComment
= o
.typeComment
;
312 typeCode
= o
.typeCode
;
313 int32Val
= o
.int32Val
;
314 stringVal
= o
.stringVal
;
324 String16 typeComment
;
334 // Value. May be one of these.
340 * A group of related symbols (such as indices into a string block)
341 * that have been generated from the assets.
343 class AaptSymbols
: public RefBase
347 virtual ~AaptSymbols() { }
349 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
350 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
353 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
354 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
355 sym
.int32Val
= value
;
359 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
360 const SourcePos
& pos
) {
361 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
364 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
365 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
366 sym
.stringVal
= value
;
370 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
371 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
374 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
379 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
380 if (comment
.size() <= 0) {
383 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
384 if (sym
.comment
.size() == 0) {
385 sym
.comment
= comment
;
387 sym
.comment
.append(String16("\n"));
388 sym
.comment
.append(comment
);
392 void appendTypeComment(const String8
& name
, const String16
& comment
) {
393 if (comment
.size() <= 0) {
396 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
397 if (sym
.typeComment
.size() == 0) {
398 sym
.typeComment
= comment
;
400 sym
.typeComment
.append(String16("\n"));
401 sym
.typeComment
.append(comment
);
405 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
406 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
410 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
412 sym
= new AaptSymbols();
413 mNestedSymbols
.add(name
, sym
);
419 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
421 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
422 { return mNestedSymbols
; }
424 const String16
& getComment(const String8
& name
) const
425 { return get_symbol(name
).comment
; }
426 const String16
& getTypeComment(const String8
& name
) const
427 { return get_symbol(name
).typeComment
; }
430 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
431 if (valid_symbol_name(symbol
)) {
434 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
437 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
438 ssize_t i
= mSymbols
.indexOfKey(symbol
);
440 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
442 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
443 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
444 sym
.sourcePos
= *pos
;
448 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
449 ssize_t i
= mSymbols
.indexOfKey(symbol
);
451 return mSymbols
.valueAt(i
);
456 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
457 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
458 AaptSymbolEntry mDefSymbol
;
461 class ResourceTypeSet
: public RefBase
,
462 public KeyedVector
<String8
,sp
<AaptGroup
> >
470 * Asset hierarchy being operated on.
472 class AaptAssets
: public AaptDir
475 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL
) { }
476 virtual ~AaptAssets() { delete mRes
; }
478 const String8
& getPackage() const { return mPackage
; }
479 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
481 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
483 sp
<AaptFile
> addFile(const String8
& filePath
,
484 const AaptGroupEntry
& entry
,
485 const String8
& srcDir
,
486 sp
<AaptGroup
>* outGroup
,
487 const String8
& resType
);
489 void addResource(const String8
& leafName
,
491 const sp
<AaptFile
>& file
,
492 const String8
& resType
);
494 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
496 ssize_t
slurpFromArgs(Bundle
* bundle
);
498 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
499 const String8
& srcDir
,
500 const AaptGroupEntry
& kind
,
501 const String8
& resType
);
503 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
504 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
506 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
508 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
510 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
511 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
513 status_t
buildIncludedResources(Bundle
* bundle
);
514 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
515 const ResTable
& getIncludedResources() const;
519 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
520 sp
<AaptDir
> resDir(const String8
& name
);
522 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
523 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
525 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
527 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
531 SortedVector
<AaptGroupEntry
> mGroupEntries
;
532 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
533 String8 mSymbolsPrivatePackage
;
535 Vector
<sp
<AaptDir
> > mDirs
;
537 bool mHaveIncludedAssets
;
538 AssetManager mIncludedAssets
;
540 sp
<AaptAssets
> mOverlay
;
541 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
544 #endif // __AAPT_ASSETS_H