• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include "idmap2/ResourceUtils.h"
18 
19 #include <memory>
20 #include <string>
21 
22 #include "androidfw/StringPiece.h"
23 #include "androidfw/Util.h"
24 #include "idmap2/Result.h"
25 
26 using android::StringPiece16;
27 using android::idmap2::Result;
28 using android::util::Utf16ToUtf8;
29 
30 namespace android::idmap2::utils {
31 
IsReference(uint8_t data_type)32 bool IsReference(uint8_t data_type) {
33   return data_type == Res_value::TYPE_REFERENCE || data_type == Res_value::TYPE_DYNAMIC_REFERENCE;
34 }
35 
DataTypeToString(uint8_t data_type)36 StringPiece 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 
ResToTypeEntryName(const AssetManager2 & am,uint32_t resid)70 Result<std::string> ResToTypeEntryName(const AssetManager2& am, uint32_t resid) {
71   const auto name = am.GetResourceName(resid);
72   if (!name.has_value()) {
73     return Error("no resource 0x%08x in asset manager", resid);
74   }
75   std::string out;
76   if (name->type != nullptr) {
77     out.append(name->type, name->type_len);
78   } else {
79     out += Utf16ToUtf8(StringPiece16(name->type16, name->type_len));
80   }
81   out.append("/");
82   if (name->entry != nullptr) {
83     out.append(name->entry, name->entry_len);
84   } else {
85     out += Utf16ToUtf8(StringPiece16(name->entry16, name->entry_len));
86   }
87   return out;
88 }
89 
90 }  // namespace android::idmap2::utils
91