1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "res_desc.h"
16
17 #include <cstdlib>
18
19 #include "hilog_wrapper.h"
20 #if defined(__WINNT__)
21 #include <cstring>
22 #else
23 #include "securec.h"
24 #endif
25 #include "utils/errors.h"
26 #include "utils/string_utils.h"
27 #include "utils/utils.h"
28 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
29 #include "parameters.h"
30 #endif
31
32 namespace OHOS {
33 namespace Global {
34 namespace Resource {
35 const std::string PROPERTY_DEVICE_TYPE = "const.product.devicetype";
36 const std::string PROPERTY_DEVICE_TYPE_DEFAULT = "default";
GetScreenDensityStr() const37 std::string KeyParam::GetScreenDensityStr() const
38 {
39 std::string ret("not_screen_density");
40 if (type_ == KeyType::SCREEN_DENSITY) {
41 switch (value_) {
42 case ScreenDensity::SCREEN_DENSITY_SDPI:
43 ret = std::string(RE_120_STR);
44 break;
45 case ScreenDensity::SCREEN_DENSITY_MDPI:
46 ret = std::string(RE_160_STR);
47 break;
48 case ScreenDensity::SCREEN_DENSITY_LDPI:
49 ret = std::string(RE_240_STR);
50 break;
51 case ScreenDensity::SCREEN_DENSITY_XLDPI:
52 ret = std::string(RE_320_STR);
53 break;
54 case ScreenDensity::SCREEN_DENSITY_XXLDPI:
55 ret = std::string(RE_480_STR);
56 break;
57 case ScreenDensity::SCREEN_DENSITY_XXXLDPI:
58 ret = std::string(RE_640_STR);
59 break;
60 default:
61 break;
62 }
63 }
64 return ret;
65 }
GetDeviceTypeStr() const66 std::string KeyParam::GetDeviceTypeStr() const
67 {
68 std::string ret("not_device_type");
69 if (type_ == KeyType::DEVICETYPE) {
70 switch (value_) {
71 case DeviceType::DEVICE_PHONE:
72 ret = std::string(PHONE_STR);
73 break;
74 case DeviceType::DEVICE_TABLET:
75 ret = std::string(TABLET_STR);
76 break;
77 case DeviceType::DEVICE_CAR:
78 ret = std::string(CAR_STR);
79 break;
80 case DeviceType::DEVICE_PAD:
81 ret = std::string(PAD_STR);
82 break;
83 case DeviceType::DEVICE_TV:
84 ret = std::string(TV_STR);
85 break;
86 case DeviceType::DEVICE_WEARABLE:
87 ret = std::string(WEARABLE_STR);
88 break;
89 case DeviceType::DEVICE_TWOINONE:
90 ret = std::string(TWOINONE_STR);
91 break;
92 default:
93 break;
94 }
95 }
96 return ret;
97 }
98
GetColorModeStr() const99 std::string KeyParam::GetColorModeStr() const
100 {
101 std::string ret("not_color_mode");
102 if (type_ == KeyType::COLORMODE) {
103 switch (value_) {
104 case ColorMode::DARK:
105 ret = std::string(DARK_STR);
106 break;
107 case ColorMode::LIGHT:
108 ret = std::string(LIGHT_STR);
109 break;
110 default:
111 break;
112 }
113 }
114 return ret;
115 }
116
GetInputDeviceStr() const117 std::string KeyParam::GetInputDeviceStr() const
118 {
119 std::string ret("not_input_device");
120 if (type_ == KeyType::INPUTDEVICE) {
121 if (value_ == InputDevice::INPUTDEVICE_POINTINGDEVICE) {
122 ret = std::string(POINTING_DEVICE_STR);
123 }
124 }
125 return ret;
126 }
127
GetMccStr() const128 std::string KeyParam::GetMccStr() const
129 {
130 std::string ret("not_mcc");
131 if (type_ == KeyType::MCC) {
132 ret = std::string("mcc");
133 }
134 return ret;
135 }
136
GetMncStr() const137 std::string KeyParam::GetMncStr() const
138 {
139 std::string ret("not_mnc");
140 if (type_ == KeyType::MNC) {
141 ret = std::string("mnc");
142 }
143 return ret;
144 }
145
ConvertToStr() const146 const std::string KeyParam::ConvertToStr() const
147 {
148 if ((type_ == KeyType::LANGUAGES) || (type_ == KeyType::REGION) || (type_ == KeyType::SCRIPT)) {
149 char tmp[4];
150 char tmp2[5];
151 errno_t eret = memcpy_s(tmp, sizeof(tmp), &value_, sizeof(value_));
152 if (eret != OK) {
153 RESMGR_HILOGE(RESMGR_TAG, "memcpy_s error : %d", eret);
154 return std::string();
155 }
156 int j = 0;
157 // 4 means langauges/region/script key value max length
158 for (int i = 0; i < 4; ++i) {
159 if (tmp[3 - i]) { // 3 means reverse temp value to temp2
160 tmp2[j++] = tmp[3 - i]; // 3 means reverse temp value to temp2
161 }
162 }
163 tmp2[j] = '\0';
164 return std::string(tmp2);
165 }
166 if (type_ == KeyType::DIRECTION) {
167 return std::string((value_ == 0) ? VERTICAL : HORIZONTAL);
168 }
169 if (type_ == KeyType::DEVICETYPE) {
170 return GetDeviceTypeStr();
171 }
172 if (type_ == KeyType::COLORMODE) {
173 return GetColorModeStr();
174 }
175 if (type_ == KeyType::INPUTDEVICE) {
176 return GetInputDeviceStr();
177 }
178 if (type_ == KeyType::MCC) {
179 return GetMccStr();
180 }
181 if (type_ == KeyType::MNC) {
182 return GetMncStr();
183 }
184 if (type_ == KeyType::SCREEN_DENSITY) {
185 return GetScreenDensityStr();
186 }
187 return std::string();
188 }
189
ToString() const190 std::string KeyParam::ToString() const
191 {
192 std::string ret = FormatString("[type:%d, value:%u", type_, value_);
193 if (str_.length() > 0) {
194 ret.append(FormatString(", str:%s", str_.c_str()));
195 }
196 ret.append("]");
197 return ret;
198 }
199
200 // IdItem
201
202 std::map<ResType, std::string> IdItem::resTypeStrList;
203
204 bool IdItem::sInit = IdItem::Init();
205
Init()206 bool IdItem::Init()
207 {
208 resTypeStrList.insert(make_pair(ResType::STRING, std::string("string")));
209 resTypeStrList.insert(make_pair(ResType::BOOLEAN, std::string("boolean")));
210 resTypeStrList.insert(make_pair(ResType::COLOR, std::string("color")));
211 resTypeStrList.insert(make_pair(ResType::FLOAT, std::string("float")));
212 resTypeStrList.insert(make_pair(ResType::INTEGER, std::string("integer")));
213 resTypeStrList.insert(make_pair(ResType::PATTERN, std::string("pattern")));
214 resTypeStrList.insert(make_pair(ResType::THEME, std::string("theme")));
215 resTypeStrList.insert(make_pair(ResType::MEDIA, std::string("media")));
216 resTypeStrList.insert(make_pair(ResType::SYMBOL, std::string("symbol")));
217 resTypeStrList.insert(make_pair(ResType::PLURALS, std::string("plural")));
218 return true;
219 }
220
HaveParent() const221 bool IdItem::HaveParent() const
222 {
223 if (!(resType_ == THEME || resType_ == PATTERN)) {
224 return false;
225 }
226 return (values_.size() % 2 == 1); // Taking the remainder of 2 to determine the existence of a parent node
227 }
228
IsRef(const std::string & value,ResType & resType,uint32_t & id)229 bool IdItem::IsRef(const std::string &value, ResType &resType, uint32_t &id)
230 {
231 const char *it = value.c_str();
232 const char *st = it;
233 if (*st != '$') {
234 return false;
235 }
236 auto index = value.find(":");
237 if (index == std::string::npos || index < ArrayIndex::INDEX_TWO) {
238 return false;
239 }
240 std::string typeStr;
241 std::string idStr;
242 typeStr.assign(it + 1, index - 1);
243 idStr.assign(it + index + 1, value.size() - index);
244
245 unsigned long tmpId;
246 if (!Utils::convertToUnsignedLong(idStr, tmpId) || tmpId > UINT32_MAX) {
247 return false;
248 }
249 uint32_t idd = static_cast<uint32_t>(tmpId);
250 for (auto iit = resTypeStrList.begin(); iit != resTypeStrList.end(); ++iit) {
251 auto tValue = iit->second;
252 auto type = iit->first;
253 if (typeStr == tValue) {
254 id = idd;
255 resType = type;
256 return true;
257 }
258 }
259
260 return false;
261 }
262
ToString() const263 std::string IdItem::ToString() const
264 {
265 std::string ret = FormatString(
266 "[size:%u, resType:%d, id:%u, valueLen:%u, isArray:%d, name:'%s', value:",
267 size_, resType_, id_, valueLen_, isArray_, name_.c_str());
268 if (isArray_) {
269 ret.append("[");
270 for (size_t i = 0; i < values_.size(); ++i) {
271 ret.append(FormatString("'%s',", values_[i].c_str()));
272 }
273 ret.append("]");
274 } else {
275 ret.append(FormatString("'%s'", value_.c_str()));
276 }
277 ret.append("]");
278 return ret;
279 }
280
~IdParam()281 IdParam::~IdParam()
282 {}
283
ToString() const284 std::string IdParam::ToString() const
285 {
286 return FormatString("[id:%u, offset:%u, data:%s]", id_, offset_,
287 idItem_->ToString().c_str());
288 }
289
~ResId()290 ResId::~ResId()
291 {}
292
ToString() const293 std::string ResId::ToString() const
294 {
295 std::string ret = FormatString("idcount:%u, ", count_);
296 for (size_t i = 0; i < idParams_.size(); ++i) {
297 ret.append(idParams_[i]->ToString());
298 }
299 return ret;
300 }
301
~ResKey()302 ResKey::~ResKey()
303 {
304 RESMGR_HILOGD(RESMGR_TAG, "~ResKey()");
305 }
306
ToString() const307 std::string ResKey::ToString() const
308 {
309 std::string ret = FormatString("offset:%u, keyParamsCount:%u, folder path:", offset_, keyParamsCount_);
310 ret.append(resConfig_->ToString());
311 ret.append("\nid: ");
312 ret.append(resId_->ToString());
313 return ret;
314 }
315
ResDesc()316 ResDesc::ResDesc()
317 {}
318
~ResDesc()319 ResDesc::~ResDesc()
320 {
321 RESMGR_HILOGD(RESMGR_TAG, "~ResDesc()");
322 }
323
ToString() const324 std::string ResDesc::ToString() const
325 {
326 std::string ret = FormatString("keyCount:%u\n", keys_.size());
327 for (size_t i = 0; i < keys_.size(); ++i) {
328 ret.append(keys_[i]->ToString());
329 ret.append("\n");
330 }
331 return ret;
332 }
333
GetCurrentDeviceType()334 std::string ResDesc::GetCurrentDeviceType()
335 {
336 std::string deviceType;
337 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
338 deviceType = system::GetParameter(PROPERTY_DEVICE_TYPE, PROPERTY_DEVICE_TYPE_DEFAULT);
339 #endif
340 return deviceType;
341 }
342 } // namespace Resource
343 } // namespace Global
344 } // namespace OHOS
345