blob: 32c04d2a6caba19b39d58fda3f31aa90b37efa92 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/ResourceUtils.h"
18
Ryan Mitchella3628462019-01-14 12:19:40 -080019#include <memory>
Mårten Kongstad1195a6b2021-05-11 12:57:01 +000020#include <string>
Mårten Kongstad02751232018-04-27 13:16:32 +020021
22#include "androidfw/StringPiece.h"
23#include "androidfw/Util.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010024#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020025
26using android::StringPiece16;
Ryan Mitchella3628462019-01-14 12:19:40 -080027using android::idmap2::Result;
Mårten Kongstad02751232018-04-27 13:16:32 +020028using android::util::Utf16ToUtf8;
29
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010030namespace android::idmap2::utils {
Mårten Kongstad02751232018-04-27 13:16:32 +020031
Ryan Mitchell5035d662020-01-22 13:19:41 -080032bool IsReference(uint8_t data_type) {
33 return data_type == Res_value::TYPE_REFERENCE || data_type == Res_value::TYPE_DYNAMIC_REFERENCE;
34}
35
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070036StringPiece DataTypeToString(uint8_t data_type) {
37 switch (data_type) {
38 case Res_value::TYPE_NULL:
39 return "null";
40 case Res_value::TYPE_REFERENCE:
41 return "reference";
42 case Res_value::TYPE_ATTRIBUTE:
43 return "attribute";
44 case Res_value::TYPE_STRING:
45 return "string";
46 case Res_value::TYPE_FLOAT:
47 return "float";
48 case Res_value::TYPE_DIMENSION:
49 return "dimension";
50 case Res_value::TYPE_FRACTION:
51 return "fraction";
52 case Res_value::TYPE_DYNAMIC_REFERENCE:
53 return "reference (dynamic)";
54 case Res_value::TYPE_DYNAMIC_ATTRIBUTE:
55 return "attribute (dynamic)";
56 case Res_value::TYPE_INT_DEC:
57 case Res_value::TYPE_INT_HEX:
58 return "integer";
59 case Res_value::TYPE_INT_BOOLEAN:
60 return "boolean";
61 case Res_value::TYPE_INT_COLOR_ARGB8:
62 case Res_value::TYPE_INT_COLOR_RGB8:
63 case Res_value::TYPE_INT_COLOR_RGB4:
64 return "color";
65 default:
66 return "unknown";
67 }
68}
69
Ryan Mitchellcd965a32019-09-18 14:52:45 -070070Result<std::string> ResToTypeEntryName(const AssetManager2& am, uint32_t resid) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000071 const auto name = am.GetResourceName(resid);
72 if (!name.has_value()) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010073 return Error("no resource 0x%08x in asset manager", resid);
Mårten Kongstad02751232018-04-27 13:16:32 +020074 }
75 std::string out;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000076 if (name->type != nullptr) {
77 out.append(name->type, name->type_len);
Mårten Kongstad02751232018-04-27 13:16:32 +020078 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000079 out += Utf16ToUtf8(StringPiece16(name->type16, name->type_len));
Mårten Kongstad02751232018-04-27 13:16:32 +020080 }
81 out.append("/");
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000082 if (name->entry != nullptr) {
83 out.append(name->entry, name->entry_len);
Mårten Kongstad02751232018-04-27 13:16:32 +020084 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000085 out += Utf16ToUtf8(StringPiece16(name->entry16, name->entry_len));
Mårten Kongstad02751232018-04-27 13:16:32 +020086 }
Mårten Kongstad49d835d2019-01-31 10:50:48 +010087 return out;
Mårten Kongstad02751232018-04-27 13:16:32 +020088}
89
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010090} // namespace android::idmap2::utils