]>
Commit | Line | Data |
---|---|---|
a534180c TAOSP |
1 | // |
2 | // Copyright 2006 The Android Open Source Project | |
3 | // | |
4 | // Information about assets being operated on. | |
5 | // | |
6 | #ifndef __AAPT_ASSETS_H | |
7 | #define __AAPT_ASSETS_H | |
8 | ||
9 | #include <stdlib.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> | |
1f170917 | 18 | #include "ZipFile.h" |
a534180c TAOSP |
19 | |
20 | #include "Bundle.h" | |
21 | #include "SourcePos.h" | |
22 | ||
23 | using namespace android; | |
24 | ||
25 | bool valid_symbol_name(const String8& str); | |
26 | ||
27 | enum { | |
28 | AXIS_NONE = 0, | |
29 | AXIS_MCC = 1, | |
30 | AXIS_MNC, | |
31 | AXIS_LANGUAGE, | |
32 | AXIS_REGION, | |
784c6915 DH |
33 | AXIS_SCREENLAYOUTSIZE, |
34 | AXIS_SCREENLAYOUTLONG, | |
a534180c | 35 | AXIS_ORIENTATION, |
2ca01a37 TH |
36 | AXIS_UIMODETYPE, |
37 | AXIS_UIMODENIGHT, | |
a534180c TAOSP |
38 | AXIS_DENSITY, |
39 | AXIS_TOUCHSCREEN, | |
40 | AXIS_KEYSHIDDEN, | |
41 | AXIS_KEYBOARD, | |
0096feb5 | 42 | AXIS_NAVHIDDEN, |
a534180c TAOSP |
43 | AXIS_NAVIGATION, |
44 | AXIS_SCREENSIZE, | |
45 | AXIS_VERSION | |
46 | }; | |
47 | ||
48 | /** | |
49 | * This structure contains a specific variation of a single file out | |
50 | * of all the variations it can have that we can have. | |
51 | */ | |
52 | struct AaptGroupEntry | |
53 | { | |
54 | public: | |
55 | AaptGroupEntry() { } | |
56 | AaptGroupEntry(const String8& _locale, const String8& _vendor) | |
57 | : locale(_locale), vendor(_vendor) { } | |
58 | ||
59 | String8 mcc; | |
60 | String8 mnc; | |
61 | String8 locale; | |
62 | String8 vendor; | |
784c6915 DH |
63 | String8 screenLayoutSize; |
64 | String8 screenLayoutLong; | |
a534180c | 65 | String8 orientation; |
2ca01a37 TH |
66 | String8 uiModeType; |
67 | String8 uiModeNight; | |
a534180c TAOSP |
68 | String8 density; |
69 | String8 touchscreen; | |
70 | String8 keysHidden; | |
71 | String8 keyboard; | |
0096feb5 | 72 | String8 navHidden; |
a534180c TAOSP |
73 | String8 navigation; |
74 | String8 screenSize; | |
75 | String8 version; | |
76 | ||
77 | bool initFromDirName(const char* dir, String8* resType); | |
78 | ||
79 | static status_t parseNamePart(const String8& part, int* axis, uint32_t* value); | |
80 | ||
81 | static bool getMccName(const char* name, ResTable_config* out = NULL); | |
82 | static bool getMncName(const char* name, ResTable_config* out = NULL); | |
83 | static bool getLocaleName(const char* name, ResTable_config* out = NULL); | |
784c6915 DH |
84 | static bool getScreenLayoutSizeName(const char* name, ResTable_config* out = NULL); |
85 | static bool getScreenLayoutLongName(const char* name, ResTable_config* out = NULL); | |
a534180c | 86 | static bool getOrientationName(const char* name, ResTable_config* out = NULL); |
2ca01a37 TH |
87 | static bool getUiModeTypeName(const char* name, ResTable_config* out = NULL); |
88 | static bool getUiModeNightName(const char* name, ResTable_config* out = NULL); | |
a534180c TAOSP |
89 | static bool getDensityName(const char* name, ResTable_config* out = NULL); |
90 | static bool getTouchscreenName(const char* name, ResTable_config* out = NULL); | |
91 | static bool getKeysHiddenName(const char* name, ResTable_config* out = NULL); | |
92 | static bool getKeyboardName(const char* name, ResTable_config* out = NULL); | |
93 | static bool getNavigationName(const char* name, ResTable_config* out = NULL); | |
0096feb5 | 94 | static bool getNavHiddenName(const char* name, ResTable_config* out = NULL); |
a534180c TAOSP |
95 | static bool getScreenSizeName(const char* name, ResTable_config* out = NULL); |
96 | static bool getVersionName(const char* name, ResTable_config* out = NULL); | |
97 | ||
98 | int compare(const AaptGroupEntry& o) const; | |
99 | ||
100 | ResTable_config toParams() const; | |
101 | ||
102 | inline bool operator<(const AaptGroupEntry& o) const { return compare(o) < 0; } | |
103 | inline bool operator<=(const AaptGroupEntry& o) const { return compare(o) <= 0; } | |
104 | inline bool operator==(const AaptGroupEntry& o) const { return compare(o) == 0; } | |
105 | inline bool operator!=(const AaptGroupEntry& o) const { return compare(o) != 0; } | |
106 | inline bool operator>=(const AaptGroupEntry& o) const { return compare(o) >= 0; } | |
107 | inline bool operator>(const AaptGroupEntry& o) const { return compare(o) > 0; } | |
108 | ||
109 | String8 toString() const; | |
110 | String8 toDirName(const String8& resType) const; | |
111 | }; | |
112 | ||
113 | inline int compare_type(const AaptGroupEntry& lhs, const AaptGroupEntry& rhs) | |
114 | { | |
115 | return lhs.compare(rhs); | |
116 | } | |
117 | ||
118 | inline int strictly_order_type(const AaptGroupEntry& lhs, const AaptGroupEntry& rhs) | |
119 | { | |
120 | return compare_type(lhs, rhs) < 0; | |
121 | } | |
122 | ||
123 | class AaptGroup; | |
124 | ||
125 | /** | |
126 | * A single asset file we know about. | |
127 | */ | |
128 | class AaptFile : public RefBase | |
129 | { | |
130 | public: | |
131 | AaptFile(const String8& sourceFile, const AaptGroupEntry& groupEntry, | |
132 | const String8& resType) | |
133 | : mGroupEntry(groupEntry) | |
134 | , mResourceType(resType) | |
135 | , mSourceFile(sourceFile) | |
136 | , mData(NULL) | |
137 | , mDataSize(0) | |
138 | , mBufferSize(0) | |
139 | , mCompression(ZipEntry::kCompressStored) | |
140 | { | |
141 | //printf("new AaptFile created %s\n", (const char*)sourceFile); | |
142 | } | |
981d1579 MN |
143 | virtual ~AaptFile() { |
144 | free(mData); | |
145 | } | |
a534180c TAOSP |
146 | |
147 | const String8& getPath() const { return mPath; } | |
148 | const AaptGroupEntry& getGroupEntry() const { return mGroupEntry; } | |
149 | ||
150 | // Data API. If there is data attached to the file, | |
151 | // getSourceFile() is not used. | |
152 | bool hasData() const { return mData != NULL; } | |
153 | const void* getData() const { return mData; } | |
154 | size_t getSize() const { return mDataSize; } | |
155 | void* editData(size_t size); | |
156 | void* editData(size_t* outSize = NULL); | |
157 | void* padData(size_t wordSize); | |
158 | status_t writeData(const void* data, size_t size); | |
159 | void clearData(); | |
160 | ||
161 | const String8& getResourceType() const { return mResourceType; } | |
162 | ||
163 | // File API. If the file does not hold raw data, this is | |
164 | // a full path to a file on the filesystem that holds its data. | |
165 | const String8& getSourceFile() const { return mSourceFile; } | |
166 | ||
167 | String8 getPrintableSource() const; | |
168 | ||
169 | // Desired compression method, as per utils/ZipEntry.h. For example, | |
170 | // no compression is ZipEntry::kCompressStored. | |
171 | int getCompressionMethod() const { return mCompression; } | |
172 | void setCompressionMethod(int c) { mCompression = c; } | |
173 | private: | |
174 | friend class AaptGroup; | |
175 | ||
176 | String8 mPath; | |
177 | AaptGroupEntry mGroupEntry; | |
178 | String8 mResourceType; | |
179 | String8 mSourceFile; | |
180 | void* mData; | |
181 | size_t mDataSize; | |
182 | size_t mBufferSize; | |
183 | int mCompression; | |
184 | }; | |
185 | ||
186 | /** | |
187 | * A group of related files (the same file, with different | |
188 | * vendor/locale variations). | |
189 | */ | |
190 | class AaptGroup : public RefBase | |
191 | { | |
192 | public: | |
193 | AaptGroup(const String8& leaf, const String8& path) | |
194 | : mLeaf(leaf), mPath(path) { } | |
195 | virtual ~AaptGroup() { } | |
196 | ||
197 | const String8& getLeaf() const { return mLeaf; } | |
198 | ||
199 | // Returns the relative path after the AaptGroupEntry dirs. | |
200 | const String8& getPath() const { return mPath; } | |
201 | ||
202 | const DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> >& getFiles() const | |
203 | { return mFiles; } | |
204 | ||
205 | status_t addFile(const sp<AaptFile>& file); | |
206 | void removeFile(size_t index); | |
207 | ||
208 | void print() const; | |
209 | ||
210 | String8 getPrintableSource() const; | |
211 | ||
212 | private: | |
213 | String8 mLeaf; | |
214 | String8 mPath; | |
215 | ||
216 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > mFiles; | |
217 | }; | |
218 | ||
219 | /** | |
220 | * A single directory of assets, which can contain for files and other | |
221 | * sub-directories. | |
222 | */ | |
223 | class AaptDir : public RefBase | |
224 | { | |
225 | public: | |
226 | AaptDir(const String8& leaf, const String8& path) | |
227 | : mLeaf(leaf), mPath(path) { } | |
228 | virtual ~AaptDir() { } | |
229 | ||
230 | const String8& getLeaf() const { return mLeaf; } | |
231 | ||
232 | const String8& getPath() const { return mPath; } | |
233 | ||
234 | const DefaultKeyedVector<String8, sp<AaptGroup> >& getFiles() const { return mFiles; } | |
235 | const DefaultKeyedVector<String8, sp<AaptDir> >& getDirs() const { return mDirs; } | |
236 | ||
237 | status_t addFile(const String8& name, const sp<AaptGroup>& file); | |
238 | status_t addDir(const String8& name, const sp<AaptDir>& dir); | |
239 | ||
240 | sp<AaptDir> makeDir(const String8& name); | |
241 | ||
242 | void removeFile(const String8& name); | |
243 | void removeDir(const String8& name); | |
244 | ||
245 | status_t renameFile(const sp<AaptFile>& file, const String8& newName); | |
246 | ||
247 | status_t addLeafFile(const String8& leafName, | |
248 | const sp<AaptFile>& file); | |
249 | ||
250 | virtual ssize_t slurpFullTree(Bundle* bundle, | |
251 | const String8& srcDir, | |
252 | const AaptGroupEntry& kind, | |
253 | const String8& resType); | |
254 | ||
255 | /* | |
256 | * Perform some sanity checks on the names of files and directories here. | |
257 | * In particular: | |
258 | * - Check for illegal chars in filenames. | |
259 | * - Check filename length. | |
260 | * - Check for presence of ".gz" and non-".gz" copies of same file. | |
261 | * - Check for multiple files whose names match in a case-insensitive | |
262 | * fashion (problematic for some systems). | |
263 | * | |
264 | * Comparing names against all other names is O(n^2). We could speed | |
265 | * it up some by sorting the entries and being smarter about what we | |
266 | * compare against, but I'm not expecting to have enough files in a | |
267 | * single directory to make a noticeable difference in speed. | |
268 | * | |
269 | * Note that sorting here is not enough to guarantee that the package | |
270 | * contents are sorted -- subsequent updates can rearrange things. | |
271 | */ | |
272 | status_t validate() const; | |
273 | ||
274 | void print() const; | |
275 | ||
276 | String8 getPrintableSource() const; | |
277 | ||
278 | private: | |
279 | String8 mLeaf; | |
280 | String8 mPath; | |
281 | ||
282 | DefaultKeyedVector<String8, sp<AaptGroup> > mFiles; | |
283 | DefaultKeyedVector<String8, sp<AaptDir> > mDirs; | |
284 | }; | |
285 | ||
286 | /** | |
287 | * All information we know about a particular symbol. | |
288 | */ | |
289 | class AaptSymbolEntry | |
290 | { | |
291 | public: | |
292 | AaptSymbolEntry() | |
293 | : isPublic(false), typeCode(TYPE_UNKNOWN) | |
294 | { | |
295 | } | |
296 | AaptSymbolEntry(const String8& _name) | |
297 | : name(_name), isPublic(false), typeCode(TYPE_UNKNOWN) | |
298 | { | |
299 | } | |
300 | AaptSymbolEntry(const AaptSymbolEntry& o) | |
301 | : name(o.name), sourcePos(o.sourcePos), isPublic(o.isPublic) | |
302 | , comment(o.comment), typeComment(o.typeComment) | |
303 | , typeCode(o.typeCode), int32Val(o.int32Val), stringVal(o.stringVal) | |
304 | { | |
305 | } | |
306 | AaptSymbolEntry operator=(const AaptSymbolEntry& o) | |
307 | { | |
308 | sourcePos = o.sourcePos; | |
309 | isPublic = o.isPublic; | |
310 | comment = o.comment; | |
311 | typeComment = o.typeComment; | |
312 | typeCode = o.typeCode; | |
313 | int32Val = o.int32Val; | |
314 | stringVal = o.stringVal; | |
315 | return *this; | |
316 | } | |
317 | ||
318 | const String8 name; | |
319 | ||
320 | SourcePos sourcePos; | |
321 | bool isPublic; | |
322 | ||
323 | String16 comment; | |
324 | String16 typeComment; | |
325 | ||
326 | enum { | |
327 | TYPE_UNKNOWN = 0, | |
328 | TYPE_INT32, | |
329 | TYPE_STRING | |
330 | }; | |
331 | ||
332 | int typeCode; | |
333 | ||
334 | // Value. May be one of these. | |
335 | int32_t int32Val; | |
336 | String8 stringVal; | |
337 | }; | |
338 | ||
339 | /** | |
340 | * A group of related symbols (such as indices into a string block) | |
341 | * that have been generated from the assets. | |
342 | */ | |
343 | class AaptSymbols : public RefBase | |
344 | { | |
345 | public: | |
346 | AaptSymbols() { } | |
347 | virtual ~AaptSymbols() { } | |
348 | ||
349 | status_t addSymbol(const String8& name, int32_t value, const SourcePos& pos) { | |
350 | if (!check_valid_symbol_name(name, pos, "symbol")) { | |
351 | return BAD_VALUE; | |
352 | } | |
353 | AaptSymbolEntry& sym = edit_symbol(name, &pos); | |
354 | sym.typeCode = AaptSymbolEntry::TYPE_INT32; | |
355 | sym.int32Val = value; | |
356 | return NO_ERROR; | |
357 | } | |
358 | ||
359 | status_t addStringSymbol(const String8& name, const String8& value, | |
360 | const SourcePos& pos) { | |
361 | if (!check_valid_symbol_name(name, pos, "symbol")) { | |
362 | return BAD_VALUE; | |
363 | } | |
364 | AaptSymbolEntry& sym = edit_symbol(name, &pos); | |
365 | sym.typeCode = AaptSymbolEntry::TYPE_STRING; | |
366 | sym.stringVal = value; | |
367 | return NO_ERROR; | |
368 | } | |
369 | ||
370 | status_t makeSymbolPublic(const String8& name, const SourcePos& pos) { | |
371 | if (!check_valid_symbol_name(name, pos, "symbol")) { | |
372 | return BAD_VALUE; | |
373 | } | |
374 | AaptSymbolEntry& sym = edit_symbol(name, &pos); | |
375 | sym.isPublic = true; | |
376 | return NO_ERROR; | |
377 | } | |
378 | ||
379 | void appendComment(const String8& name, const String16& comment, const SourcePos& pos) { | |
380 | if (comment.size() <= 0) { | |
381 | return; | |
382 | } | |
383 | AaptSymbolEntry& sym = edit_symbol(name, &pos); | |
384 | if (sym.comment.size() == 0) { | |
385 | sym.comment = comment; | |
386 | } else { | |
387 | sym.comment.append(String16("\n")); | |
388 | sym.comment.append(comment); | |
389 | } | |
390 | } | |
391 | ||
392 | void appendTypeComment(const String8& name, const String16& comment) { | |
393 | if (comment.size() <= 0) { | |
394 | return; | |
395 | } | |
396 | AaptSymbolEntry& sym = edit_symbol(name, NULL); | |
397 | if (sym.typeComment.size() == 0) { | |
398 | sym.typeComment = comment; | |
399 | } else { | |
400 | sym.typeComment.append(String16("\n")); | |
401 | sym.typeComment.append(comment); | |
402 | } | |
403 | } | |
404 | ||
405 | sp<AaptSymbols> addNestedSymbol(const String8& name, const SourcePos& pos) { | |
406 | if (!check_valid_symbol_name(name, pos, "nested symbol")) { | |
407 | return NULL; | |
408 | } | |
409 | ||
410 | sp<AaptSymbols> sym = mNestedSymbols.valueFor(name); | |
411 | if (sym == NULL) { | |
412 | sym = new AaptSymbols(); | |
413 | mNestedSymbols.add(name, sym); | |
414 | } | |
415 | ||
416 | return sym; | |
417 | } | |
418 | ||
419 | const KeyedVector<String8, AaptSymbolEntry>& getSymbols() const | |
420 | { return mSymbols; } | |
421 | const DefaultKeyedVector<String8, sp<AaptSymbols> >& getNestedSymbols() const | |
422 | { return mNestedSymbols; } | |
423 | ||
424 | const String16& getComment(const String8& name) const | |
425 | { return get_symbol(name).comment; } | |
426 | const String16& getTypeComment(const String8& name) const | |
427 | { return get_symbol(name).typeComment; } | |
428 | ||
429 | private: | |
430 | bool check_valid_symbol_name(const String8& symbol, const SourcePos& pos, const char* label) { | |
431 | if (valid_symbol_name(symbol)) { | |
432 | return true; | |
433 | } | |
434 | pos.error("invalid %s: '%s'\n", label, symbol.string()); | |
435 | return false; | |
436 | } | |
437 | AaptSymbolEntry& edit_symbol(const String8& symbol, const SourcePos* pos) { | |
438 | ssize_t i = mSymbols.indexOfKey(symbol); | |
439 | if (i < 0) { | |
440 | i = mSymbols.add(symbol, AaptSymbolEntry(symbol)); | |
441 | } | |
442 | AaptSymbolEntry& sym = mSymbols.editValueAt(i); | |
443 | if (pos != NULL && sym.sourcePos.line < 0) { | |
444 | sym.sourcePos = *pos; | |
445 | } | |
446 | return sym; | |
447 | } | |
448 | const AaptSymbolEntry& get_symbol(const String8& symbol) const { | |
449 | ssize_t i = mSymbols.indexOfKey(symbol); | |
450 | if (i >= 0) { | |
451 | return mSymbols.valueAt(i); | |
452 | } | |
453 | return mDefSymbol; | |
454 | } | |
455 | ||
456 | KeyedVector<String8, AaptSymbolEntry> mSymbols; | |
457 | DefaultKeyedVector<String8, sp<AaptSymbols> > mNestedSymbols; | |
458 | AaptSymbolEntry mDefSymbol; | |
459 | }; | |
460 | ||
981d1579 MN |
461 | class ResourceTypeSet : public RefBase, |
462 | public KeyedVector<String8,sp<AaptGroup> > | |
463 | { | |
464 | public: | |
465 | ResourceTypeSet(); | |
466 | }; | |
467 | ||
a534180c TAOSP |
468 | |
469 | /** | |
470 | * Asset hierarchy being operated on. | |
471 | */ | |
472 | class AaptAssets : public AaptDir | |
473 | { | |
474 | public: | |
981d1579 MN |
475 | AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL) { } |
476 | virtual ~AaptAssets() { delete mRes; } | |
a534180c TAOSP |
477 | |
478 | const String8& getPackage() const { return mPackage; } | |
479 | void setPackage(const String8& package) { mPackage = package; mSymbolsPrivatePackage = package; } | |
480 | ||
481 | const SortedVector<AaptGroupEntry>& getGroupEntries() const { return mGroupEntries; } | |
482 | ||
483 | sp<AaptFile> addFile(const String8& filePath, | |
484 | const AaptGroupEntry& entry, | |
485 | const String8& srcDir, | |
486 | sp<AaptGroup>* outGroup, | |
487 | const String8& resType); | |
488 | ||
489 | void addResource(const String8& leafName, | |
490 | const String8& path, | |
491 | const sp<AaptFile>& file, | |
492 | const String8& resType); | |
493 | ||
a76631e8 DH |
494 | void addGroupEntry(const AaptGroupEntry& entry) { mGroupEntries.add(entry); } |
495 | ||
a534180c TAOSP |
496 | ssize_t slurpFromArgs(Bundle* bundle); |
497 | ||
498 | virtual ssize_t slurpFullTree(Bundle* bundle, | |
499 | const String8& srcDir, | |
500 | const AaptGroupEntry& kind, | |
501 | const String8& resType); | |
502 | ||
503 | ssize_t slurpResourceTree(Bundle* bundle, const String8& srcDir); | |
504 | ssize_t slurpResourceZip(Bundle* bundle, const char* filename); | |
505 | ||
506 | sp<AaptSymbols> getSymbolsFor(const String8& name); | |
507 | ||
508 | const DefaultKeyedVector<String8, sp<AaptSymbols> >& getSymbols() const { return mSymbols; } | |
509 | ||
510 | String8 getSymbolsPrivatePackage() const { return mSymbolsPrivatePackage; } | |
511 | void setSymbolsPrivatePackage(const String8& pkg) { mSymbolsPrivatePackage = pkg; } | |
512 | ||
513 | status_t buildIncludedResources(Bundle* bundle); | |
514 | status_t addIncludedResources(const sp<AaptFile>& file); | |
515 | const ResTable& getIncludedResources() const; | |
516 | ||
517 | void print() const; | |
518 | ||
519 | inline const Vector<sp<AaptDir> >& resDirs() { return mDirs; } | |
6648ff78 | 520 | sp<AaptDir> resDir(const String8& name); |
a534180c TAOSP |
521 | |
522 | inline sp<AaptAssets> getOverlay() { return mOverlay; } | |
523 | inline void setOverlay(sp<AaptAssets>& overlay) { mOverlay = overlay; } | |
524 | ||
525 | inline KeyedVector<String8, sp<ResourceTypeSet> >* getResources() { return mRes; } | |
526 | inline void | |
981d1579 | 527 | setResources(KeyedVector<String8, sp<ResourceTypeSet> >* res) { delete mRes; mRes = res; } |
a534180c TAOSP |
528 | |
529 | private: | |
530 | String8 mPackage; | |
531 | SortedVector<AaptGroupEntry> mGroupEntries; | |
532 | DefaultKeyedVector<String8, sp<AaptSymbols> > mSymbols; | |
533 | String8 mSymbolsPrivatePackage; | |
534 | ||
535 | Vector<sp<AaptDir> > mDirs; | |
536 | ||
537 | bool mHaveIncludedAssets; | |
538 | AssetManager mIncludedAssets; | |
539 | ||
540 | sp<AaptAssets> mOverlay; | |
541 | KeyedVector<String8, sp<ResourceTypeSet> >* mRes; | |
542 | }; | |
543 | ||
544 | #endif // __AAPT_ASSETS_H | |
545 |