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), isJavaSymbol(false), typeCode(TYPE_UNKNOWN
)
321 AaptSymbolEntry(const String8
& _name
)
322 : name(_name
), isPublic(false), isJavaSymbol(false), typeCode(TYPE_UNKNOWN
)
325 AaptSymbolEntry(const AaptSymbolEntry
& o
)
326 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
327 , isJavaSymbol(o
.isJavaSymbol
), 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
;
335 isJavaSymbol
= o
.isJavaSymbol
;
337 typeComment
= o
.typeComment
;
338 typeCode
= o
.typeCode
;
339 int32Val
= o
.int32Val
;
340 stringVal
= o
.stringVal
;
351 String16 typeComment
;
361 // Value. May be one of these.
367 * A group of related symbols (such as indices into a string block)
368 * that have been generated from the assets.
370 class AaptSymbols
: public RefBase
374 virtual ~AaptSymbols() { }
376 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
377 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
380 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
381 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
382 sym
.int32Val
= value
;
386 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
387 const SourcePos
& pos
) {
388 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
391 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
392 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
393 sym
.stringVal
= value
;
397 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
398 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
401 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
406 status_t
makeSymbolJavaSymbol(const String8
& name
, const SourcePos
& pos
) {
407 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
410 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
411 sym
.isJavaSymbol
= true;
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 status_t
applyJavaSymbols(const sp
<AaptSymbols
>& javaSymbols
);
457 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
459 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
460 { return mNestedSymbols
; }
462 const String16
& getComment(const String8
& name
) const
463 { return get_symbol(name
).comment
; }
464 const String16
& getTypeComment(const String8
& name
) const
465 { return get_symbol(name
).typeComment
; }
468 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
469 if (valid_symbol_name(symbol
)) {
472 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
475 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
476 ssize_t i
= mSymbols
.indexOfKey(symbol
);
478 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
480 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
481 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
482 sym
.sourcePos
= *pos
;
486 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
487 ssize_t i
= mSymbols
.indexOfKey(symbol
);
489 return mSymbols
.valueAt(i
);
494 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
495 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
496 AaptSymbolEntry mDefSymbol
;
499 class ResourceTypeSet
: public RefBase
,
500 public KeyedVector
<String8
,sp
<AaptGroup
> >
506 // Storage for lists of fully qualified paths for
507 // resources encountered during slurping.
508 class FilePathStore
: public RefBase
,
509 public Vector
<String8
>
516 * Asset hierarchy being operated on.
518 class AaptAssets
: public AaptDir
522 virtual ~AaptAssets() { delete mRes
; }
524 const String8
& getPackage() const { return mPackage
; }
525 void setPackage(const String8
& package
) {
527 mSymbolsPrivatePackage
= package
;
528 mHavePrivateSymbols
= false;
531 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const;
533 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
535 sp
<AaptFile
> addFile(const String8
& filePath
,
536 const AaptGroupEntry
& entry
,
537 const String8
& srcDir
,
538 sp
<AaptGroup
>* outGroup
,
539 const String8
& resType
);
541 void addResource(const String8
& leafName
,
543 const sp
<AaptFile
>& file
,
544 const String8
& resType
);
546 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
548 ssize_t
slurpFromArgs(Bundle
* bundle
);
550 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
552 sp
<AaptSymbols
> getJavaSymbolsFor(const String8
& name
);
554 status_t
applyJavaSymbols();
556 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
558 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
559 void setSymbolsPrivatePackage(const String8
& pkg
) {
560 mSymbolsPrivatePackage
= pkg
;
561 mHavePrivateSymbols
= mSymbolsPrivatePackage
!= mPackage
;
564 bool havePrivateSymbols() const { return mHavePrivateSymbols
; }
566 bool isJavaSymbol(const AaptSymbolEntry
& sym
, bool includePrivate
) const;
568 status_t
buildIncludedResources(Bundle
* bundle
);
569 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
570 const ResTable
& getIncludedResources() const;
572 void print(const String8
& prefix
) const;
574 inline const Vector
<sp
<AaptDir
> >& resDirs() const { return mResDirs
; }
575 sp
<AaptDir
> resDir(const String8
& name
) const;
577 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
578 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
580 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
582 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
584 inline sp
<FilePathStore
>& getFullResPaths() { return mFullResPaths
; }
586 setFullResPaths(sp
<FilePathStore
>& res
) { mFullResPaths
= res
; }
588 inline sp
<FilePathStore
>& getFullAssetPaths() { return mFullAssetPaths
; }
590 setFullAssetPaths(sp
<FilePathStore
>& res
) { mFullAssetPaths
= res
; }
593 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
594 const String8
& srcDir
,
595 const AaptGroupEntry
& kind
,
596 const String8
& resType
,
597 sp
<FilePathStore
>& fullResPaths
);
599 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
600 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
602 status_t
filter(Bundle
* bundle
);
605 SortedVector
<AaptGroupEntry
> mGroupEntries
;
606 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
607 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mJavaSymbols
;
608 String8 mSymbolsPrivatePackage
;
609 bool mHavePrivateSymbols
;
611 Vector
<sp
<AaptDir
> > mResDirs
;
615 bool mHaveIncludedAssets
;
616 AssetManager mIncludedAssets
;
618 sp
<AaptAssets
> mOverlay
;
619 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
621 sp
<FilePathStore
> mFullResPaths
;
622 sp
<FilePathStore
> mFullAssetPaths
;
625 #endif // __AAPT_ASSETS_H