diff options
| author | 2017-01-16 19:11:19 -0800 | |
|---|---|---|
| committer | 2017-02-08 06:04:52 -0800 | |
| commit | 929d6517dfd338f0d481dbe6587643d5aef27ec6 (patch) | |
| tree | b80a93b93946bd4e982b9f4abd97a4c3aa43feb3 /libs/androidfw/include | |
| parent | c270de85cc0c398d9ce165592908d2740219a708 (diff) | |
AssetManager2: Add GetResourceId
Add ability to lookup a resource by name.
Test: make libandroidfw_tests
Change-Id: I262ba5ce4c9892458226fbdb44cf21f9877fb92d
Diffstat (limited to 'libs/androidfw/include')
| -rw-r--r-- | libs/androidfw/include/androidfw/LoadedArsc.h | 7 | ||||
| -rw-r--r-- | libs/androidfw/include/androidfw/ResourceUtils.h | 60 | ||||
| -rw-r--r-- | libs/androidfw/include/androidfw/StringPiece.h | 10 | ||||
| -rw-r--r-- | libs/androidfw/include/androidfw/Util.h | 23 |
4 files changed, 77 insertions, 23 deletions
diff --git a/libs/androidfw/include/androidfw/LoadedArsc.h b/libs/androidfw/include/androidfw/LoadedArsc.h index 91a7cb7f45f9..f30b158084eb 100644 --- a/libs/androidfw/include/androidfw/LoadedArsc.h +++ b/libs/androidfw/include/androidfw/LoadedArsc.h @@ -101,6 +101,13 @@ class LoadedPackage { // before being inserted into the set. This may cause some equivalent locales to de-dupe. void CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const; + // Finds the entry with the specified type name and entry name. The names are in UTF-16 because + // the underlying ResStringPool API expects this. For now this is acceptable, but since + // the default policy in AAPT2 is to build UTF-8 string pools, this needs to change. + // Returns a partial resource ID, with the package ID left as 0x00. The caller is responsible + // for patching the correct package ID to the resource ID. + uint32_t FindEntryByName(const std::u16string& type_name, const std::u16string& entry_name) const; + private: DISALLOW_COPY_AND_ASSIGN(LoadedPackage); diff --git a/libs/androidfw/include/androidfw/ResourceUtils.h b/libs/androidfw/include/androidfw/ResourceUtils.h new file mode 100644 index 000000000000..6bf7c2438797 --- /dev/null +++ b/libs/androidfw/include/androidfw/ResourceUtils.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2017 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. + */ + +#ifndef ANDROIDFW_RESOURCEUTILS_H +#define ANDROIDFW_RESOURCEUTILS_H + +#include "androidfw/StringPiece.h" + +namespace android { + +// Extracts the package, type, and name from a string of the format: [[package:]type/]name +// Validation must be performed on each extracted piece. +// Returns false if there was a syntax error. +bool ExtractResourceName(const StringPiece& str, StringPiece* out_package, StringPiece* out_type, + StringPiece* out_entry); + +inline uint32_t fix_package_id(uint32_t resid, uint8_t package_id) { + return resid | (static_cast<uint32_t>(package_id) << 24); +} + +inline uint8_t get_package_id(uint32_t resid) { + return static_cast<uint8_t>((resid >> 24) & 0x000000ffu); +} + +// The type ID is 1-based, so if the returned value is 0 it is invalid. +inline uint8_t get_type_id(uint32_t resid) { + return static_cast<uint8_t>((resid >> 16) & 0x000000ffu); +} + +inline uint16_t get_entry_id(uint32_t resid) { return static_cast<uint16_t>(resid & 0x0000ffffu); } + +inline bool is_internal_resid(uint32_t resid) { + return (resid & 0xffff0000u) != 0 && (resid & 0x00ff0000u) == 0; +} + +inline bool is_valid_resid(uint32_t resid) { + return (resid & 0x00ff0000u) != 0 && (resid & 0xff000000u) != 0; +} + +inline uint32_t make_resid(uint8_t package_id, uint8_t type_id, uint16_t entry_id) { + return (static_cast<uint32_t>(package_id) << 24) | (static_cast<uint32_t>(type_id) << 16) | + entry_id; +} + +} // namespace android + +#endif /* ANDROIDFW_RESOURCEUTILS_H */ diff --git a/libs/androidfw/include/androidfw/StringPiece.h b/libs/androidfw/include/androidfw/StringPiece.h index c9effd1a5112..8f6824b5322c 100644 --- a/libs/androidfw/include/androidfw/StringPiece.h +++ b/libs/androidfw/include/androidfw/StringPiece.h @@ -257,6 +257,16 @@ inline typename BasicStringPiece<TChar>::const_iterator BasicStringPiece<TChar>: return data_ + length_; } +template <typename TChar> +inline bool operator==(const TChar* lhs, const BasicStringPiece<TChar>& rhs) { + return BasicStringPiece<TChar>(lhs) == rhs; +} + +template <typename TChar> +inline bool operator!=(const TChar* lhs, const BasicStringPiece<TChar>& rhs) { + return BasicStringPiece<TChar>(lhs) != rhs; +} + inline ::std::ostream& operator<<(::std::ostream& out, const BasicStringPiece<char>& str) { return out.write(str.data(), str.size()); } diff --git a/libs/androidfw/include/androidfw/Util.h b/libs/androidfw/include/androidfw/Util.h index 96b42bf45a08..3950cf240a9e 100644 --- a/libs/androidfw/include/androidfw/Util.h +++ b/libs/androidfw/include/androidfw/Util.h @@ -106,29 +106,6 @@ class unique_cptr { pointer ptr_; }; -inline uint32_t fix_package_id(uint32_t resid, uint8_t package_id) { - return resid | (static_cast<uint32_t>(package_id) << 24); -} - -inline uint8_t get_package_id(uint32_t resid) { - return static_cast<uint8_t>((resid >> 24) & 0x000000ffu); -} - -// The type ID is 1-based, so if the returned value is 0 it is invalid. -inline uint8_t get_type_id(uint32_t resid) { - return static_cast<uint8_t>((resid >> 16) & 0x000000ffu); -} - -inline uint16_t get_entry_id(uint32_t resid) { return static_cast<uint16_t>(resid & 0x0000ffffu); } - -inline bool is_internal_resid(uint32_t resid) { - return (resid & 0xffff0000u) != 0 && (resid & 0x00ff0000u) == 0; -} - -inline bool is_valid_resid(uint32_t resid) { - return (resid & 0x00ff0000u) != 0 && (resid & 0xff000000u) != 0; -} - void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out); } // namespace util |