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