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
;
26 extern const char * const gDefaultIgnoreAssets
;
27 extern const char * gUserIgnoreAssets
;
29 bool valid_symbol_name(const String8
& str
);
39 AXIS_SCREENLAYOUTSIZE
,
40 AXIS_SCREENLAYOUTLONG
,
51 AXIS_SMALLESTSCREENWIDTHDP
,
56 AXIS_START
= AXIS_MCC
,
57 AXIS_END
= AXIS_VERSION
,
67 SDK_HONEYCOMB_MR2
= 13,
68 SDK_ICE_CREAM_SANDWICH
= 14,
72 * This structure contains a specific variation of a single file out
73 * of all the variations it can have that we can have.
78 AaptGroupEntry() : mParamsChanged(true) { }
79 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
80 : locale(_locale
), vendor(_vendor
), mParamsChanged(true) { }
82 bool initFromDirName(const char* dir
, String8
* resType
);
84 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
86 static uint32_t getConfigValueForAxis(const ResTable_config
& config
, int axis
);
88 static bool configSameExcept(const ResTable_config
& config
,
89 const ResTable_config
& otherConfig
, int axis
);
91 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
92 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
93 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
94 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
95 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
96 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
97 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
98 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
99 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
100 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
101 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
102 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
103 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
104 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
105 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
106 static bool getSmallestScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
107 static bool getScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
108 static bool getScreenHeightDpName(const char* name
, ResTable_config
* out
= NULL
);
109 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
111 int compare(const AaptGroupEntry
& o
) const;
113 const ResTable_config
& toParams() const;
115 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
116 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
117 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
118 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
119 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
120 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
122 String8
toString() const;
123 String8
toDirName(const String8
& resType
) const;
125 const String8
& getVersionString() const { return version
; }
132 String8 smallestScreenWidthDp
;
133 String8 screenWidthDp
;
134 String8 screenHeightDp
;
135 String8 screenLayoutSize
;
136 String8 screenLayoutLong
;
149 mutable bool mParamsChanged
;
150 mutable ResTable_config mParams
;
153 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
155 return lhs
.compare(rhs
);
158 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
160 return compare_type(lhs
, rhs
) < 0;
167 * A single asset file we know about.
169 class AaptFile
: public RefBase
172 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
173 const String8
& resType
)
174 : mGroupEntry(groupEntry
)
175 , mResourceType(resType
)
176 , mSourceFile(sourceFile
)
180 , mCompression(ZipEntry::kCompressStored
)
182 //printf("new AaptFile created %s\n", (const char*)sourceFile);
184 virtual ~AaptFile() {
188 const String8
& getPath() const { return mPath
; }
189 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
191 // Data API. If there is data attached to the file,
192 // getSourceFile() is not used.
193 bool hasData() const { return mData
!= NULL
; }
194 const void* getData() const { return mData
; }
195 size_t getSize() const { return mDataSize
; }
196 void* editData(size_t size
);
197 void* editData(size_t* outSize
= NULL
);
198 void* padData(size_t wordSize
);
199 status_t
writeData(const void* data
, size_t size
);
202 const String8
& getResourceType() const { return mResourceType
; }
204 // File API. If the file does not hold raw data, this is
205 // a full path to a file on the filesystem that holds its data.
206 const String8
& getSourceFile() const { return mSourceFile
; }
208 String8
getPrintableSource() const;
210 // Desired compression method, as per utils/ZipEntry.h. For example,
211 // no compression is ZipEntry::kCompressStored.
212 int getCompressionMethod() const { return mCompression
; }
213 void setCompressionMethod(int c
) { mCompression
= c
; }
215 friend class AaptGroup
;
218 AaptGroupEntry mGroupEntry
;
219 String8 mResourceType
;
228 * A group of related files (the same file, with different
229 * vendor/locale variations).
231 class AaptGroup
: public RefBase
234 AaptGroup(const String8
& leaf
, const String8
& path
)
235 : mLeaf(leaf
), mPath(path
) { }
236 virtual ~AaptGroup() { }
238 const String8
& getLeaf() const { return mLeaf
; }
240 // Returns the relative path after the AaptGroupEntry dirs.
241 const String8
& getPath() const { return mPath
; }
243 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
246 status_t
addFile(const sp
<AaptFile
>& file
);
247 void removeFile(size_t index
);
249 void print(const String8
& prefix
) const;
251 String8
getPrintableSource() const;
257 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
261 * A single directory of assets, which can contain files and other
264 class AaptDir
: public RefBase
267 AaptDir(const String8
& leaf
, const String8
& path
)
268 : mLeaf(leaf
), mPath(path
) { }
269 virtual ~AaptDir() { }
271 const String8
& getLeaf() const { return mLeaf
; }
273 const String8
& getPath() const { return mPath
; }
275 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
276 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
278 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
280 void removeFile(const String8
& name
);
281 void removeDir(const String8
& name
);
284 * Perform some sanity checks on the names of files and directories here.
286 * - Check for illegal chars in filenames.
287 * - Check filename length.
288 * - Check for presence of ".gz" and non-".gz" copies of same file.
289 * - Check for multiple files whose names match in a case-insensitive
290 * fashion (problematic for some systems).
292 * Comparing names against all other names is O(n^2). We could speed
293 * it up some by sorting the entries and being smarter about what we
294 * compare against, but I'm not expecting to have enough files in a
295 * single directory to make a noticeable difference in speed.
297 * Note that sorting here is not enough to guarantee that the package
298 * contents are sorted -- subsequent updates can rearrange things.
300 status_t
validate() const;
302 void print(const String8
& prefix
) const;
304 String8
getPrintableSource() const;
307 friend class AaptAssets
;
309 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
310 sp
<AaptDir
> makeDir(const String8
& name
);
311 status_t
addLeafFile(const String8
& leafName
,
312 const sp
<AaptFile
>& file
);
313 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
314 const String8
& srcDir
,
315 const AaptGroupEntry
& kind
,
316 const String8
& resType
,
317 sp
<FilePathStore
>& fullResPaths
);
322 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
323 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
327 * All information we know about a particular symbol.
329 class AaptSymbolEntry
333 : isPublic(false), typeCode(TYPE_UNKNOWN
)
336 AaptSymbolEntry(const String8
& _name
)
337 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
340 AaptSymbolEntry(const AaptSymbolEntry
& o
)
341 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
342 , comment(o
.comment
), typeComment(o
.typeComment
)
343 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
346 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
348 sourcePos
= o
.sourcePos
;
349 isPublic
= o
.isPublic
;
351 typeComment
= o
.typeComment
;
352 typeCode
= o
.typeCode
;
353 int32Val
= o
.int32Val
;
354 stringVal
= o
.stringVal
;
364 String16 typeComment
;
374 // Value. May be one of these.
380 * A group of related symbols (such as indices into a string block)
381 * that have been generated from the assets.
383 class AaptSymbols
: public RefBase
387 virtual ~AaptSymbols() { }
389 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
390 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
393 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
394 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
395 sym
.int32Val
= value
;
399 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
400 const SourcePos
& pos
) {
401 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
404 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
405 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
406 sym
.stringVal
= value
;
410 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
411 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
414 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
419 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
420 if (comment
.size() <= 0) {
423 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
424 if (sym
.comment
.size() == 0) {
425 sym
.comment
= comment
;
427 sym
.comment
.append(String16("\n"));
428 sym
.comment
.append(comment
);
432 void appendTypeComment(const String8
& name
, const String16
& comment
) {
433 if (comment
.size() <= 0) {
436 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
437 if (sym
.typeComment
.size() == 0) {
438 sym
.typeComment
= comment
;
440 sym
.typeComment
.append(String16("\n"));
441 sym
.typeComment
.append(comment
);
445 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
446 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
450 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
452 sym
= new AaptSymbols();
453 mNestedSymbols
.add(name
, sym
);
459 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
461 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
462 { return mNestedSymbols
; }
464 const String16
& getComment(const String8
& name
) const
465 { return get_symbol(name
).comment
; }
466 const String16
& getTypeComment(const String8
& name
) const
467 { return get_symbol(name
).typeComment
; }
470 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
471 if (valid_symbol_name(symbol
)) {
474 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
477 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
478 ssize_t i
= mSymbols
.indexOfKey(symbol
);
480 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
482 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
483 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
484 sym
.sourcePos
= *pos
;
488 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
489 ssize_t i
= mSymbols
.indexOfKey(symbol
);
491 return mSymbols
.valueAt(i
);
496 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
497 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
498 AaptSymbolEntry mDefSymbol
;
501 class ResourceTypeSet
: public RefBase
,
502 public KeyedVector
<String8
,sp
<AaptGroup
> >
508 // Storage for lists of fully qualified paths for
509 // resources encountered during slurping.
510 class FilePathStore
: public RefBase
,
511 public Vector
<String8
>
518 * Asset hierarchy being operated on.
520 class AaptAssets
: public AaptDir
524 virtual ~AaptAssets() { delete mRes
; }
526 const String8
& getPackage() const { return mPackage
; }
527 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
529 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const;
531 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
533 sp
<AaptFile
> addFile(const String8
& filePath
,
534 const AaptGroupEntry
& entry
,
535 const String8
& srcDir
,
536 sp
<AaptGroup
>* outGroup
,
537 const String8
& resType
);
539 void addResource(const String8
& leafName
,
541 const sp
<AaptFile
>& file
,
542 const String8
& resType
);
544 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
546 ssize_t
slurpFromArgs(Bundle
* bundle
);
548 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
550 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
552 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
553 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
555 status_t
buildIncludedResources(Bundle
* bundle
);
556 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
557 const ResTable
& getIncludedResources() const;
559 void print(const String8
& prefix
) const;
561 inline const Vector
<sp
<AaptDir
> >& resDirs() const { return mResDirs
; }
562 sp
<AaptDir
> resDir(const String8
& name
) const;
564 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
565 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
567 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
569 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
571 inline sp
<FilePathStore
>& getFullResPaths() { return mFullResPaths
; }
573 setFullResPaths(sp
<FilePathStore
>& res
) { mFullResPaths
= res
; }
575 inline sp
<FilePathStore
>& getFullAssetPaths() { return mFullAssetPaths
; }
577 setFullAssetPaths(sp
<FilePathStore
>& res
) { mFullAssetPaths
= res
; }
580 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
581 const String8
& srcDir
,
582 const AaptGroupEntry
& kind
,
583 const String8
& resType
,
584 sp
<FilePathStore
>& fullResPaths
);
586 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
587 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
589 status_t
filter(Bundle
* bundle
);
592 SortedVector
<AaptGroupEntry
> mGroupEntries
;
593 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
594 String8 mSymbolsPrivatePackage
;
596 Vector
<sp
<AaptDir
> > mResDirs
;
600 bool mHaveIncludedAssets
;
601 AssetManager mIncludedAssets
;
603 sp
<AaptAssets
> mOverlay
;
604 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
606 sp
<FilePathStore
> mFullResPaths
;
607 sp
<FilePathStore
> mFullAssetPaths
;
610 #endif // __AAPT_ASSETS_H