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;
146 * A single asset file we know about.
148 class AaptFile
: public RefBase
151 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
152 const String8
& resType
)
153 : mGroupEntry(groupEntry
)
154 , mResourceType(resType
)
155 , mSourceFile(sourceFile
)
159 , mCompression(ZipEntry::kCompressStored
)
161 //printf("new AaptFile created %s\n", (const char*)sourceFile);
163 virtual ~AaptFile() {
167 const String8
& getPath() const { return mPath
; }
168 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
170 // Data API. If there is data attached to the file,
171 // getSourceFile() is not used.
172 bool hasData() const { return mData
!= NULL
; }
173 const void* getData() const { return mData
; }
174 size_t getSize() const { return mDataSize
; }
175 void* editData(size_t size
);
176 void* editData(size_t* outSize
= NULL
);
177 void* padData(size_t wordSize
);
178 status_t
writeData(const void* data
, size_t size
);
181 const String8
& getResourceType() const { return mResourceType
; }
183 // File API. If the file does not hold raw data, this is
184 // a full path to a file on the filesystem that holds its data.
185 const String8
& getSourceFile() const { return mSourceFile
; }
187 String8
getPrintableSource() const;
189 // Desired compression method, as per utils/ZipEntry.h. For example,
190 // no compression is ZipEntry::kCompressStored.
191 int getCompressionMethod() const { return mCompression
; }
192 void setCompressionMethod(int c
) { mCompression
= c
; }
194 friend class AaptGroup
;
197 AaptGroupEntry mGroupEntry
;
198 String8 mResourceType
;
207 * A group of related files (the same file, with different
208 * vendor/locale variations).
210 class AaptGroup
: public RefBase
213 AaptGroup(const String8
& leaf
, const String8
& path
)
214 : mLeaf(leaf
), mPath(path
) { }
215 virtual ~AaptGroup() { }
217 const String8
& getLeaf() const { return mLeaf
; }
219 // Returns the relative path after the AaptGroupEntry dirs.
220 const String8
& getPath() const { return mPath
; }
222 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
225 status_t
addFile(const sp
<AaptFile
>& file
);
226 void removeFile(size_t index
);
230 String8
getPrintableSource() const;
236 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
240 * A single directory of assets, which can contain for files and other
243 class AaptDir
: public RefBase
246 AaptDir(const String8
& leaf
, const String8
& path
)
247 : mLeaf(leaf
), mPath(path
) { }
248 virtual ~AaptDir() { }
250 const String8
& getLeaf() const { return mLeaf
; }
252 const String8
& getPath() const { return mPath
; }
254 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
255 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
257 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
258 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
260 sp
<AaptDir
> makeDir(const String8
& name
);
262 void removeFile(const String8
& name
);
263 void removeDir(const String8
& name
);
265 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
267 status_t
addLeafFile(const String8
& leafName
,
268 const sp
<AaptFile
>& file
);
270 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
271 const String8
& srcDir
,
272 const AaptGroupEntry
& kind
,
273 const String8
& resType
,
274 sp
<FilePathStore
>& fullResPaths
);
277 * Perform some sanity checks on the names of files and directories here.
279 * - Check for illegal chars in filenames.
280 * - Check filename length.
281 * - Check for presence of ".gz" and non-".gz" copies of same file.
282 * - Check for multiple files whose names match in a case-insensitive
283 * fashion (problematic for some systems).
285 * Comparing names against all other names is O(n^2). We could speed
286 * it up some by sorting the entries and being smarter about what we
287 * compare against, but I'm not expecting to have enough files in a
288 * single directory to make a noticeable difference in speed.
290 * Note that sorting here is not enough to guarantee that the package
291 * contents are sorted -- subsequent updates can rearrange things.
293 status_t
validate() const;
297 String8
getPrintableSource() const;
303 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
304 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
308 * All information we know about a particular symbol.
310 class AaptSymbolEntry
314 : isPublic(false), typeCode(TYPE_UNKNOWN
)
317 AaptSymbolEntry(const String8
& _name
)
318 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
321 AaptSymbolEntry(const AaptSymbolEntry
& o
)
322 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
323 , comment(o
.comment
), typeComment(o
.typeComment
)
324 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
327 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
329 sourcePos
= o
.sourcePos
;
330 isPublic
= o
.isPublic
;
332 typeComment
= o
.typeComment
;
333 typeCode
= o
.typeCode
;
334 int32Val
= o
.int32Val
;
335 stringVal
= o
.stringVal
;
345 String16 typeComment
;
355 // Value. May be one of these.
361 * A group of related symbols (such as indices into a string block)
362 * that have been generated from the assets.
364 class AaptSymbols
: public RefBase
368 virtual ~AaptSymbols() { }
370 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
371 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
374 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
375 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
376 sym
.int32Val
= value
;
380 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
381 const SourcePos
& pos
) {
382 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
385 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
386 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
387 sym
.stringVal
= value
;
391 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
392 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
395 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
400 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
401 if (comment
.size() <= 0) {
404 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
405 if (sym
.comment
.size() == 0) {
406 sym
.comment
= comment
;
408 sym
.comment
.append(String16("\n"));
409 sym
.comment
.append(comment
);
413 void appendTypeComment(const String8
& name
, const String16
& comment
) {
414 if (comment
.size() <= 0) {
417 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
418 if (sym
.typeComment
.size() == 0) {
419 sym
.typeComment
= comment
;
421 sym
.typeComment
.append(String16("\n"));
422 sym
.typeComment
.append(comment
);
426 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
427 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
431 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
433 sym
= new AaptSymbols();
434 mNestedSymbols
.add(name
, sym
);
440 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
442 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
443 { return mNestedSymbols
; }
445 const String16
& getComment(const String8
& name
) const
446 { return get_symbol(name
).comment
; }
447 const String16
& getTypeComment(const String8
& name
) const
448 { return get_symbol(name
).typeComment
; }
451 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
452 if (valid_symbol_name(symbol
)) {
455 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
458 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
459 ssize_t i
= mSymbols
.indexOfKey(symbol
);
461 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
463 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
464 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
465 sym
.sourcePos
= *pos
;
469 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
470 ssize_t i
= mSymbols
.indexOfKey(symbol
);
472 return mSymbols
.valueAt(i
);
477 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
478 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
479 AaptSymbolEntry mDefSymbol
;
482 class ResourceTypeSet
: public RefBase
,
483 public KeyedVector
<String8
,sp
<AaptGroup
> >
489 // Storage for lists of fully qualified paths for
490 // resources encountered during slurping.
491 class FilePathStore
: public RefBase
,
492 public Vector
<String8
>
499 * Asset hierarchy being operated on.
501 class AaptAssets
: public AaptDir
504 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL
) { }
505 virtual ~AaptAssets() { delete mRes
; }
507 const String8
& getPackage() const { return mPackage
; }
508 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
510 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
512 sp
<AaptFile
> addFile(const String8
& filePath
,
513 const AaptGroupEntry
& entry
,
514 const String8
& srcDir
,
515 sp
<AaptGroup
>* outGroup
,
516 const String8
& resType
);
518 void addResource(const String8
& leafName
,
520 const sp
<AaptFile
>& file
,
521 const String8
& resType
);
523 void addGroupEntry(const AaptGroupEntry
& entry
) { mGroupEntries
.add(entry
); }
525 ssize_t
slurpFromArgs(Bundle
* bundle
);
527 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
528 const String8
& srcDir
,
529 const AaptGroupEntry
& kind
,
530 const String8
& resType
,
531 sp
<FilePathStore
>& fullResPaths
);
533 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
534 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
536 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
538 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
540 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
541 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
543 status_t
buildIncludedResources(Bundle
* bundle
);
544 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
545 const ResTable
& getIncludedResources() const;
549 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
550 sp
<AaptDir
> resDir(const String8
& name
);
552 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
553 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
555 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
557 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { delete mRes
; mRes
= res
; }
559 inline sp
<FilePathStore
>& getFullResPaths() { return mFullResPaths
; }
561 setFullResPaths(sp
<FilePathStore
>& res
) { mFullResPaths
= res
; }
563 inline sp
<FilePathStore
>& getFullAssetPaths() { return mFullAssetPaths
; }
565 setFullAssetPaths(sp
<FilePathStore
>& res
) { mFullAssetPaths
= res
; }
569 SortedVector
<AaptGroupEntry
> mGroupEntries
;
570 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
571 String8 mSymbolsPrivatePackage
;
573 Vector
<sp
<AaptDir
> > mDirs
;
575 bool mHaveIncludedAssets
;
576 AssetManager mIncludedAssets
;
578 sp
<AaptAssets
> mOverlay
;
579 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
581 sp
<FilePathStore
> mFullResPaths
;
582 sp
<FilePathStore
> mFullAssetPaths
;
585 #endif // __AAPT_ASSETS_H