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
);
33 AXIS_SCREENLAYOUTSIZE
,
34 AXIS_SCREENLAYOUTLONG
,
45 AXIS_SMALLESTSCREENWIDTHDP
,
58 SDK_HONEYCOMB_MR2
= 13,
62 * This structure contains a specific variation of a single file out
63 * of all the variations it can have that we can have.
69 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
70 : locale(_locale
), vendor(_vendor
) { }
76 String8 smallestScreenWidthDp
;
77 String8 screenWidthDp
;
78 String8 screenHeightDp
;
79 String8 screenLayoutSize
;
80 String8 screenLayoutLong
;
93 bool initFromDirName(const char* dir
, String8
* resType
);
95 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
97 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
98 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
99 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
100 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
101 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
102 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
103 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
104 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
105 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
106 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
107 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
108 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
109 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
110 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
111 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
112 static bool getSmallestScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
113 static bool getScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
114 static bool getScreenHeightDpName(const char* name
, ResTable_config
* out
= NULL
);
115 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
117 int compare(const AaptGroupEntry
& o
) const;
119 ResTable_config
toParams() const;
121 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
122 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
123 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
124 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
125 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
126 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
128 String8
toString() const;
129 String8
toDirName(const String8
& resType
) const;
132 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
134 return lhs
.compare(rhs
);
137 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
139 return compare_type(lhs
, rhs
) < 0;
145 * A single asset file we know about.
147 class AaptFile
: public RefBase
150 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
151 const String8
& resType
)
152 : mGroupEntry(groupEntry
)
153 , mResourceType(resType
)
154 , mSourceFile(sourceFile
)
158 , mCompression(ZipEntry::kCompressStored
)
160 //printf("new AaptFile created %s\n", (const char*)sourceFile);
162 virtual ~AaptFile() {
166 const String8
& getPath() const { return mPath
; }
167 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
169 // Data API. If there is data attached to the file,
170 // getSourceFile() is not used.
171 bool hasData() const { return mData
!= NULL
; }
172 const void* getData() const { return mData
; }
173 size_t getSize() const { return mDataSize
; }
174 void* editData(size_t size
);
175 void* editData(size_t* outSize
= NULL
);
176 void* padData(size_t wordSize
);
177 status_t
writeData(const void* data
, size_t size
);
180 const String8
& getResourceType() const { return mResourceType
; }
182 // File API. If the file does not hold raw data, this is
183 // a full path to a file on the filesystem that holds its data.
184 const String8
& getSourceFile() const { return mSourceFile
; }
186 String8
getPrintableSource() const;
188 // Desired compression method, as per utils/ZipEntry.h. For example,
189 // no compression is ZipEntry::kCompressStored.
190 int getCompressionMethod() const { return mCompression
; }
191 void setCompressionMethod(int c
) { mCompression
= c
; }
193 friend class AaptGroup
;
196 AaptGroupEntry mGroupEntry
;
197 String8 mResourceType
;
206 * A group of related files (the same file, with different
207 * vendor/locale variations).
209 class AaptGroup
: public RefBase
212 AaptGroup(const String8
& leaf
, const String8
& path
)
213 : mLeaf(leaf
), mPath(path
) { }
214 virtual ~AaptGroup() { }
216 const String8
& getLeaf() const { return mLeaf
; }
218 // Returns the relative path after the AaptGroupEntry dirs.
219 const String8
& getPath() const { return mPath
; }
221 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
224 status_t
addFile(const sp
<AaptFile
>& file
);
225 void removeFile(size_t index
);
229 String8
getPrintableSource() const;
235 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
239 * A single directory of assets, which can contain for files and other
242 class AaptDir
: public RefBase
245 AaptDir(const String8
& leaf
, const String8
& path
)
246 : mLeaf(leaf
), mPath(path
) { }
247 virtual ~AaptDir() { }
249 const String8
& getLeaf() const { return mLeaf
; }
251 const String8
& getPath() const { return mPath
; }
253 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
254 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
256 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
257 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
259 sp
<AaptDir
> makeDir(const String8
& name
);
261 void removeFile(const String8
& name
);
262 void removeDir(const String8
& name
);
264 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
266 status_t
addLeafFile(const String8
& leafName
,
267 const sp
<AaptFile
>& file
);
269 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
270 const String8
& srcDir
,
271 const AaptGroupEntry
& kind
,
272 const String8
& resType
);
275 * Perform some sanity checks on the names of files and directories here.
277 * - Check for illegal chars in filenames.
278 * - Check filename length.
279 * - Check for presence of ".gz" and non-".gz" copies of same file.
280 * - Check for multiple files whose names match in a case-insensitive
281 * fashion (problematic for some systems).
283 * Comparing names against all other names is O(n^2). We could speed
284 * it up some by sorting the entries and being smarter about what we
285 * compare against, but I'm not expecting to have enough files in a
286 * single directory to make a noticeable difference in speed.
288 * Note that sorting here is not enough to guarantee that the package
289 * contents are sorted -- subsequent updates can rearrange things.
291 status_t
validate() const;
295 String8
getPrintableSource() const;
301 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
302 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
306 * All information we know about a particular symbol.
308 class AaptSymbolEntry
312 : isPublic(false), typeCode(TYPE_UNKNOWN
)
315 AaptSymbolEntry(const String8
& _name
)
316 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
319 AaptSymbolEntry(const AaptSymbolEntry
& o
)
320 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
321 , comment(o
.comment
), typeComment(o
.typeComment
)
322 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
325 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
327 sourcePos
= o
.sourcePos
;
328 isPublic
= o
.isPublic
;
330 typeComment
= o
.typeComment
;
331 typeCode
= o
.typeCode
;
332 int32Val
= o
.int32Val
;
333 stringVal
= o
.stringVal
;
343 String16 typeComment
;
353 // Value. May be one of these.
359 * A group of related symbols (such as indices into a string block)
360 * that have been generated from the assets.
362 class AaptSymbols
: public RefBase
366 virtual ~AaptSymbols() { }
368 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
369 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
372 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
373 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
374 sym
.int32Val
= value
;
378 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
379 const SourcePos
& pos
) {
380 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
383 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
384 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
385 sym
.stringVal
= value
;
389 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
390 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
393 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
398 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
399 if (comment
.size() <= 0) {
402 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
403 if (sym
.comment
.size() == 0) {
404 sym
.comment
= comment
;
406 sym
.comment
.append(String16("\n"));
407 sym
.comment
.append(comment
);
411 void appendTypeComment(const String8
& name
, const String16
& comment
) {
412 if (comment
.size() <= 0) {
415 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
416 if (sym
.typeComment
.size() == 0) {
417 sym
.typeComment
= comment
;
419 sym
.typeComment
.append(String16("\n"));
420 sym
.typeComment
.append(comment
);
424 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
425 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
429 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
431 sym
= new AaptSymbols();
432 mNestedSymbols
.add(name
, sym
);
438 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
440 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
441 { return mNestedSymbols
; }
443 const String16
& getComment(const String8
& name
) const
444 { return get_symbol(name
).comment
; }
445 const String16
& getTypeComment(const String8
& name
) const
446 { return get_symbol(name
).typeComment
; }
449 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
450 if (valid_symbol_name(symbol
)) {
453 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
456 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
457 ssize_t i
= mSymbols
.indexOfKey(symbol
);
459 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
461 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
462 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
463 sym
.sourcePos
= *pos
;
467 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
468 ssize_t i
= mSymbols
.indexOfKey(symbol
);
470 return mSymbols
.valueAt(i
);
475 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
476 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
477 AaptSymbolEntry mDefSymbol
;
480 class ResourceTypeSet
: public RefBase
,
481 public KeyedVector
<String8
,sp
<AaptGroup
> >
489 * Asset hierarchy being operated on.
491 class AaptAssets
: public AaptDir
494 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL
) { }
495 virtual ~AaptAssets() { delete mRes
; }
497 const String8
& getPackage() const { return mPackage
; }
498 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
500 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
502 sp
<AaptFile
> addFile(const String8
& filePath
,
503 const AaptGroupEntry
& entry
,
504 const String8
& srcDir
,
505 sp
<AaptGroup
>* outGroup
,
506 const String8
& resType
);
508 void addResource(const String8
& leafName
,
510 const sp
<AaptFile
>& file
,
511 const String8
& resType
);
513 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
515 ssize_t
slurpFromArgs(Bundle
* bundle
);
517 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
518 const String8
& srcDir
,
519 const AaptGroupEntry
& kind
,
520 const String8
& resType
);
522 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
523 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
525 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
527 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
529 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
530 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
532 status_t
buildIncludedResources(Bundle
* bundle
);
533 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
534 const ResTable
& getIncludedResources() const;
538 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
539 sp
<AaptDir
> resDir(const String8
& name
);
541 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
542 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
544 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
546 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
550 SortedVector
<AaptGroupEntry
> mGroupEntries
;
551 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
552 String8 mSymbolsPrivatePackage
;
554 Vector
<sp
<AaptDir
> > mDirs
;
556 bool mHaveIncludedAssets
;
557 AssetManager mIncludedAssets
;
559 sp
<AaptAssets
> mOverlay
;
560 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
563 #endif // __AAPT_ASSETS_H