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
,
61 * This structure contains a specific variation of a single file out
62 * of all the variations it can have that we can have.
68 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
69 : locale(_locale
), vendor(_vendor
) { }
75 String8 screenLayoutSize
;
76 String8 screenLayoutLong
;
77 String8 screenWidthDp
;
78 String8 screenHeightDp
;
91 bool initFromDirName(const char* dir
, String8
* resType
);
93 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
95 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
96 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
97 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
98 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
99 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
100 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
101 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
102 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
103 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
104 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
105 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
106 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
107 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
108 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
109 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
110 static bool getScreenWidthDpName(const char* name
, ResTable_config
* out
= NULL
);
111 static bool getScreenHeightDpName(const char* name
, ResTable_config
* out
= NULL
);
112 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
114 int compare(const AaptGroupEntry
& o
) const;
116 ResTable_config
toParams() const;
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; }
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; }
125 String8
toString() const;
126 String8
toDirName(const String8
& resType
) const;
129 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
131 return lhs
.compare(rhs
);
134 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
136 return compare_type(lhs
, rhs
) < 0;
142 * A single asset file we know about.
144 class AaptFile
: public RefBase
147 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
148 const String8
& resType
)
149 : mGroupEntry(groupEntry
)
150 , mResourceType(resType
)
151 , mSourceFile(sourceFile
)
155 , mCompression(ZipEntry::kCompressStored
)
157 //printf("new AaptFile created %s\n", (const char*)sourceFile);
159 virtual ~AaptFile() {
163 const String8
& getPath() const { return mPath
; }
164 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
166 // Data API. If there is data attached to the file,
167 // getSourceFile() is not used.
168 bool hasData() const { return mData
!= NULL
; }
169 const void* getData() const { return mData
; }
170 size_t getSize() const { return mDataSize
; }
171 void* editData(size_t size
);
172 void* editData(size_t* outSize
= NULL
);
173 void* padData(size_t wordSize
);
174 status_t
writeData(const void* data
, size_t size
);
177 const String8
& getResourceType() const { return mResourceType
; }
179 // File API. If the file does not hold raw data, this is
180 // a full path to a file on the filesystem that holds its data.
181 const String8
& getSourceFile() const { return mSourceFile
; }
183 String8
getPrintableSource() const;
185 // Desired compression method, as per utils/ZipEntry.h. For example,
186 // no compression is ZipEntry::kCompressStored.
187 int getCompressionMethod() const { return mCompression
; }
188 void setCompressionMethod(int c
) { mCompression
= c
; }
190 friend class AaptGroup
;
193 AaptGroupEntry mGroupEntry
;
194 String8 mResourceType
;
203 * A group of related files (the same file, with different
204 * vendor/locale variations).
206 class AaptGroup
: public RefBase
209 AaptGroup(const String8
& leaf
, const String8
& path
)
210 : mLeaf(leaf
), mPath(path
) { }
211 virtual ~AaptGroup() { }
213 const String8
& getLeaf() const { return mLeaf
; }
215 // Returns the relative path after the AaptGroupEntry dirs.
216 const String8
& getPath() const { return mPath
; }
218 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
221 status_t
addFile(const sp
<AaptFile
>& file
);
222 void removeFile(size_t index
);
226 String8
getPrintableSource() const;
232 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
236 * A single directory of assets, which can contain for files and other
239 class AaptDir
: public RefBase
242 AaptDir(const String8
& leaf
, const String8
& path
)
243 : mLeaf(leaf
), mPath(path
) { }
244 virtual ~AaptDir() { }
246 const String8
& getLeaf() const { return mLeaf
; }
248 const String8
& getPath() const { return mPath
; }
250 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
251 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
253 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
254 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
256 sp
<AaptDir
> makeDir(const String8
& name
);
258 void removeFile(const String8
& name
);
259 void removeDir(const String8
& name
);
261 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
263 status_t
addLeafFile(const String8
& leafName
,
264 const sp
<AaptFile
>& file
);
266 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
267 const String8
& srcDir
,
268 const AaptGroupEntry
& kind
,
269 const String8
& resType
);
272 * Perform some sanity checks on the names of files and directories here.
274 * - Check for illegal chars in filenames.
275 * - Check filename length.
276 * - Check for presence of ".gz" and non-".gz" copies of same file.
277 * - Check for multiple files whose names match in a case-insensitive
278 * fashion (problematic for some systems).
280 * Comparing names against all other names is O(n^2). We could speed
281 * it up some by sorting the entries and being smarter about what we
282 * compare against, but I'm not expecting to have enough files in a
283 * single directory to make a noticeable difference in speed.
285 * Note that sorting here is not enough to guarantee that the package
286 * contents are sorted -- subsequent updates can rearrange things.
288 status_t
validate() const;
292 String8
getPrintableSource() const;
298 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
299 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
303 * All information we know about a particular symbol.
305 class AaptSymbolEntry
309 : isPublic(false), typeCode(TYPE_UNKNOWN
)
312 AaptSymbolEntry(const String8
& _name
)
313 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
316 AaptSymbolEntry(const AaptSymbolEntry
& o
)
317 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
318 , comment(o
.comment
), typeComment(o
.typeComment
)
319 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
322 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
324 sourcePos
= o
.sourcePos
;
325 isPublic
= o
.isPublic
;
327 typeComment
= o
.typeComment
;
328 typeCode
= o
.typeCode
;
329 int32Val
= o
.int32Val
;
330 stringVal
= o
.stringVal
;
340 String16 typeComment
;
350 // Value. May be one of these.
356 * A group of related symbols (such as indices into a string block)
357 * that have been generated from the assets.
359 class AaptSymbols
: public RefBase
363 virtual ~AaptSymbols() { }
365 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
366 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
369 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
370 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
371 sym
.int32Val
= value
;
375 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
376 const SourcePos
& pos
) {
377 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
380 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
381 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
382 sym
.stringVal
= value
;
386 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
387 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
390 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
395 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
396 if (comment
.size() <= 0) {
399 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
400 if (sym
.comment
.size() == 0) {
401 sym
.comment
= comment
;
403 sym
.comment
.append(String16("\n"));
404 sym
.comment
.append(comment
);
408 void appendTypeComment(const String8
& name
, const String16
& comment
) {
409 if (comment
.size() <= 0) {
412 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
413 if (sym
.typeComment
.size() == 0) {
414 sym
.typeComment
= comment
;
416 sym
.typeComment
.append(String16("\n"));
417 sym
.typeComment
.append(comment
);
421 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
422 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
426 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
428 sym
= new AaptSymbols();
429 mNestedSymbols
.add(name
, sym
);
435 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
437 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
438 { return mNestedSymbols
; }
440 const String16
& getComment(const String8
& name
) const
441 { return get_symbol(name
).comment
; }
442 const String16
& getTypeComment(const String8
& name
) const
443 { return get_symbol(name
).typeComment
; }
446 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
447 if (valid_symbol_name(symbol
)) {
450 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
453 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
454 ssize_t i
= mSymbols
.indexOfKey(symbol
);
456 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
458 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
459 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
460 sym
.sourcePos
= *pos
;
464 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
465 ssize_t i
= mSymbols
.indexOfKey(symbol
);
467 return mSymbols
.valueAt(i
);
472 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
473 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
474 AaptSymbolEntry mDefSymbol
;
477 class ResourceTypeSet
: public RefBase
,
478 public KeyedVector
<String8
,sp
<AaptGroup
> >
486 * Asset hierarchy being operated on.
488 class AaptAssets
: public AaptDir
491 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL
) { }
492 virtual ~AaptAssets() { delete mRes
; }
494 const String8
& getPackage() const { return mPackage
; }
495 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
497 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
499 sp
<AaptFile
> addFile(const String8
& filePath
,
500 const AaptGroupEntry
& entry
,
501 const String8
& srcDir
,
502 sp
<AaptGroup
>* outGroup
,
503 const String8
& resType
);
505 void addResource(const String8
& leafName
,
507 const sp
<AaptFile
>& file
,
508 const String8
& resType
);
510 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
512 ssize_t
slurpFromArgs(Bundle
* bundle
);
514 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
515 const String8
& srcDir
,
516 const AaptGroupEntry
& kind
,
517 const String8
& resType
);
519 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
520 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
522 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
524 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
526 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
527 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
529 status_t
buildIncludedResources(Bundle
* bundle
);
530 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
531 const ResTable
& getIncludedResources() const;
535 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
536 sp
<AaptDir
> resDir(const String8
& name
);
538 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
539 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
541 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
543 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
547 SortedVector
<AaptGroupEntry
> mGroupEntries
;
548 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
549 String8 mSymbolsPrivatePackage
;
551 Vector
<sp
<AaptDir
> > mDirs
;
553 bool mHaveIncludedAssets
;
554 AssetManager mIncludedAssets
;
556 sp
<AaptAssets
> mOverlay
;
557 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
560 #endif // __AAPT_ASSETS_H