ResourceTableResolver.h revision 24aad163bc88cb10d2275385e9afc3de7f342d65
124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski/*
224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * Copyright (C) 2015 The Android Open Source Project
324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski *
424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * you may not use this file except in compliance with the License.
624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * You may obtain a copy of the License at
724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski *
824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski *
1024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * Unless required by applicable law or agreed to in writing, software
1124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
1224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * See the License for the specific language governing permissions and
1424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * limitations under the License.
1524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski */
1624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
1724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#ifndef AAPT_RESOURCE_TABLE_RESOLVER_H
1824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#define AAPT_RESOURCE_TABLE_RESOLVER_H
1924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
2024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "Maybe.h"
2124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "Resolver.h"
2224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "Resource.h"
2324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "ResourceTable.h"
2424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "ResourceValues.h"
2524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
2624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <androidfw/AssetManager.h>
2724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <androidfw/ResourceTypes.h>
2824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <memory>
2924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <vector>
3024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <unordered_set>
3124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
3224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskinamespace aapt {
3324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
3424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski/**
3524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * Encapsulates the search of library sources as well as the local ResourceTable.
3624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski */
3724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskiclass ResourceTableResolver : public IResolver {
3824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskipublic:
3924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    /**
4024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski     * Creates a resolver with a local ResourceTable and an AssetManager
4124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski     * loaded with library packages.
4224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski     */
4324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    ResourceTableResolver(std::shared_ptr<const ResourceTable> table,
4424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski                          std::shared_ptr<const android::AssetManager> sources);
4524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
4624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    ResourceTableResolver(const ResourceTableResolver&) = delete; // Not copyable.
4724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
4824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    virtual const std::u16string& getDefaultPackage() const override;
4924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
5024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    virtual Maybe<ResourceId> findId(const ResourceName& name) override;
5124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
5224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    virtual Maybe<Entry> findAttribute(const ResourceName& name) override;
5324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
5424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    virtual Maybe<ResourceName> findName(ResourceId resId) override;
5524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
5624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    const android::ResTable& getResTable() const;
5724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
5824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskiprivate:
5924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    struct CacheEntry {
6024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski        ResourceId id;
6124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski        std::unique_ptr<Attribute> attr;
6224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    };
6324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
6424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    const CacheEntry* buildCacheEntry(const ResourceName& name);
6524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
6624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    std::shared_ptr<const ResourceTable> mTable;
6724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    std::shared_ptr<const android::AssetManager> mSources;
6824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    std::map<ResourceName, CacheEntry> mCache;
6924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    std::unordered_set<std::u16string> mIncludedPackages;
7024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski};
7124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
7224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskiinline const std::u16string& ResourceTableResolver::getDefaultPackage() const {
7324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    return mTable->getPackage();
7424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski}
7524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
7624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskiinline const android::ResTable& ResourceTableResolver::getResTable() const {
7724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    return mSources->getResources(false);
7824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski}
7924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
8024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski} // namespace aapt
8124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
8224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#endif // AAPT_RESOURCE_TABLE_RESOLVER_H
83