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
);
35 AXIS_SCREENLAYOUTSIZE
,
36 AXIS_SCREENLAYOUTLONG
,
47 AXIS_SMALLESTSCREENWIDTHDP
,
52 AXIS_START
= AXIS_MCC
,
53 AXIS_END
= AXIS_VERSION
,
57 * This structure contains a specific variation of a single file out
58 * of all the variations it can have that we can have.
63 AaptGroupEntry() : mParamsChanged(true) { }
64 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
65 : locale(_locale
), vendor(_vendor
), mParamsChanged(true) { }
67 bool initFromDirName(const char* dir
, String8
* resType
);
69 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
71 static uint32_t getConfigValueForAxis(const ResTable_config
& config
, int axis
);
73 static bool configSameExcept(const ResTable_config
& config
,
74 const ResTable_config
& otherConfig
, int axis
);
76 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
77 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
78 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
79 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
80 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
81 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
82 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
83 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
84 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
85 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
86 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
87 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
88 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
89 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
90 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
91 static bool getSmallestScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
92 static bool getScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
93 static bool getScreenHeightDpName(const char* name
, ResTable_config
* out
= NULL
);
94 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
96 int compare(const AaptGroupEntry
& o
) const;
98 const ResTable_config
& toParams() const;
100 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
101 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
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; }
107 String8
toString() const;
108 String8
toDirName(const String8
& resType
) const;
110 const String8
& getVersionString() const { return version
; }
117 String8 smallestScreenWidthDp
;
118 String8 screenWidthDp
;
119 String8 screenHeightDp
;
120 String8 screenLayoutSize
;
121 String8 screenLayoutLong
;
134 mutable bool mParamsChanged
;
135 mutable ResTable_config mParams
;
138 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
140 return lhs
.compare(rhs
);
143 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
145 return compare_type(lhs
, rhs
) < 0;
152 * A single asset file we know about.
154 class AaptFile
: public RefBase
157 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
158 const String8
& resType
)
159 : mGroupEntry(groupEntry
)
160 , mResourceType(resType
)
161 , mSourceFile(sourceFile
)
165 , mCompression(ZipEntry::kCompressStored
)
167 //printf("new AaptFile created %s\n", (const char*)sourceFile);
169 virtual ~AaptFile() {
173 const String8
& getPath() const { return mPath
; }
174 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
176 // Data API. If there is data attached to the file,
177 // getSourceFile() is not used.
178 bool hasData() const { return mData
!= NULL
; }
179 const void* getData() const { return mData
; }
180 size_t getSize() const { return mDataSize
; }
181 void* editData(size_t size
);
182 void* editData(size_t* outSize
= NULL
);
183 void* padData(size_t wordSize
);
184 status_t
writeData(const void* data
, size_t size
);
187 const String8
& getResourceType() const { return mResourceType
; }
189 // File API. If the file does not hold raw data, this is
190 // a full path to a file on the filesystem that holds its data.
191 const String8
& getSourceFile() const { return mSourceFile
; }
193 String8
getPrintableSource() const;
195 // Desired compression method, as per utils/ZipEntry.h. For example,
196 // no compression is ZipEntry::kCompressStored.
197 int getCompressionMethod() const { return mCompression
; }
198 void setCompressionMethod(int c
) { mCompression
= c
; }
200 friend class AaptGroup
;
203 AaptGroupEntry mGroupEntry
;
204 String8 mResourceType
;
213 * A group of related files (the same file, with different
214 * vendor/locale variations).
216 class AaptGroup
: public RefBase
219 AaptGroup(const String8
& leaf
, const String8
& path
)
220 : mLeaf(leaf
), mPath(path
) { }
221 virtual ~AaptGroup() { }
223 const String8
& getLeaf() const { return mLeaf
; }
225 // Returns the relative path after the AaptGroupEntry dirs.
226 const String8
& getPath() const { return mPath
; }
228 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
231 status_t
addFile(const sp
<AaptFile
>& file
);
232 void removeFile(size_t index
);
234 void print(const String8
& prefix
) const;
236 String8
getPrintableSource() const;
242 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
246 * A single directory of assets, which can contain files and other
249 class AaptDir
: public RefBase
252 AaptDir(const String8
& leaf
, const String8
& path
)
253 : mLeaf(leaf
), mPath(path
) { }
254 virtual ~AaptDir() { }
256 const String8
& getLeaf() const { return mLeaf
; }
258 const String8
& getPath() const { return mPath
; }
260 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
261 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
263 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
265 void removeFile(const String8
& name
);
266 void removeDir(const String8
& name
);
269 * Perform some sanity checks on the names of files and directories here.
271 * - Check for illegal chars in filenames.
272 * - Check filename length.
273 * - Check for presence of ".gz" and non-".gz" copies of same file.
274 * - Check for multiple files whose names match in a case-insensitive
275 * fashion (problematic for some systems).
277 * Comparing names against all other names is O(n^2). We could speed
278 * it up some by sorting the entries and being smarter about what we
279 * compare against, but I'm not expecting to have enough files in a
280 * single directory to make a noticeable difference in speed.
282 * Note that sorting here is not enough to guarantee that the package
283 * contents are sorted -- subsequent updates can rearrange things.
285 status_t
validate() const;
287 void print(const String8
& prefix
) const;
289 String8
getPrintableSource() const;
292 friend class AaptAssets
;
294 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
295 sp
<AaptDir
> makeDir(const String8
& name
);
296 status_t
addLeafFile(const String8
& leafName
,
297 const sp
<AaptFile
>& file
);
298 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
299 const String8
& srcDir
,
300 const AaptGroupEntry
& kind
,
301 const String8
& resType
,
302 sp
<FilePathStore
>& fullResPaths
);
307 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
308 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
312 * All information we know about a particular symbol.
314 class AaptSymbolEntry
318 : isPublic(false), typeCode(TYPE_UNKNOWN
)
321 AaptSymbolEntry(const String8
& _name
)
322 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
325 AaptSymbolEntry(const AaptSymbolEntry
& o
)
326 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
327 , comment(o
.comment
), typeComment(o
.typeComment
)
328 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
331 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
333 sourcePos
= o
.sourcePos
;
334 isPublic
= o
.isPublic
;
336 typeComment
= o
.typeComment
;
337 typeCode
= o
.typeCode
;
338 int32Val
= o
.int32Val
;
339 stringVal
= o
.stringVal
;
349 String16 typeComment
;
359 // Value. May be one of these.
365 * A group of related symbols (such as indices into a string block)
366 * that have been generated from the assets.
368 class AaptSymbols
: public RefBase
372 virtual ~AaptSymbols() { }
374 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
375 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
378 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
379 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
380 sym
.int32Val
= value
;
384 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
385 const SourcePos
& pos
) {
386 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
389 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
390 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
391 sym
.stringVal
= value
;
395 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
396 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
399 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
404 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
405 if (comment
.size() <= 0) {
408 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
409 if (sym
.comment
.size() == 0) {
410 sym
.comment
= comment
;
412 sym
.comment
.append(String16("\n"));
413 sym
.comment
.append(comment
);
417 void appendTypeComment(const String8
& name
, const String16
& comment
) {
418 if (comment
.size() <= 0) {
421 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
422 if (sym
.typeComment
.size() == 0) {
423 sym
.typeComment
= comment
;
425 sym
.typeComment
.append(String16("\n"));
426 sym
.typeComment
.append(comment
);
430 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
431 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
435 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
437 sym
= new AaptSymbols();
438 mNestedSymbols
.add(name
, sym
);
444 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
446 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
447 { return mNestedSymbols
; }
449 const String16
& getComment(const String8
& name
) const
450 { return get_symbol(name
).comment
; }
451 const String16
& getTypeComment(const String8
& name
) const
452 { return get_symbol(name
).typeComment
; }
455 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
456 if (valid_symbol_name(symbol
)) {
459 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
462 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
463 ssize_t i
= mSymbols
.indexOfKey(symbol
);
465 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
467 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
468 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
469 sym
.sourcePos
= *pos
;
473 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
474 ssize_t i
= mSymbols
.indexOfKey(symbol
);
476 return mSymbols
.valueAt(i
);
481 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
482 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
483 AaptSymbolEntry mDefSymbol
;
486 class ResourceTypeSet
: public RefBase
,
487 public KeyedVector
<String8
,sp
<AaptGroup
> >
493 // Storage for lists of fully qualified paths for
494 // resources encountered during slurping.
495 class FilePathStore
: public RefBase
,
496 public Vector
<String8
>
503 * Asset hierarchy being operated on.
505 class AaptAssets
: public AaptDir
509 virtual ~AaptAssets() { delete mRes
; }
511 const String8
& getPackage() const { return mPackage
; }
512 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
514 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const;
516 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
518 sp
<AaptFile
> addFile(const String8
& filePath
,
519 const AaptGroupEntry
& entry
,
520 const String8
& srcDir
,
521 sp
<AaptGroup
>* outGroup
,
522 const String8
& resType
);
524 void addResource(const String8
& leafName
,
526 const sp
<AaptFile
>& file
,
527 const String8
& resType
);
529 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
531 ssize_t
slurpFromArgs(Bundle
* bundle
);
533 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
535 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
537 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
538 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
540 status_t
buildIncludedResources(Bundle
* bundle
);
541 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
542 const ResTable
& getIncludedResources() const;
544 void print(const String8
& prefix
) const;
546 inline const Vector
<sp
<AaptDir
> >& resDirs() const { return mResDirs
; }
547 sp
<AaptDir
> resDir(const String8
& name
) const;
549 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
550 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
552 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
554 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
556 inline sp
<FilePathStore
>& getFullResPaths() { return mFullResPaths
; }
558 setFullResPaths(sp
<FilePathStore
>& res
) { mFullResPaths
= res
; }
560 inline sp
<FilePathStore
>& getFullAssetPaths() { return mFullAssetPaths
; }
562 setFullAssetPaths(sp
<FilePathStore
>& res
) { mFullAssetPaths
= res
; }
565 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
566 const String8
& srcDir
,
567 const AaptGroupEntry
& kind
,
568 const String8
& resType
,
569 sp
<FilePathStore
>& fullResPaths
);
571 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
572 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
574 status_t
filter(Bundle
* bundle
);
577 SortedVector
<AaptGroupEntry
> mGroupEntries
;
578 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
579 String8 mSymbolsPrivatePackage
;
581 Vector
<sp
<AaptDir
> > mResDirs
;
585 bool mHaveIncludedAssets
;
586 AssetManager mIncludedAssets
;
588 sp
<AaptAssets
> mOverlay
;
589 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
591 sp
<FilePathStore
> mFullResPaths
;
592 sp
<FilePathStore
> mFullAssetPaths
;
595 #endif // __AAPT_ASSETS_H