blob: e809bf1f4b02ebb2068fca6f651dcc1a676254c2 [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 Kongstad02751232018-04-27 13:16:32 +020020
21#include "androidfw/StringPiece.h"
22#include "androidfw/Util.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010023#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020024
25using android::StringPiece16;
Ryan Mitchella3628462019-01-14 12:19:40 -080026using android::idmap2::Result;
Mårten Kongstad02751232018-04-27 13:16:32 +020027using android::util::Utf16ToUtf8;
28
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010029namespace android::idmap2::utils {
Mårten Kongstad02751232018-04-27 13:16:32 +020030
Ryan Mitchell5035d662020-01-22 13:19:41 -080031bool IsReference(uint8_t data_type) {
32 return data_type == Res_value::TYPE_REFERENCE || data_type == Res_value::TYPE_DYNAMIC_REFERENCE;
33}
34
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070035StringPiece DataTypeToString(uint8_t data_type) {
36 switch (data_type) {
37 case Res_value::TYPE_NULL:
38 return "null";
39 case Res_value::TYPE_REFERENCE:
40 return "reference";
41 case Res_value::TYPE_ATTRIBUTE:
42 return "attribute";
43 case Res_value::TYPE_STRING:
44 return "string";
45 case Res_value::TYPE_FLOAT:
46 return "float";
47 case Res_value::TYPE_DIMENSION:
48 return "dimension";
49 case Res_value::TYPE_FRACTION:
50 return "fraction";
51 case Res_value::TYPE_DYNAMIC_REFERENCE:
52 return "reference (dynamic)";
53 case Res_value::TYPE_DYNAMIC_ATTRIBUTE:
54 return "attribute (dynamic)";
55 case Res_value::TYPE_INT_DEC:
56 case Res_value::TYPE_INT_HEX:
57 return "integer";
58 case Res_value::TYPE_INT_BOOLEAN:
59 return "boolean";
60 case Res_value::TYPE_INT_COLOR_ARGB8:
61 case Res_value::TYPE_INT_COLOR_RGB8:
62 case Res_value::TYPE_INT_COLOR_RGB4:
63 return "color";
64 default:
65 return "unknown";
66 }
67}
68
Ryan Mitchellcd965a32019-09-18 14:52:45 -070069Result<std::string> ResToTypeEntryName(const AssetManager2& am, uint32_t resid) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000070 const auto name = am.GetResourceName(resid);
71 if (!name.has_value()) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010072 return Error("no resource 0x%08x in asset manager", resid);
Mårten Kongstad02751232018-04-27 13:16:32 +020073 }
74 std::string out;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000075 if (name->type != nullptr) {
76 out.append(name->type, name->type_len);
Mårten Kongstad02751232018-04-27 13:16:32 +020077 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000078 out += Utf16ToUtf8(StringPiece16(name->type16, name->type_len));
Mårten Kongstad02751232018-04-27 13:16:32 +020079 }
80 out.append("/");
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000081 if (name->entry != nullptr) {
82 out.append(name->entry, name->entry_len);
Mårten Kongstad02751232018-04-27 13:16:32 +020083 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000084 out += Utf16ToUtf8(StringPiece16(name->entry16, name->entry_len));
Mårten Kongstad02751232018-04-27 13:16:32 +020085 }
Mårten Kongstad49d835d2019-01-31 10:50:48 +010086 return out;
Mårten Kongstad02751232018-04-27 13:16:32 +020087}
88
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010089} // namespace android::idmap2::utils