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
,
63 SDK_HONEYCOMB_MR2
= 13,
64 SDK_ICE_CREAM_SANDWICH
= 14,
68 * This structure contains a specific variation of a single file out
69 * of all the variations it can have that we can have.
74 AaptGroupEntry() : mParamsChanged(true) { }
75 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
76 : locale(_locale
), vendor(_vendor
), mParamsChanged(true) { }
78 bool initFromDirName(const char* dir
, String8
* resType
);
80 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
82 static uint32_t getConfigValueForAxis(const ResTable_config
& config
, int axis
);
84 static bool configSameExcept(const ResTable_config
& config
,
85 const ResTable_config
& otherConfig
, int axis
);
87 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
88 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
89 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
90 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
91 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
92 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
93 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
94 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
95 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
96 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
97 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
98 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
99 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
100 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
101 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
102 static bool getSmallestScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
103 static bool getScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
104 static bool getScreenHeightDpName(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 const 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;
121 const String8
& getVersionString() const { return version
; }
128 String8 smallestScreenWidthDp
;
129 String8 screenWidthDp
;
130 String8 screenHeightDp
;
131 String8 screenLayoutSize
;
132 String8 screenLayoutLong
;
145 mutable bool mParamsChanged
;
146 mutable ResTable_config mParams
;
149 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
151 return lhs
.compare(rhs
);
154 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
156 return compare_type(lhs
, rhs
) < 0;
163 * A single asset file we know about.
165 class AaptFile
: public RefBase
168 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
169 const String8
& resType
)
170 : mGroupEntry(groupEntry
)
171 , mResourceType(resType
)
172 , mSourceFile(sourceFile
)
176 , mCompression(ZipEntry::kCompressStored
)
178 //printf("new AaptFile created %s\n", (const char*)sourceFile);
180 virtual ~AaptFile() {
184 const String8
& getPath() const { return mPath
; }
185 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
187 // Data API. If there is data attached to the file,
188 // getSourceFile() is not used.
189 bool hasData() const { return mData
!= NULL
; }
190 const void* getData() const { return mData
; }
191 size_t getSize() const { return mDataSize
; }
192 void* editData(size_t size
);
193 void* editData(size_t* outSize
= NULL
);
194 void* padData(size_t wordSize
);
195 status_t
writeData(const void* data
, size_t size
);
198 const String8
& getResourceType() const { return mResourceType
; }
200 // File API. If the file does not hold raw data, this is
201 // a full path to a file on the filesystem that holds its data.
202 const String8
& getSourceFile() const { return mSourceFile
; }
204 String8
getPrintableSource() const;
206 // Desired compression method, as per utils/ZipEntry.h. For example,
207 // no compression is ZipEntry::kCompressStored.
208 int getCompressionMethod() const { return mCompression
; }
209 void setCompressionMethod(int c
) { mCompression
= c
; }
211 friend class AaptGroup
;
214 AaptGroupEntry mGroupEntry
;
215 String8 mResourceType
;
224 * A group of related files (the same file, with different
225 * vendor/locale variations).
227 class AaptGroup
: public RefBase
230 AaptGroup(const String8
& leaf
, const String8
& path
)
231 : mLeaf(leaf
), mPath(path
) { }
232 virtual ~AaptGroup() { }
234 const String8
& getLeaf() const { return mLeaf
; }
236 // Returns the relative path after the AaptGroupEntry dirs.
237 const String8
& getPath() const { return mPath
; }
239 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
242 status_t
addFile(const sp
<AaptFile
>& file
);
243 void removeFile(size_t index
);
245 void print(const String8
& prefix
) const;
247 String8
getPrintableSource() const;
253 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
257 * A single directory of assets, which can contain files and other
260 class AaptDir
: public RefBase
263 AaptDir(const String8
& leaf
, const String8
& path
)
264 : mLeaf(leaf
), mPath(path
) { }
265 virtual ~AaptDir() { }
267 const String8
& getLeaf() const { return mLeaf
; }
269 const String8
& getPath() const { return mPath
; }
271 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
272 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
274 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
276 void removeFile(const String8
& name
);
277 void removeDir(const String8
& name
);
280 * Perform some sanity checks on the names of files and directories here.
282 * - Check for illegal chars in filenames.
283 * - Check filename length.
284 * - Check for presence of ".gz" and non-".gz" copies of same file.
285 * - Check for multiple files whose names match in a case-insensitive
286 * fashion (problematic for some systems).
288 * Comparing names against all other names is O(n^2). We could speed
289 * it up some by sorting the entries and being smarter about what we
290 * compare against, but I'm not expecting to have enough files in a
291 * single directory to make a noticeable difference in speed.
293 * Note that sorting here is not enough to guarantee that the package
294 * contents are sorted -- subsequent updates can rearrange things.
296 status_t
validate() const;
298 void print(const String8
& prefix
) const;
300 String8
getPrintableSource() const;
303 friend class AaptAssets
;
305 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
306 sp
<AaptDir
> makeDir(const String8
& name
);
307 status_t
addLeafFile(const String8
& leafName
,
308 const sp
<AaptFile
>& file
);
309 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
310 const String8
& srcDir
,
311 const AaptGroupEntry
& kind
,
312 const String8
& resType
,
313 sp
<FilePathStore
>& fullResPaths
);
318 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
319 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
323 * All information we know about a particular symbol.
325 class AaptSymbolEntry
329 : isPublic(false), typeCode(TYPE_UNKNOWN
)
332 AaptSymbolEntry(const String8
& _name
)
333 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
336 AaptSymbolEntry(const AaptSymbolEntry
& o
)
337 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
338 , comment(o
.comment
), typeComment(o
.typeComment
)
339 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
342 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
344 sourcePos
= o
.sourcePos
;
345 isPublic
= o
.isPublic
;
347 typeComment
= o
.typeComment
;
348 typeCode
= o
.typeCode
;
349 int32Val
= o
.int32Val
;
350 stringVal
= o
.stringVal
;
360 String16 typeComment
;
370 // Value. May be one of these.
376 * A group of related symbols (such as indices into a string block)
377 * that have been generated from the assets.
379 class AaptSymbols
: public RefBase
383 virtual ~AaptSymbols() { }
385 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
386 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
389 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
390 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
391 sym
.int32Val
= value
;
395 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
396 const SourcePos
& pos
) {
397 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
400 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
401 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
402 sym
.stringVal
= value
;
406 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
407 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
410 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
415 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
416 if (comment
.size() <= 0) {
419 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
420 if (sym
.comment
.size() == 0) {
421 sym
.comment
= comment
;
423 sym
.comment
.append(String16("\n"));
424 sym
.comment
.append(comment
);
428 void appendTypeComment(const String8
& name
, const String16
& comment
) {
429 if (comment
.size() <= 0) {
432 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
433 if (sym
.typeComment
.size() == 0) {
434 sym
.typeComment
= comment
;
436 sym
.typeComment
.append(String16("\n"));
437 sym
.typeComment
.append(comment
);
441 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
442 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
446 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
448 sym
= new AaptSymbols();
449 mNestedSymbols
.add(name
, sym
);
455 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
457 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
458 { return mNestedSymbols
; }
460 const String16
& getComment(const String8
& name
) const
461 { return get_symbol(name
).comment
; }
462 const String16
& getTypeComment(const String8
& name
) const
463 { return get_symbol(name
).typeComment
; }
466 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
467 if (valid_symbol_name(symbol
)) {
470 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
473 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
474 ssize_t i
= mSymbols
.indexOfKey(symbol
);
476 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
478 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
479 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
480 sym
.sourcePos
= *pos
;
484 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
485 ssize_t i
= mSymbols
.indexOfKey(symbol
);
487 return mSymbols
.valueAt(i
);
492 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
493 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
494 AaptSymbolEntry mDefSymbol
;
497 class ResourceTypeSet
: public RefBase
,
498 public KeyedVector
<String8
,sp
<AaptGroup
> >
504 // Storage for lists of fully qualified paths for
505 // resources encountered during slurping.
506 class FilePathStore
: public RefBase
,
507 public Vector
<String8
>
514 * Asset hierarchy being operated on.
516 class AaptAssets
: public AaptDir
520 virtual ~AaptAssets() { delete mRes
; }
522 const String8
& getPackage() const { return mPackage
; }
523 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
525 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const;
527 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
529 sp
<AaptFile
> addFile(const String8
& filePath
,
530 const AaptGroupEntry
& entry
,
531 const String8
& srcDir
,
532 sp
<AaptGroup
>* outGroup
,
533 const String8
& resType
);
535 void addResource(const String8
& leafName
,
537 const sp
<AaptFile
>& file
,
538 const String8
& resType
);
540 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
542 ssize_t
slurpFromArgs(Bundle
* bundle
);
544 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
546 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
548 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
549 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
551 status_t
buildIncludedResources(Bundle
* bundle
);
552 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
553 const ResTable
& getIncludedResources() const;
555 void print(const String8
& prefix
) const;
557 inline const Vector
<sp
<AaptDir
> >& resDirs() const { return mResDirs
; }
558 sp
<AaptDir
> resDir(const String8
& name
) const;
560 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
561 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
563 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
565 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
567 inline sp
<FilePathStore
>& getFullResPaths() { return mFullResPaths
; }
569 setFullResPaths(sp
<FilePathStore
>& res
) { mFullResPaths
= res
; }
571 inline sp
<FilePathStore
>& getFullAssetPaths() { return mFullAssetPaths
; }
573 setFullAssetPaths(sp
<FilePathStore
>& res
) { mFullAssetPaths
= res
; }
576 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
577 const String8
& srcDir
,
578 const AaptGroupEntry
& kind
,
579 const String8
& resType
,
580 sp
<FilePathStore
>& fullResPaths
);
582 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
583 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
585 status_t
filter(Bundle
* bundle
);
588 SortedVector
<AaptGroupEntry
> mGroupEntries
;
589 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
590 String8 mSymbolsPrivatePackage
;
592 Vector
<sp
<AaptDir
> > mResDirs
;
596 bool mHaveIncludedAssets
;
597 AssetManager mIncludedAssets
;
599 sp
<AaptAssets
> mOverlay
;
600 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
602 sp
<FilePathStore
> mFullResPaths
;
603 sp
<FilePathStore
> mFullAssetPaths
;
606 #endif // __AAPT_ASSETS_H