HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Nougat 7.0
|
7.0.0_r31
下载
查看原文件
收藏
根目录
frameworks
base
tools
aapt2
proto
TableProtoDeserializer.cpp
/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ResourceTable.h" #include "ResourceUtils.h" #include "ValueVisitor.h" #include "proto/ProtoHelpers.h" #include "proto/ProtoSerialize.h" #include
namespace aapt { namespace { class ReferenceIdToNameVisitor : public ValueVisitor { public: using ValueVisitor::visit; ReferenceIdToNameVisitor(const std::map
* mapping) : mMapping(mapping) { assert(mMapping); } void visit(Reference* reference) override { if (!reference->id || !reference->id.value().isValid()) { return; } ResourceId id = reference->id.value(); auto cacheIter = mMapping->find(id); if (cacheIter != mMapping->end()) { reference->name = cacheIter->second.toResourceName(); } } private: const std::map
* mMapping; }; class PackagePbDeserializer { public: PackagePbDeserializer(const android::ResStringPool* valuePool, const android::ResStringPool* sourcePool, const android::ResStringPool* symbolPool, const Source& source, IDiagnostics* diag) : mValuePool(valuePool), mSourcePool(sourcePool), mSymbolPool(symbolPool), mSource(source), mDiag(diag) { } public: bool deserializeFromPb(const pb::Package& pbPackage, ResourceTable* table) { Maybe
id; if (pbPackage.has_package_id()) { id = static_cast
(pbPackage.package_id()); } std::map
idIndex; ResourceTablePackage* pkg = table->createPackage( util::utf8ToUtf16(pbPackage.package_name()), id); for (const pb::Type& pbType : pbPackage.types()) { const ResourceType* resType = parseResourceType(util::utf8ToUtf16(pbType.name())); if (!resType) { mDiag->error(DiagMessage(mSource) << "unknown type '" << pbType.name() << "'"); return {}; } ResourceTableType* type = pkg->findOrCreateType(*resType); for (const pb::Entry& pbEntry : pbType.entries()) { ResourceEntry* entry = type->findOrCreateEntry(util::utf8ToUtf16(pbEntry.name())); // Deserialize the symbol status (public/private with source and comments). if (pbEntry.has_symbol_status()) { const pb::SymbolStatus& pbStatus = pbEntry.symbol_status(); if (pbStatus.has_source()) { deserializeSourceFromPb(pbStatus.source(), *mSourcePool, &entry->symbolStatus.source); } if (pbStatus.has_comment()) { entry->symbolStatus.comment = util::utf8ToUtf16(pbStatus.comment()); } SymbolState visibility = deserializeVisibilityFromPb(pbStatus.visibility()); entry->symbolStatus.state = visibility; if (visibility == SymbolState::kPublic) { // This is a public symbol, we must encode the ID now if there is one. if (pbEntry.has_id()) { entry->id = static_cast
(pbEntry.id()); } if (type->symbolStatus.state != SymbolState::kPublic) { // If the type has not been made public, do so now. type->symbolStatus.state = SymbolState::kPublic; if (pbType.has_id()) { type->id = static_cast
(pbType.id()); } } } else if (visibility == SymbolState::kPrivate) { if (type->symbolStatus.state == SymbolState::kUndefined) { type->symbolStatus.state = SymbolState::kPrivate; } } } ResourceId resId(pbPackage.package_id(), pbType.id(), pbEntry.id()); if (resId.isValid()) { idIndex[resId] = ResourceNameRef(pkg->name, type->type, entry->name); } for (const pb::ConfigValue& pbConfigValue : pbEntry.config_values()) { const pb::ConfigDescription& pbConfig = pbConfigValue.config(); ConfigDescription config; if (!deserializeConfigDescriptionFromPb(pbConfig, &config)) { mDiag->error(DiagMessage(mSource) << "invalid configuration"); return {}; } ResourceConfigValue* configValue = entry->findOrCreateValue(config, pbConfig.product()); if (configValue->value) { // Duplicate config. mDiag->error(DiagMessage(mSource) << "duplicate configuration"); return {}; } configValue->value = deserializeValueFromPb(pbConfigValue.value(), config, &table->stringPool); if (!configValue->value) { return {}; } } } } ReferenceIdToNameVisitor visitor(&idIndex); visitAllValuesInPackage(pkg, &visitor); return true; } private: std::unique_ptr
deserializeItemFromPb(const pb::Item& pbItem, const ConfigDescription& config, StringPool* pool) { if (pbItem.has_ref()) { const pb::Reference& pbRef = pbItem.ref(); std::unique_ptr
ref = util::make_unique
(); if (!deserializeReferenceFromPb(pbRef, ref.get())) { return {}; } return std::move(ref); } else if (pbItem.has_prim()) { const pb::Primitive& pbPrim = pbItem.prim(); android::Res_value prim = {}; prim.dataType = static_cast
(pbPrim.type()); prim.data = pbPrim.data(); return util::make_unique
(prim); } else if (pbItem.has_id()) { return util::make_unique
(); } else if (pbItem.has_str()) { const uint32_t idx = pbItem.str().idx(); StringPiece16 str = util::getString(*mValuePool, idx); const android::ResStringPool_span* spans = mValuePool->styleAt(idx); if (spans && spans->name.index != android::ResStringPool_span::END) { StyleString styleStr = { str.toString() }; while (spans->name.index != android::ResStringPool_span::END) { styleStr.spans.push_back(Span{ util::getString(*mValuePool, spans->name.index).toString(), spans->firstChar, spans->lastChar }); spans++; } return util::make_unique
( pool->makeRef(styleStr, StringPool::Context{ 1, config })); } return util::make_unique
( pool->makeRef(str, StringPool::Context{ 1, config })); } else if (pbItem.has_raw_str()) { const uint32_t idx = pbItem.raw_str().idx(); StringPiece16 str = util::getString(*mValuePool, idx); return util::make_unique
( pool->makeRef(str, StringPool::Context{ 1, config })); } else if (pbItem.has_file()) { const uint32_t idx = pbItem.file().path_idx(); StringPiece16 str = util::getString(*mValuePool, idx); return util::make_unique
( pool->makeRef(str, StringPool::Context{ 0, config })); } else { mDiag->error(DiagMessage(mSource) << "unknown item"); } return {}; } std::unique_ptr
deserializeValueFromPb(const pb::Value& pbValue, const ConfigDescription& config, StringPool* pool) { const bool isWeak = pbValue.has_weak() ? pbValue.weak() : false; std::unique_ptr
value; if (pbValue.has_item()) { value = deserializeItemFromPb(pbValue.item(), config, pool); if (!value) { return {}; } } else if (pbValue.has_compound_value()) { const pb::CompoundValue pbCompoundValue = pbValue.compound_value(); if (pbCompoundValue.has_attr()) { const pb::Attribute& pbAttr = pbCompoundValue.attr(); std::unique_ptr
attr = util::make_unique
(isWeak); attr->typeMask = pbAttr.format_flags(); for (const pb::Attribute_Symbol& pbSymbol : pbAttr.symbols()) { Attribute::Symbol symbol; deserializeItemCommon(pbSymbol, &symbol.symbol); if (!deserializeReferenceFromPb(pbSymbol.name(), &symbol.symbol)) { return {}; } symbol.value = pbSymbol.value(); attr->symbols.push_back(std::move(symbol)); } value = std::move(attr); } else if (pbCompoundValue.has_style()) { const pb::Style& pbStyle = pbCompoundValue.style(); std::unique_ptr