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
,
58 * This structure contains a specific variation of a single file out
59 * of all the variations it can have that we can have.
65 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
66 : locale(_locale
), vendor(_vendor
) { }
72 String8 screenLayoutSize
;
73 String8 screenLayoutLong
;
86 bool initFromDirName(const char* dir
, String8
* resType
);
88 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
90 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
91 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
92 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
93 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
94 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
95 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
96 static bool getUiModeTypeName(const char* name
, ResTable_config
* out
= NULL
);
97 static bool getUiModeNightName(const char* name
, ResTable_config
* out
= NULL
);
98 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
99 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
100 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
101 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
102 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
103 static bool getNavHiddenName(const char* name
, ResTable_config
* out
= NULL
);
104 static bool getScreenSizeName(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 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;
122 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
124 return lhs
.compare(rhs
);
127 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
129 return compare_type(lhs
, rhs
) < 0;
136 * A single asset file we know about.
138 class AaptFile
: public RefBase
141 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
142 const String8
& resType
)
143 : mGroupEntry(groupEntry
)
144 , mResourceType(resType
)
145 , mSourceFile(sourceFile
)
149 , mCompression(ZipEntry::kCompressStored
)
151 //printf("new AaptFile created %s\n", (const char*)sourceFile);
153 virtual ~AaptFile() {
157 const String8
& getPath() const { return mPath
; }
158 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
160 // Data API. If there is data attached to the file,
161 // getSourceFile() is not used.
162 bool hasData() const { return mData
!= NULL
; }
163 const void* getData() const { return mData
; }
164 size_t getSize() const { return mDataSize
; }
165 void* editData(size_t size
);
166 void* editData(size_t* outSize
= NULL
);
167 void* padData(size_t wordSize
);
168 status_t
writeData(const void* data
, size_t size
);
171 const String8
& getResourceType() const { return mResourceType
; }
173 // File API. If the file does not hold raw data, this is
174 // a full path to a file on the filesystem that holds its data.
175 const String8
& getSourceFile() const { return mSourceFile
; }
177 String8
getPrintableSource() const;
179 // Desired compression method, as per utils/ZipEntry.h. For example,
180 // no compression is ZipEntry::kCompressStored.
181 int getCompressionMethod() const { return mCompression
; }
182 void setCompressionMethod(int c
) { mCompression
= c
; }
184 friend class AaptGroup
;
187 AaptGroupEntry mGroupEntry
;
188 String8 mResourceType
;
197 * A group of related files (the same file, with different
198 * vendor/locale variations).
200 class AaptGroup
: public RefBase
203 AaptGroup(const String8
& leaf
, const String8
& path
)
204 : mLeaf(leaf
), mPath(path
) { }
205 virtual ~AaptGroup() { }
207 const String8
& getLeaf() const { return mLeaf
; }
209 // Returns the relative path after the AaptGroupEntry dirs.
210 const String8
& getPath() const { return mPath
; }
212 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
215 status_t
addFile(const sp
<AaptFile
>& file
);
216 void removeFile(size_t index
);
220 String8
getPrintableSource() const;
226 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
230 * A single directory of assets, which can contain for files and other
233 class AaptDir
: public RefBase
236 AaptDir(const String8
& leaf
, const String8
& path
)
237 : mLeaf(leaf
), mPath(path
) { }
238 virtual ~AaptDir() { }
240 const String8
& getLeaf() const { return mLeaf
; }
242 const String8
& getPath() const { return mPath
; }
244 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
245 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
247 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
248 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
250 sp
<AaptDir
> makeDir(const String8
& name
);
252 void removeFile(const String8
& name
);
253 void removeDir(const String8
& name
);
255 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
257 status_t
addLeafFile(const String8
& leafName
,
258 const sp
<AaptFile
>& file
);
260 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
261 const String8
& srcDir
,
262 const AaptGroupEntry
& kind
,
263 const String8
& resType
,
264 sp
<FilePathStore
>& fullResPaths
);
267 * Perform some sanity checks on the names of files and directories here.
269 * - Check for illegal chars in filenames.
270 * - Check filename length.
271 * - Check for presence of ".gz" and non-".gz" copies of same file.
272 * - Check for multiple files whose names match in a case-insensitive
273 * fashion (problematic for some systems).
275 * Comparing names against all other names is O(n^2). We could speed
276 * it up some by sorting the entries and being smarter about what we
277 * compare against, but I'm not expecting to have enough files in a
278 * single directory to make a noticeable difference in speed.
280 * Note that sorting here is not enough to guarantee that the package
281 * contents are sorted -- subsequent updates can rearrange things.
283 status_t
validate() const;
287 String8
getPrintableSource() const;
293 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
294 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
298 * All information we know about a particular symbol.
300 class AaptSymbolEntry
304 : isPublic(false), typeCode(TYPE_UNKNOWN
)
307 AaptSymbolEntry(const String8
& _name
)
308 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
311 AaptSymbolEntry(const AaptSymbolEntry
& o
)
312 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
313 , comment(o
.comment
), typeComment(o
.typeComment
)
314 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
317 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
319 sourcePos
= o
.sourcePos
;
320 isPublic
= o
.isPublic
;
322 typeComment
= o
.typeComment
;
323 typeCode
= o
.typeCode
;
324 int32Val
= o
.int32Val
;
325 stringVal
= o
.stringVal
;
335 String16 typeComment
;
345 // Value. May be one of these.
351 * A group of related symbols (such as indices into a string block)
352 * that have been generated from the assets.
354 class AaptSymbols
: public RefBase
358 virtual ~AaptSymbols() { }
360 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
361 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
364 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
365 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
366 sym
.int32Val
= value
;
370 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
371 const SourcePos
& pos
) {
372 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
375 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
376 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
377 sym
.stringVal
= value
;
381 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
382 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
385 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
390 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
391 if (comment
.size() <= 0) {
394 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
395 if (sym
.comment
.size() == 0) {
396 sym
.comment
= comment
;
398 sym
.comment
.append(String16("\n"));
399 sym
.comment
.append(comment
);
403 void appendTypeComment(const String8
& name
, const String16
& comment
) {
404 if (comment
.size() <= 0) {
407 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
408 if (sym
.typeComment
.size() == 0) {
409 sym
.typeComment
= comment
;
411 sym
.typeComment
.append(String16("\n"));
412 sym
.typeComment
.append(comment
);
416 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
417 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
421 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
423 sym
= new AaptSymbols();
424 mNestedSymbols
.add(name
, sym
);
430 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
432 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
433 { return mNestedSymbols
; }
435 const String16
& getComment(const String8
& name
) const
436 { return get_symbol(name
).comment
; }
437 const String16
& getTypeComment(const String8
& name
) const
438 { return get_symbol(name
).typeComment
; }
441 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
442 if (valid_symbol_name(symbol
)) {
445 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
448 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
449 ssize_t i
= mSymbols
.indexOfKey(symbol
);
451 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
453 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
454 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
455 sym
.sourcePos
= *pos
;
459 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
460 ssize_t i
= mSymbols
.indexOfKey(symbol
);
462 return mSymbols
.valueAt(i
);
467 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
468 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
469 AaptSymbolEntry mDefSymbol
;
472 class ResourceTypeSet
: public RefBase
,
473 public KeyedVector
<String8
,sp
<AaptGroup
> >
479 // Storage for lists of fully qualified paths for
480 // resources encountered during slurping.
481 class FilePathStore
: public RefBase
,
482 public Vector
<String8
>
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
,
521 sp
<FilePathStore
>& fullResPaths
);
523 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
524 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
526 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
528 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
530 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
531 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
533 status_t
buildIncludedResources(Bundle
* bundle
);
534 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
535 const ResTable
& getIncludedResources() const;
539 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
540 sp
<AaptDir
> resDir(const String8
& name
);
542 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
543 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
545 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
547 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
549 inline sp
<FilePathStore
>& getFullResPaths() { return mFullResPaths
; }
551 setFullResPaths(sp
<FilePathStore
>& res
) { mFullResPaths
= res
; }
555 SortedVector
<AaptGroupEntry
> mGroupEntries
;
556 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
557 String8 mSymbolsPrivatePackage
;
559 Vector
<sp
<AaptDir
> > mDirs
;
561 bool mHaveIncludedAssets
;
562 AssetManager mIncludedAssets
;
564 sp
<AaptAssets
> mOverlay
;
565 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
567 sp
<FilePathStore
> mFullResPaths
;
570 #endif // __AAPT_ASSETS_H