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
,
58 * This structure contains a specific variation of a single file out
59 * of all the variations it can have that we can have.
65 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
66 : locale(_locale
), vendor(_vendor
) { }
72 String8 screenLayoutSize
;
73 String8 screenLayoutLong
;
86 bool initFromDirName(const char* dir
, String8
* resType
);
88 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
90 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
91 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
92 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
93 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
94 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
95 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
96 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
97 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
98 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
99 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
100 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
101 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
102 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
103 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
104 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
105 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
107 int compare(const AaptGroupEntry
& o
) const;
109 ResTable_config
toParams() const;
111 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
112 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
113 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
114 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
115 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
116 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
118 String8
toString() const;
119 String8
toDirName(const String8
& resType
) const;
122 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
124 return lhs
.compare(rhs
);
127 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
129 return compare_type(lhs
, rhs
) < 0;
135 * A single asset file we know about.
137 class AaptFile
: public RefBase
140 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
141 const String8
& resType
)
142 : mGroupEntry(groupEntry
)
143 , mResourceType(resType
)
144 , mSourceFile(sourceFile
)
148 , mCompression(ZipEntry::kCompressStored
)
150 //printf("new AaptFile created %s\n", (const char*)sourceFile);
152 virtual ~AaptFile() {
156 const String8
& getPath() const { return mPath
; }
157 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
159 // Data API. If there is data attached to the file,
160 // getSourceFile() is not used.
161 bool hasData() const { return mData
!= NULL
; }
162 const void* getData() const { return mData
; }
163 size_t getSize() const { return mDataSize
; }
164 void* editData(size_t size
);
165 void* editData(size_t* outSize
= NULL
);
166 void* padData(size_t wordSize
);
167 status_t
writeData(const void* data
, size_t size
);
170 const String8
& getResourceType() const { return mResourceType
; }
172 // File API. If the file does not hold raw data, this is
173 // a full path to a file on the filesystem that holds its data.
174 const String8
& getSourceFile() const { return mSourceFile
; }
176 String8
getPrintableSource() const;
178 // Desired compression method, as per utils/ZipEntry.h. For example,
179 // no compression is ZipEntry::kCompressStored.
180 int getCompressionMethod() const { return mCompression
; }
181 void setCompressionMethod(int c
) { mCompression
= c
; }
183 friend class AaptGroup
;
186 AaptGroupEntry mGroupEntry
;
187 String8 mResourceType
;
196 * A group of related files (the same file, with different
197 * vendor/locale variations).
199 class AaptGroup
: public RefBase
202 AaptGroup(const String8
& leaf
, const String8
& path
)
203 : mLeaf(leaf
), mPath(path
) { }
204 virtual ~AaptGroup() { }
206 const String8
& getLeaf() const { return mLeaf
; }
208 // Returns the relative path after the AaptGroupEntry dirs.
209 const String8
& getPath() const { return mPath
; }
211 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
214 status_t
addFile(const sp
<AaptFile
>& file
);
215 void removeFile(size_t index
);
219 String8
getPrintableSource() const;
225 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
229 * A single directory of assets, which can contain for files and other
232 class AaptDir
: public RefBase
235 AaptDir(const String8
& leaf
, const String8
& path
)
236 : mLeaf(leaf
), mPath(path
) { }
237 virtual ~AaptDir() { }
239 const String8
& getLeaf() const { return mLeaf
; }
241 const String8
& getPath() const { return mPath
; }
243 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
244 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
246 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
247 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
249 sp
<AaptDir
> makeDir(const String8
& name
);
251 void removeFile(const String8
& name
);
252 void removeDir(const String8
& name
);
254 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
256 status_t
addLeafFile(const String8
& leafName
,
257 const sp
<AaptFile
>& file
);
259 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
260 const String8
& srcDir
,
261 const AaptGroupEntry
& kind
,
262 const String8
& resType
);
265 * Perform some sanity checks on the names of files and directories here.
267 * - Check for illegal chars in filenames.
268 * - Check filename length.
269 * - Check for presence of ".gz" and non-".gz" copies of same file.
270 * - Check for multiple files whose names match in a case-insensitive
271 * fashion (problematic for some systems).
273 * Comparing names against all other names is O(n^2). We could speed
274 * it up some by sorting the entries and being smarter about what we
275 * compare against, but I'm not expecting to have enough files in a
276 * single directory to make a noticeable difference in speed.
278 * Note that sorting here is not enough to guarantee that the package
279 * contents are sorted -- subsequent updates can rearrange things.
281 status_t
validate() const;
285 String8
getPrintableSource() const;
291 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
292 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
296 * All information we know about a particular symbol.
298 class AaptSymbolEntry
302 : isPublic(false), typeCode(TYPE_UNKNOWN
)
305 AaptSymbolEntry(const String8
& _name
)
306 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
309 AaptSymbolEntry(const AaptSymbolEntry
& o
)
310 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
311 , comment(o
.comment
), typeComment(o
.typeComment
)
312 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
315 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
317 sourcePos
= o
.sourcePos
;
318 isPublic
= o
.isPublic
;
320 typeComment
= o
.typeComment
;
321 typeCode
= o
.typeCode
;
322 int32Val
= o
.int32Val
;
323 stringVal
= o
.stringVal
;
333 String16 typeComment
;
343 // Value. May be one of these.
349 * A group of related symbols (such as indices into a string block)
350 * that have been generated from the assets.
352 class AaptSymbols
: public RefBase
356 virtual ~AaptSymbols() { }
358 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
359 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
362 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
363 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
364 sym
.int32Val
= value
;
368 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
369 const SourcePos
& pos
) {
370 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
373 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
374 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
375 sym
.stringVal
= value
;
379 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
380 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
383 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
388 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
389 if (comment
.size() <= 0) {
392 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
393 if (sym
.comment
.size() == 0) {
394 sym
.comment
= comment
;
396 sym
.comment
.append(String16("\n"));
397 sym
.comment
.append(comment
);
401 void appendTypeComment(const String8
& name
, const String16
& comment
) {
402 if (comment
.size() <= 0) {
405 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
406 if (sym
.typeComment
.size() == 0) {
407 sym
.typeComment
= comment
;
409 sym
.typeComment
.append(String16("\n"));
410 sym
.typeComment
.append(comment
);
414 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
415 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
419 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
421 sym
= new AaptSymbols();
422 mNestedSymbols
.add(name
, sym
);
428 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
430 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
431 { return mNestedSymbols
; }
433 const String16
& getComment(const String8
& name
) const
434 { return get_symbol(name
).comment
; }
435 const String16
& getTypeComment(const String8
& name
) const
436 { return get_symbol(name
).typeComment
; }
439 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
440 if (valid_symbol_name(symbol
)) {
443 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
446 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
447 ssize_t i
= mSymbols
.indexOfKey(symbol
);
449 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
451 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
452 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
453 sym
.sourcePos
= *pos
;
457 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
458 ssize_t i
= mSymbols
.indexOfKey(symbol
);
460 return mSymbols
.valueAt(i
);
465 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
466 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
467 AaptSymbolEntry mDefSymbol
;
470 class ResourceTypeSet
: public RefBase
,
471 public KeyedVector
<String8
,sp
<AaptGroup
> >
479 * Asset hierarchy being operated on.
481 class AaptAssets
: public AaptDir
484 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL
) { }
485 virtual ~AaptAssets() { delete mRes
; }
487 const String8
& getPackage() const { return mPackage
; }
488 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
490 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
492 sp
<AaptFile
> addFile(const String8
& filePath
,
493 const AaptGroupEntry
& entry
,
494 const String8
& srcDir
,
495 sp
<AaptGroup
>* outGroup
,
496 const String8
& resType
);
498 void addResource(const String8
& leafName
,
500 const sp
<AaptFile
>& file
,
501 const String8
& resType
);
503 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
505 ssize_t
slurpFromArgs(Bundle
* bundle
);
507 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
508 const String8
& srcDir
,
509 const AaptGroupEntry
& kind
,
510 const String8
& resType
);
512 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
513 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
515 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
517 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
519 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
520 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
522 status_t
buildIncludedResources(Bundle
* bundle
);
523 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
524 const ResTable
& getIncludedResources() const;
528 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
529 sp
<AaptDir
> resDir(const String8
& name
);
531 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
532 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
534 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
536 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
540 SortedVector
<AaptGroupEntry
> mGroupEntries
;
541 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
542 String8 mSymbolsPrivatePackage
;
544 Vector
<sp
<AaptDir
> > mDirs
;
546 bool mHaveIncludedAssets
;
547 AssetManager mIncludedAssets
;
549 sp
<AaptAssets
> mOverlay
;
550 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
553 #endif // __AAPT_ASSETS_H