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>
18 #include <utils/ZipFile.h>
21 #include "SourcePos.h"
23 using namespace android
;
25 bool valid_symbol_name(const String8
& str
);
33 AXIS_SCREENLAYOUTSIZE
,
34 AXIS_SCREENLAYOUTLONG
,
46 * This structure contains a specific variation of a single file out
47 * of all the variations it can have that we can have.
53 AaptGroupEntry(const String8
& _locale
, const String8
& _vendor
)
54 : locale(_locale
), vendor(_vendor
) { }
60 String8 screenLayoutSize
;
61 String8 screenLayoutLong
;
71 bool initFromDirName(const char* dir
, String8
* resType
);
73 static status_t
parseNamePart(const String8
& part
, int* axis
, uint32_t* value
);
75 static bool getMccName(const char* name
, ResTable_config
* out
= NULL
);
76 static bool getMncName(const char* name
, ResTable_config
* out
= NULL
);
77 static bool getLocaleName(const char* name
, ResTable_config
* out
= NULL
);
78 static bool getScreenLayoutSizeName(const char* name
, ResTable_config
* out
= NULL
);
79 static bool getScreenLayoutLongName(const char* name
, ResTable_config
* out
= NULL
);
80 static bool getOrientationName(const char* name
, ResTable_config
* out
= NULL
);
81 static bool getDensityName(const char* name
, ResTable_config
* out
= NULL
);
82 static bool getTouchscreenName(const char* name
, ResTable_config
* out
= NULL
);
83 static bool getKeysHiddenName(const char* name
, ResTable_config
* out
= NULL
);
84 static bool getKeyboardName(const char* name
, ResTable_config
* out
= NULL
);
85 static bool getNavigationName(const char* name
, ResTable_config
* out
= NULL
);
86 static bool getScreenSizeName(const char* name
, ResTable_config
* out
= NULL
);
87 static bool getVersionName(const char* name
, ResTable_config
* out
= NULL
);
89 int compare(const AaptGroupEntry
& o
) const;
91 ResTable_config
toParams() const;
93 inline bool operator<(const AaptGroupEntry
& o
) const { return compare(o
) < 0; }
94 inline bool operator<=(const AaptGroupEntry
& o
) const { return compare(o
) <= 0; }
95 inline bool operator==(const AaptGroupEntry
& o
) const { return compare(o
) == 0; }
96 inline bool operator!=(const AaptGroupEntry
& o
) const { return compare(o
) != 0; }
97 inline bool operator>=(const AaptGroupEntry
& o
) const { return compare(o
) >= 0; }
98 inline bool operator>(const AaptGroupEntry
& o
) const { return compare(o
) > 0; }
100 String8
toString() const;
101 String8
toDirName(const String8
& resType
) const;
104 inline int compare_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
106 return lhs
.compare(rhs
);
109 inline int strictly_order_type(const AaptGroupEntry
& lhs
, const AaptGroupEntry
& rhs
)
111 return compare_type(lhs
, rhs
) < 0;
117 * A single asset file we know about.
119 class AaptFile
: public RefBase
122 AaptFile(const String8
& sourceFile
, const AaptGroupEntry
& groupEntry
,
123 const String8
& resType
)
124 : mGroupEntry(groupEntry
)
125 , mResourceType(resType
)
126 , mSourceFile(sourceFile
)
130 , mCompression(ZipEntry::kCompressStored
)
132 //printf("new AaptFile created %s\n", (const char*)sourceFile);
134 virtual ~AaptFile() { }
136 const String8
& getPath() const { return mPath
; }
137 const AaptGroupEntry
& getGroupEntry() const { return mGroupEntry
; }
139 // Data API. If there is data attached to the file,
140 // getSourceFile() is not used.
141 bool hasData() const { return mData
!= NULL
; }
142 const void* getData() const { return mData
; }
143 size_t getSize() const { return mDataSize
; }
144 void* editData(size_t size
);
145 void* editData(size_t* outSize
= NULL
);
146 void* padData(size_t wordSize
);
147 status_t
writeData(const void* data
, size_t size
);
150 const String8
& getResourceType() const { return mResourceType
; }
152 // File API. If the file does not hold raw data, this is
153 // a full path to a file on the filesystem that holds its data.
154 const String8
& getSourceFile() const { return mSourceFile
; }
156 String8
getPrintableSource() const;
158 // Desired compression method, as per utils/ZipEntry.h. For example,
159 // no compression is ZipEntry::kCompressStored.
160 int getCompressionMethod() const { return mCompression
; }
161 void setCompressionMethod(int c
) { mCompression
= c
; }
163 friend class AaptGroup
;
166 AaptGroupEntry mGroupEntry
;
167 String8 mResourceType
;
176 * A group of related files (the same file, with different
177 * vendor/locale variations).
179 class AaptGroup
: public RefBase
182 AaptGroup(const String8
& leaf
, const String8
& path
)
183 : mLeaf(leaf
), mPath(path
) { }
184 virtual ~AaptGroup() { }
186 const String8
& getLeaf() const { return mLeaf
; }
188 // Returns the relative path after the AaptGroupEntry dirs.
189 const String8
& getPath() const { return mPath
; }
191 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& getFiles() const
194 status_t
addFile(const sp
<AaptFile
>& file
);
195 void removeFile(size_t index
);
199 String8
getPrintableSource() const;
205 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > mFiles
;
209 * A single directory of assets, which can contain for files and other
212 class AaptDir
: public RefBase
215 AaptDir(const String8
& leaf
, const String8
& path
)
216 : mLeaf(leaf
), mPath(path
) { }
217 virtual ~AaptDir() { }
219 const String8
& getLeaf() const { return mLeaf
; }
221 const String8
& getPath() const { return mPath
; }
223 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& getFiles() const { return mFiles
; }
224 const DefaultKeyedVector
<String8
, sp
<AaptDir
> >& getDirs() const { return mDirs
; }
226 status_t
addFile(const String8
& name
, const sp
<AaptGroup
>& file
);
227 status_t
addDir(const String8
& name
, const sp
<AaptDir
>& dir
);
229 sp
<AaptDir
> makeDir(const String8
& name
);
231 void removeFile(const String8
& name
);
232 void removeDir(const String8
& name
);
234 status_t
renameFile(const sp
<AaptFile
>& file
, const String8
& newName
);
236 status_t
addLeafFile(const String8
& leafName
,
237 const sp
<AaptFile
>& file
);
239 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
240 const String8
& srcDir
,
241 const AaptGroupEntry
& kind
,
242 const String8
& resType
);
245 * Perform some sanity checks on the names of files and directories here.
247 * - Check for illegal chars in filenames.
248 * - Check filename length.
249 * - Check for presence of ".gz" and non-".gz" copies of same file.
250 * - Check for multiple files whose names match in a case-insensitive
251 * fashion (problematic for some systems).
253 * Comparing names against all other names is O(n^2). We could speed
254 * it up some by sorting the entries and being smarter about what we
255 * compare against, but I'm not expecting to have enough files in a
256 * single directory to make a noticeable difference in speed.
258 * Note that sorting here is not enough to guarantee that the package
259 * contents are sorted -- subsequent updates can rearrange things.
261 status_t
validate() const;
265 String8
getPrintableSource() const;
271 DefaultKeyedVector
<String8
, sp
<AaptGroup
> > mFiles
;
272 DefaultKeyedVector
<String8
, sp
<AaptDir
> > mDirs
;
276 * All information we know about a particular symbol.
278 class AaptSymbolEntry
282 : isPublic(false), typeCode(TYPE_UNKNOWN
)
285 AaptSymbolEntry(const String8
& _name
)
286 : name(_name
), isPublic(false), typeCode(TYPE_UNKNOWN
)
289 AaptSymbolEntry(const AaptSymbolEntry
& o
)
290 : name(o
.name
), sourcePos(o
.sourcePos
), isPublic(o
.isPublic
)
291 , comment(o
.comment
), typeComment(o
.typeComment
)
292 , typeCode(o
.typeCode
), int32Val(o
.int32Val
), stringVal(o
.stringVal
)
295 AaptSymbolEntry
operator=(const AaptSymbolEntry
& o
)
297 sourcePos
= o
.sourcePos
;
298 isPublic
= o
.isPublic
;
300 typeComment
= o
.typeComment
;
301 typeCode
= o
.typeCode
;
302 int32Val
= o
.int32Val
;
303 stringVal
= o
.stringVal
;
313 String16 typeComment
;
323 // Value. May be one of these.
329 * A group of related symbols (such as indices into a string block)
330 * that have been generated from the assets.
332 class AaptSymbols
: public RefBase
336 virtual ~AaptSymbols() { }
338 status_t
addSymbol(const String8
& name
, int32_t value
, const SourcePos
& pos
) {
339 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
342 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
343 sym
.typeCode
= AaptSymbolEntry::TYPE_INT32
;
344 sym
.int32Val
= value
;
348 status_t
addStringSymbol(const String8
& name
, const String8
& value
,
349 const SourcePos
& pos
) {
350 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
353 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
354 sym
.typeCode
= AaptSymbolEntry::TYPE_STRING
;
355 sym
.stringVal
= value
;
359 status_t
makeSymbolPublic(const String8
& name
, const SourcePos
& pos
) {
360 if (!check_valid_symbol_name(name
, pos
, "symbol")) {
363 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
368 void appendComment(const String8
& name
, const String16
& comment
, const SourcePos
& pos
) {
369 if (comment
.size() <= 0) {
372 AaptSymbolEntry
& sym
= edit_symbol(name
, &pos
);
373 if (sym
.comment
.size() == 0) {
374 sym
.comment
= comment
;
376 sym
.comment
.append(String16("\n"));
377 sym
.comment
.append(comment
);
381 void appendTypeComment(const String8
& name
, const String16
& comment
) {
382 if (comment
.size() <= 0) {
385 AaptSymbolEntry
& sym
= edit_symbol(name
, NULL
);
386 if (sym
.typeComment
.size() == 0) {
387 sym
.typeComment
= comment
;
389 sym
.typeComment
.append(String16("\n"));
390 sym
.typeComment
.append(comment
);
394 sp
<AaptSymbols
> addNestedSymbol(const String8
& name
, const SourcePos
& pos
) {
395 if (!check_valid_symbol_name(name
, pos
, "nested symbol")) {
399 sp
<AaptSymbols
> sym
= mNestedSymbols
.valueFor(name
);
401 sym
= new AaptSymbols();
402 mNestedSymbols
.add(name
, sym
);
408 const KeyedVector
<String8
, AaptSymbolEntry
>& getSymbols() const
410 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getNestedSymbols() const
411 { return mNestedSymbols
; }
413 const String16
& getComment(const String8
& name
) const
414 { return get_symbol(name
).comment
; }
415 const String16
& getTypeComment(const String8
& name
) const
416 { return get_symbol(name
).typeComment
; }
419 bool check_valid_symbol_name(const String8
& symbol
, const SourcePos
& pos
, const char* label
) {
420 if (valid_symbol_name(symbol
)) {
423 pos
.error("invalid %s: '%s'\n", label
, symbol
.string());
426 AaptSymbolEntry
& edit_symbol(const String8
& symbol
, const SourcePos
* pos
) {
427 ssize_t i
= mSymbols
.indexOfKey(symbol
);
429 i
= mSymbols
.add(symbol
, AaptSymbolEntry(symbol
));
431 AaptSymbolEntry
& sym
= mSymbols
.editValueAt(i
);
432 if (pos
!= NULL
&& sym
.sourcePos
.line
< 0) {
433 sym
.sourcePos
= *pos
;
437 const AaptSymbolEntry
& get_symbol(const String8
& symbol
) const {
438 ssize_t i
= mSymbols
.indexOfKey(symbol
);
440 return mSymbols
.valueAt(i
);
445 KeyedVector
<String8
, AaptSymbolEntry
> mSymbols
;
446 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mNestedSymbols
;
447 AaptSymbolEntry mDefSymbol
;
450 class ResourceTypeSet
;
453 * Asset hierarchy being operated on.
455 class AaptAssets
: public AaptDir
458 AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false) { }
459 virtual ~AaptAssets() { }
461 const String8
& getPackage() const { return mPackage
; }
462 void setPackage(const String8
& package
) { mPackage
= package
; mSymbolsPrivatePackage
= package
; }
464 const SortedVector
<AaptGroupEntry
>& getGroupEntries() const { return mGroupEntries
; }
466 sp
<AaptFile
> addFile(const String8
& filePath
,
467 const AaptGroupEntry
& entry
,
468 const String8
& srcDir
,
469 sp
<AaptGroup
>* outGroup
,
470 const String8
& resType
);
472 void addResource(const String8
& leafName
,
474 const sp
<AaptFile
>& file
,
475 const String8
& resType
);
477 ssize_t
slurpFromArgs(Bundle
* bundle
);
479 virtual ssize_t
slurpFullTree(Bundle
* bundle
,
480 const String8
& srcDir
,
481 const AaptGroupEntry
& kind
,
482 const String8
& resType
);
484 ssize_t
slurpResourceTree(Bundle
* bundle
, const String8
& srcDir
);
485 ssize_t
slurpResourceZip(Bundle
* bundle
, const char* filename
);
487 sp
<AaptSymbols
> getSymbolsFor(const String8
& name
);
489 const DefaultKeyedVector
<String8
, sp
<AaptSymbols
> >& getSymbols() const { return mSymbols
; }
491 String8
getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage
; }
492 void setSymbolsPrivatePackage(const String8
& pkg
) { mSymbolsPrivatePackage
= pkg
; }
494 status_t
buildIncludedResources(Bundle
* bundle
);
495 status_t
addIncludedResources(const sp
<AaptFile
>& file
);
496 const ResTable
& getIncludedResources() const;
500 inline const Vector
<sp
<AaptDir
> >& resDirs() { return mDirs
; }
502 inline sp
<AaptAssets
> getOverlay() { return mOverlay
; }
503 inline void setOverlay(sp
<AaptAssets
>& overlay
) { mOverlay
= overlay
; }
505 inline KeyedVector
<String8
, sp
<ResourceTypeSet
> >* getResources() { return mRes
; }
507 setResources(KeyedVector
<String8
, sp
<ResourceTypeSet
> >* res
) { mRes
= res
; }
511 SortedVector
<AaptGroupEntry
> mGroupEntries
;
512 DefaultKeyedVector
<String8
, sp
<AaptSymbols
> > mSymbols
;
513 String8 mSymbolsPrivatePackage
;
515 Vector
<sp
<AaptDir
> > mDirs
;
517 bool mHaveIncludedAssets
;
518 AssetManager mIncludedAssets
;
520 sp
<AaptAssets
> mOverlay
;
521 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* mRes
;
524 #endif // __AAPT_ASSETS_H