blob: cb2234d860c122fed085f958d755c1df7f673154 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_RESOLVER_H
18#define AAPT_RESOLVER_H
19
20#include "Maybe.h"
21#include "Resource.h"
22#include "ResourceTable.h"
23#include "ResourceValues.h"
24
25#include <androidfw/AssetManager.h>
26#include <androidfw/ResourceTypes.h>
27#include <memory>
28#include <vector>
Adam Lesinski769de982015-04-10 19:43:55 -070029#include <unordered_set>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030
31namespace aapt {
32
33/**
34 * Resolves symbolic references (package:type/entry) into resource IDs/objects.
35 * Encapsulates the search of library sources as well as the local ResourceTable.
36 */
37class Resolver {
38public:
39 /**
40 * Creates a resolver with a local ResourceTable and an AssetManager
41 * loaded with library packages.
42 */
43 Resolver(std::shared_ptr<const ResourceTable> table,
44 std::shared_ptr<const android::AssetManager> sources);
45
46 Resolver(const Resolver&) = delete; // Not copyable.
47
48 /**
49 * Holds the result of a resource name lookup.
50 */
51 struct Entry {
52 /**
53 * The ID of the resource. ResourceId::isValid() may
54 * return false if the resource has not been assigned
55 * an ID.
56 */
57 ResourceId id;
58
59 /**
60 * If the resource is an attribute, this will point
61 * to a valid Attribute object, or else it will be
62 * nullptr.
63 */
64 const Attribute* attr;
65 };
66
67 /**
68 * Return the package to use when none is specified. This
69 * is the package name of the app being built.
70 */
71 const std::u16string& getDefaultPackage() const;
72
73 /**
74 * Returns a ResourceID if the name is found. The ResourceID
75 * may not be valid if the resource was not assigned an ID.
76 */
77 Maybe<ResourceId> findId(const ResourceName& name);
78
79 /**
80 * Returns an Entry if the name is found. Entry::attr
81 * may be nullptr if the resource is not an attribute.
82 */
83 Maybe<Entry> findAttribute(const ResourceName& name);
84
85 const android::ResTable& getResTable() const;
86
87private:
88 struct CacheEntry {
89 ResourceId id;
90 std::unique_ptr<Attribute> attr;
91 };
92
93 const CacheEntry* buildCacheEntry(const ResourceName& name);
94
95 std::shared_ptr<const ResourceTable> mTable;
96 std::shared_ptr<const android::AssetManager> mSources;
97 std::map<ResourceName, CacheEntry> mCache;
Adam Lesinski769de982015-04-10 19:43:55 -070098 std::unordered_set<std::u16string> mIncludedPackages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099};
100
101inline const std::u16string& Resolver::getDefaultPackage() const {
102 return mTable->getPackage();
103}
104
105inline const android::ResTable& Resolver::getResTable() const {
106 return mSources->getResources(false);
107}
108
109} // namespace aapt
110
111#endif // AAPT_RESOLVER_H