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 <androidfw/AssetManager.h>
11 #include <androidfw/ResourceTypes.h>
12 #include <utils/KeyedVector.h>
13 #include <utils/RefBase.h>
14 #include <utils/SortedVector.h>
15 #include <utils/String8.h>
16 #include <utils/String8.h>
17 #include <utils/Vector.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
,
57 AXIS_START
= AXIS_MCC
,
58 AXIS_END
= AXIS_VERSION
,
62 * This structure contains a specific variation of a single file out
63 * of all the variations it can have that we can have.
68 AaptGroupEntry() : mParamsChanged(true) { }
69 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
70 : locale(_locale
), vendor(_vendor
), mParamsChanged(true) { }
72 bool initFromDirName(const char* dir
, String8
* resType
);
74 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
76 static uint32_t getConfigValueForAxis(const ResTable_config
& config
, int axis
);
78 static bool configSameExcept(const ResTable_config
& config
,
79 const ResTable_config
& otherConfig
, int axis
);
81 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
82 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
83 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
84 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
85 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
86 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
87 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
88 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
89 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
90 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
91 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
92 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
93 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
94 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
95 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
96 static bool getSmallestScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
97 static bool getScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
98 static bool getScreenHeightDpName(const char* name
, ResTable_config
* out
= NULL
);
99 static bool getLayoutDirectionName(const char* name
, ResTable_config
* out
= NULL
);
100 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
102 int compare(const AaptGroupEntry
& o
) const;
104 const ResTable_config
& toParams() const;
106 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
107 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
108 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
109 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
110 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
111 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
113 String8
toString() const;
114 String8
toDirName(const String8
& resType
) const;
116 const String8
& getVersionString() const { return version
; }
123 String8 smallestScreenWidthDp
;
124 String8 screenWidthDp
;
125 String8 screenHeightDp
;
126 String8 screenLayoutSize
;
127 String8 screenLayoutLong
;
138 String8 layoutDirection
;
141 mutable bool mParamsChanged
;
142 mutable ResTable_config mParams
;
145 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
147 return lhs
.compare(rhs
);
150 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
152 return compare_type(lhs
, rhs
) < 0;
159 * A single asset file we know about.
161 class AaptFile
: public RefBase
164 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
165 const String8
& resType
)
166 : mGroupEntry(groupEntry
)
167 , mResourceType(resType
)
168 , mSourceFile(sourceFile
)
172 , mCompression(ZipEntry::kCompressStored
)
174 //printf("new AaptFile created %s\n", (const char*)sourceFile);
176 virtual ~AaptFile() {
180 const String8
& getPath() const { return mPath
; }
181 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
183 // Data API. If there is data attached to the file,
184 // getSourceFile() is not used.
185 bool hasData() const { return mData
!= NULL
; }
186 const void* getData() const { return mData
; }
187 size_t getSize() const { return mDataSize
; }
188 void* editData(size_t size
);
189 void* editData(size_t* outSize
= NULL
);
190 void* padData(size_t wordSize
);
191 status_t
writeData(const void* data
, size_t size
);
194 const String8
& getResourceType() const { return mResourceType
; }
196 // File API. If the file does not hold raw data, this is
197 // a full path to a file on the filesystem that holds its data.
198 const String8
& getSourceFile() const { return mSourceFile
; }
200 String8
getPrintableSource() const;
202 // Desired compression method, as per utils/ZipEntry.h. For example,
203 // no compression is ZipEntry::kCompressStored.
204 int getCompressionMethod() const { return mCompression
; }
205 void setCompressionMethod(int c
) { mCompression
= c
; }
207 friend class AaptGroup
;
210 AaptGroupEntry mGroupEntry
;
211 String8 mResourceType
;
220 * A group of related files (the same file, with different
221 * vendor/locale variations).
223 class AaptGroup
: public RefBase
226 AaptGroup(const String8
& leaf
, const String8
& path
)
227 : mLeaf(leaf
), mPath(path
) { }
228 virtual ~AaptGroup() { }
230 const String8
& getLeaf() const { return mLeaf
; }
232 // Returns the relative path after the AaptGroupEntry dirs.
233 const String8
& getPath() const { return mPath
; }
235 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
238 status_t
addFile(const sp
<AaptFile
>& file
);
239 void removeFile(size_t index
);
241 void print(const String8
& prefix
) const;
243 String8
getPrintableSource() const;
249 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
253 * A single directory of assets, which can contain files and other
256 class AaptDir
: public RefBase
259 AaptDir(const String8
& leaf
, const String8
& path
)
260 : mLeaf(leaf
), mPath(path
) { }
261 virtual ~AaptDir() { }
263 const String8
& getLeaf() const { return mLeaf
; }
265 const String8
& getPath() const { return mPath
; }
267 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
268 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
270 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
272 void removeFile(const String8
& name
);
273 void removeDir(const String8
& name
);
276 * Perform some sanity checks on the names of files and directories here.
278 * - Check for illegal chars in filenames.
279 * - Check filename length.
280 * - Check for presence of ".gz" and non-".gz" copies of same file.
281 * - Check for multiple files whose names match in a case-insensitive
282 * fashion (problematic for some systems).
284 * Comparing names against all other names is O(n^2). We could speed
285 * it up some by sorting the entries and being smarter about what we
286 * compare against, but I'm not expecting to have enough files in a
287 * single directory to make a noticeable difference in speed.
289 * Note that sorting here is not enough to guarantee that the package
290 * contents are sorted -- subsequent updates can rearrange things.
292 status_t
validate() const;
294 void print(const String8
& prefix
) const;
296 String8
getPrintableSource() const;
299 friend class AaptAssets
;
301 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
302 sp
<AaptDir
> makeDir(const String8
& name
);
303 status_t
addLeafFile(const String8
& leafName
,
304 const sp
<AaptFile
>& file
);
305 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
306 const String8
& srcDir
,
307 const AaptGroupEntry
& kind
,
308 const String8
& resType
,
309 sp
<FilePathStore
>& fullResPaths
);
314 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
315 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
319 * All information we know about a particular symbol.
321 class AaptSymbolEntry
325 : isPublic(false), isJavaSymbol(false), typeCode(TYPE_UNKNOWN
)
328 AaptSymbolEntry(const String8
& _name
)
329 : name(_name
), isPublic(false), isJavaSymbol(false), typeCode(TYPE_UNKNOWN
)
332 AaptSymbolEntry(const AaptSymbolEntry
& o
)
333 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
334 , isJavaSymbol(o
.isJavaSymbol
), comment(o
.comment
), typeComment(o
.typeComment
)
335 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
338 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
340 sourcePos
= o
.sourcePos
;
341 isPublic
= o
.isPublic
;
342 isJavaSymbol
= o
.isJavaSymbol
;
344 typeComment
= o
.typeComment
;
345 typeCode
= o
.typeCode
;
346 int32Val
= o
.int32Val
;
347 stringVal
= o
.stringVal
;
358 String16 typeComment
;
368 // Value. May be one of these.
374 * A group of related symbols (such as indices into a string block)
375 * that have been generated from the assets.
377 class AaptSymbols
: public RefBase
381 virtual ~AaptSymbols() { }
383 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
384 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
387 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
388 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
389 sym
.int32Val
= value
;
393 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
394 const SourcePos
& pos
) {
395 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
398 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
399 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
400 sym
.stringVal
= value
;
404 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
405 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
408 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
413 status_t
makeSymbolJavaSymbol(const String8
& name
, const SourcePos
& pos
) {
414 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
417 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
418 sym
.isJavaSymbol
= true;
422 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
423 if (comment
.size() <= 0) {
426 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
427 if (sym
.comment
.size() == 0) {
428 sym
.comment
= comment
;
430 sym
.comment
.append(String16("\n"));
431 sym
.comment
.append(comment
);
435 void appendTypeComment(const String8
& name
, const String16
& comment
) {
436 if (comment
.size() <= 0) {
439 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
440 if (sym
.typeComment
.size() == 0) {
441 sym
.typeComment
= comment
;
443 sym
.typeComment
.append(String16("\n"));
444 sym
.typeComment
.append(comment
);
448 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
449 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
453 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
455 sym
= new AaptSymbols();
456 mNestedSymbols
.add(name
, sym
);
462 status_t
applyJavaSymbols(const sp
<AaptSymbols
>& javaSymbols
);
464 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
466 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
467 { return mNestedSymbols
; }
469 const String16
& getComment(const String8
& name
) const
470 { return get_symbol(name
).comment
; }
471 const String16
& getTypeComment(const String8
& name
) const
472 { return get_symbol(name
).typeComment
; }
475 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
476 if (valid_symbol_name(symbol
)) {
479 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
482 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
483 ssize_t i
= mSymbols
.indexOfKey(symbol
);
485 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
487 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
488 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
489 sym
.sourcePos
= *pos
;
493 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
494 ssize_t i
= mSymbols
.indexOfKey(symbol
);
496 return mSymbols
.valueAt(i
);
501 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
502 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
503 AaptSymbolEntry mDefSymbol
;
506 class ResourceTypeSet
: public RefBase
,
507 public KeyedVector
<String8
,sp
<AaptGroup
> >
513 // Storage for lists of fully qualified paths for
514 // resources encountered during slurping.
515 class FilePathStore
: public RefBase
,
516 public Vector
<String8
>
523 * Asset hierarchy being operated on.
525 class AaptAssets
: public AaptDir
529 virtual ~AaptAssets() { delete mRes
; }
531 const String8
& getPackage() const { return mPackage
; }
532 void setPackage(const String8
& package
) {
534 mSymbolsPrivatePackage
= package
;
535 mHavePrivateSymbols
= false;
538 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const;
540 virtual status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
542 sp
<AaptFile
> addFile(const String8
& filePath
,
543 const AaptGroupEntry
& entry
,
544 const String8
& srcDir
,
545 sp
<AaptGroup
>* outGroup
,
546 const String8
& resType
);
548 void addResource(const String8
& leafName
,
550 const sp
<AaptFile
>& file
,
551 const String8
& resType
);
553 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
555 ssize_t
slurpFromArgs(Bundle
* bundle
);
557 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
559 sp
<AaptSymbols
> getJavaSymbolsFor(const String8
& name
);
561 status_t
applyJavaSymbols();
563 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
565 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
566 void setSymbolsPrivatePackage(const String8
& pkg
) {
567 mSymbolsPrivatePackage
= pkg
;
568 mHavePrivateSymbols
= mSymbolsPrivatePackage
!= mPackage
;
571 bool havePrivateSymbols() const { return mHavePrivateSymbols
; }
573 bool isJavaSymbol(const AaptSymbolEntry
& sym
, bool includePrivate
) const;
575 status_t
buildIncludedResources(Bundle
* bundle
);
576 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
577 const ResTable
& getIncludedResources() const;
579 void print(const String8
& prefix
) const;
581 inline const Vector
<sp
<AaptDir
> >& resDirs() const { return mResDirs
; }
582 sp
<AaptDir
> resDir(const String8
& name
) const;
584 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
585 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
587 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
589 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
591 inline sp
<FilePathStore
>& getFullResPaths() { return mFullResPaths
; }
593 setFullResPaths(sp
<FilePathStore
>& res
) { mFullResPaths
= res
; }
595 inline sp
<FilePathStore
>& getFullAssetPaths() { return mFullAssetPaths
; }
597 setFullAssetPaths(sp
<FilePathStore
>& res
) { mFullAssetPaths
= res
; }
600 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
601 const String8
& srcDir
,
602 const AaptGroupEntry
& kind
,
603 const String8
& resType
,
604 sp
<FilePathStore
>& fullResPaths
);
606 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
607 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
609 status_t
filter(Bundle
* bundle
);
612 SortedVector
<AaptGroupEntry
> mGroupEntries
;
613 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
614 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mJavaSymbols
;
615 String8 mSymbolsPrivatePackage
;
616 bool mHavePrivateSymbols
;
618 Vector
<sp
<AaptDir
> > mResDirs
;
622 bool mHaveIncludedAssets
;
623 AssetManager mIncludedAssets
;
625 sp
<AaptAssets
> mOverlay
;
626 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
628 sp
<FilePathStore
> mFullResPaths
;
629 sp
<FilePathStore
> mFullAssetPaths
;
632 #endif // __AAPT_ASSETS_H