1 /*
2  * Copyright (c) 2024 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 
16 #include "ohresmgr.h"
17 
18 #include <securec.h>
19 #include "drawable_descriptor/drawable_descriptor.h"
20 #include "hilog_wrapper.h"
21 #include "resource_manager_impl.h"
22 #include "rstate.h"
23 
24 using namespace OHOS::Global::Resource;
25 using namespace OHOS::Ace::Napi;
26 
27 struct NativeResourceManager {
28     std::shared_ptr<ResourceManager> resManager = nullptr;
29 };
30 
copyStringArray(char *** resultValue,uint32_t * resultLen,vector<string> tempResultValue,string apiName)31 ResourceManager_ErrorCode copyStringArray(char ***resultValue, uint32_t *resultLen,
32     vector<string> tempResultValue, string apiName)
33 {
34     size_t len = tempResultValue.size();
35     *resultValue = new char* [tempResultValue.size()];
36     if (*resultValue == nullptr) {
37         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "%{public}s malloc error", apiName.c_str());
38         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
39     }
40     for (size_t i = 0; i < len; i++) {
41         size_t strLen = tempResultValue[i].size();
42         (*resultValue)[i] = new char[strLen + 1];
43         if ((*resultValue)[i] == nullptr) {
44             RESMGR_HILOGE(RESMGR_NATIVE_TAG, "%{public}s malloc error", apiName.c_str());
45             OH_ResourceManager_ReleaseStringArray(resultValue, len);
46             return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
47         }
48         if (strncpy_s((*resultValue)[i], strLen + 1, tempResultValue[i].c_str(), strLen) != 0) {
49             RESMGR_HILOGE(RESMGR_NATIVE_TAG, "%{public}s strncpy_s error", apiName.c_str());
50             OH_ResourceManager_ReleaseStringArray(resultValue, len);
51             return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
52         }
53     }
54     *resultLen = static_cast<uint32_t>(len);
55     return ResourceManager_ErrorCode::SUCCESS;
56 }
57 
copyLocalInfo(const icu::Locale * localeInfo,ResourceManager_Configuration * configuration)58 ResourceManager_ErrorCode copyLocalInfo(const icu::Locale *localeInfo, ResourceManager_Configuration *configuration)
59 {
60     std::string locale;
61     if (localeInfo == nullptr) {
62         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
63             "failed get configuration localeInfo errorCode = %{public}d",
64             ResourceManager_ErrorCode::ERROR_CODE_SYSTEM_RES_MANAGER_GET_FAILED);
65         return ResourceManager_ErrorCode::ERROR_CODE_SYSTEM_RES_MANAGER_GET_FAILED;
66     }
67     const char *lang = localeInfo->getLanguage();
68     if (lang == nullptr) {
69         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
70             "failed get configuration language errorCode = %{public}d",
71             ResourceManager_ErrorCode::ERROR_CODE_SYSTEM_RES_MANAGER_GET_FAILED);
72         return ResourceManager_ErrorCode::ERROR_CODE_SYSTEM_RES_MANAGER_GET_FAILED;
73     }
74     locale = lang;
75     const char *script = localeInfo->getScript();
76     if (script != nullptr) {
77         locale += std::string("_") + script;
78     }
79     const char *region = localeInfo->getCountry();
80     if (region != nullptr) {
81         locale += std::string("_") + region;
82     }
83     configuration->locale = (char*)malloc(locale.size() + 1);
84     if (configuration->locale == nullptr) {
85         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "GetConfiguration malloc error");
86         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
87     }
88     if (strncpy_s(configuration->locale, locale.size() + 1, locale.c_str(), locale.size()) != 0) {
89         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "GetConfiguration locale strncpy_s error");
90         free(configuration->locale);
91         configuration->locale = nullptr;
92         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
93     }
94     return ResourceManager_ErrorCode::SUCCESS;
95 }
96 
copyString(char ** resultValue,string tempResultValue,string apiName)97 ResourceManager_ErrorCode copyString(char **resultValue, string tempResultValue, string apiName)
98 {
99     size_t len = tempResultValue.size();
100     *resultValue = (char*)malloc(tempResultValue.size() + 1);
101     if (*resultValue == nullptr) {
102         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "%{public}s malloc error", apiName.c_str());
103         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
104     }
105     if (strncpy_s(*resultValue, len + 1, tempResultValue.c_str(), len) != 0) {
106         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "%{public}s strncpy_s error", apiName.c_str());
107         free(*resultValue);
108         *resultValue = nullptr;
109         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
110     }
111     return ResourceManager_ErrorCode::SUCCESS;
112 }
113 
OH_ResourceManager_GetMediaBase64(const NativeResourceManager * mgr,uint32_t resId,char ** resultValue,uint64_t * resultLen,uint32_t density)114 ResourceManager_ErrorCode OH_ResourceManager_GetMediaBase64(const NativeResourceManager *mgr, uint32_t resId,
115     char **resultValue, uint64_t *resultLen, uint32_t density)
116 {
117     return OH_ResourceManager_GetMediaBase64Data(mgr, resId, resultValue, resultLen, density);
118 }
119 
OH_ResourceManager_GetMediaBase64Data(const NativeResourceManager * mgr,uint32_t resId,char ** resultValue,uint64_t * resultLen,uint32_t density)120 ResourceManager_ErrorCode OH_ResourceManager_GetMediaBase64Data(const NativeResourceManager *mgr, uint32_t resId,
121     char **resultValue, uint64_t *resultLen, uint32_t density)
122 {
123     if (mgr == nullptr || resultValue == nullptr || resultLen == nullptr) {
124         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
125     }
126     std::string tempResultValue;
127     RState state = mgr->resManager->GetMediaBase64DataById(resId, tempResultValue, density);
128     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
129     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
130         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
131             "failed get media base64 id = %{public}d, errorCode = %{public}d", resId, errorCode);
132         return errorCode;
133     }
134     *resultValue = (char*)malloc(tempResultValue.size() + 1);
135     if (*resultValue == nullptr) {
136         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "GetMediaBase64 malloc error");
137         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
138     }
139     if (strncpy_s(*resultValue, tempResultValue.size() + 1, tempResultValue.c_str(), tempResultValue.size()) != 0) {
140         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "GetMediaBase64 strncpy_s error");
141         free(*resultValue);
142         *resultValue = nullptr;
143         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
144     }
145     *resultLen = static_cast<uint64_t>(tempResultValue.size());
146     return errorCode;
147 }
148 
OH_ResourceManager_GetMediaBase64ByName(const NativeResourceManager * mgr,const char * resName,char ** resultValue,uint64_t * resultLen,uint32_t density)149 ResourceManager_ErrorCode OH_ResourceManager_GetMediaBase64ByName(const NativeResourceManager *mgr,
150     const char *resName, char **resultValue, uint64_t *resultLen, uint32_t density)
151 {
152     return OH_ResourceManager_GetMediaBase64DataByName(mgr, resName, resultValue, resultLen, density);
153 }
154 
OH_ResourceManager_GetMediaBase64DataByName(const NativeResourceManager * mgr,const char * resName,char ** resultValue,uint64_t * resultLen,uint32_t density)155 ResourceManager_ErrorCode OH_ResourceManager_GetMediaBase64DataByName(const NativeResourceManager *mgr,
156     const char *resName, char **resultValue, uint64_t *resultLen, uint32_t density)
157 {
158     if (mgr == nullptr || resultValue == nullptr || resultLen == nullptr) {
159         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
160     }
161 
162     std::string tempResultValue;
163     RState state = mgr->resManager->GetMediaBase64DataByName(resName, tempResultValue, density);
164     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
165     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
166         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
167             "failed get media base64 name = %{public}s, errorCode = %{public}d", resName, errorCode);
168         return errorCode;
169     }
170     *resultValue = (char*)malloc(tempResultValue.size() + 1);
171     if (*resultValue == nullptr) {
172         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "GetMediaBase64Byname malloc error");
173         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
174     }
175     if (strncpy_s(*resultValue, tempResultValue.size() + 1, tempResultValue.c_str(), tempResultValue.size()) != 0) {
176         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "GetMediaBase64Byname strncpy_s error");
177         free(*resultValue);
178         *resultValue = nullptr;
179         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
180     }
181     *resultLen = static_cast<uint64_t>(tempResultValue.size());
182     return errorCode;
183 }
184 
OH_ResourceManager_GetMedia(const NativeResourceManager * mgr,uint32_t resId,uint8_t ** resultValue,uint64_t * resultLen,uint32_t density)185 ResourceManager_ErrorCode OH_ResourceManager_GetMedia(const NativeResourceManager *mgr, uint32_t resId,
186     uint8_t **resultValue, uint64_t *resultLen, uint32_t density)
187 {
188     return OH_ResourceManager_GetMediaData(mgr, resId, resultValue, resultLen, density);
189 }
190 
OH_ResourceManager_GetMediaData(const NativeResourceManager * mgr,uint32_t resId,uint8_t ** resultValue,uint64_t * resultLen,uint32_t density)191 ResourceManager_ErrorCode OH_ResourceManager_GetMediaData(const NativeResourceManager *mgr, uint32_t resId,
192     uint8_t **resultValue, uint64_t *resultLen, uint32_t density)
193 {
194     if (mgr == nullptr || resultValue == nullptr || resultLen == nullptr) {
195         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
196     }
197 
198     std::unique_ptr<uint8_t[]> tempResultValue;
199     size_t len;
200     RState state = mgr->resManager->GetMediaDataById(resId, len, tempResultValue, density);
201     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
202     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
203         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
204             "failed get media resource id = %{public}d, errorCode = %{public}d", resId, errorCode);
205         return errorCode;
206     }
207     uint8_t *temPtr = tempResultValue.get();
208     *resultValue = static_cast<uint8_t*>(malloc(len));
209     if (*resultValue == nullptr) {
210         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "GetMedia malloc error");
211         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
212     }
213     std::copy(temPtr, temPtr + len, *resultValue);
214     *resultLen = static_cast<uint64_t>(len);
215     return errorCode;
216 }
217 
OH_ResourceManager_GetMediaByName(const NativeResourceManager * mgr,const char * resName,uint8_t ** resultValue,uint64_t * resultLen,uint32_t density)218 ResourceManager_ErrorCode OH_ResourceManager_GetMediaByName(const NativeResourceManager *mgr, const char *resName,
219     uint8_t **resultValue, uint64_t *resultLen, uint32_t density)
220 {
221     return OH_ResourceManager_GetMediaDataByName(mgr, resName, resultValue, resultLen, density);
222 }
223 
OH_ResourceManager_GetMediaDataByName(const NativeResourceManager * mgr,const char * resName,uint8_t ** resultValue,uint64_t * resultLen,uint32_t density)224 ResourceManager_ErrorCode OH_ResourceManager_GetMediaDataByName(const NativeResourceManager *mgr, const char *resName,
225     uint8_t **resultValue, uint64_t *resultLen, uint32_t density)
226 {
227     if (mgr == nullptr || resultValue == nullptr || resultLen == nullptr) {
228         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
229     }
230 
231     std::unique_ptr<uint8_t[]> tempResultValue;
232     size_t len;
233     RState state = mgr->resManager->GetMediaDataByName(resName, len, tempResultValue, density);
234     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
235     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
236         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
237             "failed get media resource name = %{public}s, errorCode = %{public}d", resName, errorCode);
238         return errorCode;
239     }
240     uint8_t *temPtr = tempResultValue.get();
241     *resultValue = static_cast<uint8_t*>(malloc(len));
242     if (*resultValue == nullptr) {
243         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "GetMediaByName malloc error");
244         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
245     }
246     std::copy(temPtr, temPtr + len, *resultValue);
247     *resultLen = static_cast<uint64_t>(len);
248     return errorCode;
249 }
250 
OH_ResourceManager_GetDrawableDescriptor(const NativeResourceManager * mgr,uint32_t resId,ArkUI_DrawableDescriptor ** drawableDescriptor,uint32_t density,uint32_t type)251 ResourceManager_ErrorCode OH_ResourceManager_GetDrawableDescriptor(const NativeResourceManager *mgr,
252     uint32_t resId, ArkUI_DrawableDescriptor **drawableDescriptor, uint32_t density, uint32_t type)
253 {
254     return OH_ResourceManager_GetDrawableDescriptorData(mgr, resId, drawableDescriptor, density, type);
255 }
256 
OH_ResourceManager_GetDrawableDescriptorData(const NativeResourceManager * mgr,uint32_t resId,ArkUI_DrawableDescriptor ** drawableDescriptor,uint32_t density,uint32_t type)257 ResourceManager_ErrorCode OH_ResourceManager_GetDrawableDescriptorData(const NativeResourceManager *mgr,
258     uint32_t resId, ArkUI_DrawableDescriptor **drawableDescriptor, uint32_t density, uint32_t type)
259 {
260     if (mgr == nullptr || drawableDescriptor == nullptr) {
261         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
262     }
263     RState state = RState::SUCCESS;
264     DrawableDescriptor::DrawableType drawableType;
265     if (type == 1) {
266         std::string themeMask = mgr->resManager->GetThemeMask();
267         std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo;
268         std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo;
269         state = mgr->resManager->GetThemeIcons(resId, foregroundInfo, backgroundInfo, density);
270         if (state == RState::SUCCESS) {
271             auto descriptor = DrawableDescriptorFactory::Create(foregroundInfo, backgroundInfo,
272             themeMask, drawableType, mgr->resManager);
273             *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
274             return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
275                 ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
276         }
277     }
278     auto descriptor = DrawableDescriptorFactory::Create(resId, mgr->resManager, state, drawableType, density);
279     if (state != RState::SUCCESS) {
280         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "Failed to Create drawableDescriptor");
281         return static_cast<ResourceManager_ErrorCode>(state);
282     }
283     *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
284     return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
285         ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
286 }
287 
OH_ResourceManager_GetDrawableDescriptorByName(const NativeResourceManager * mgr,const char * resName,ArkUI_DrawableDescriptor ** drawableDescriptor,uint32_t density,uint32_t type)288 ResourceManager_ErrorCode OH_ResourceManager_GetDrawableDescriptorByName(const NativeResourceManager *mgr,
289     const char *resName, ArkUI_DrawableDescriptor **drawableDescriptor, uint32_t density, uint32_t type)
290 {
291     return OH_ResourceManager_GetDrawableDescriptorDataByName(mgr, resName, drawableDescriptor, density, type);
292 }
293 
OH_ResourceManager_GetDrawableDescriptorDataByName(const NativeResourceManager * mgr,const char * resName,ArkUI_DrawableDescriptor ** drawableDescriptor,uint32_t density,uint32_t type)294 ResourceManager_ErrorCode OH_ResourceManager_GetDrawableDescriptorDataByName(const NativeResourceManager *mgr,
295     const char *resName, ArkUI_DrawableDescriptor **drawableDescriptor, uint32_t density, uint32_t type)
296 {
297     if (mgr == nullptr || drawableDescriptor == nullptr) {
298         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
299     }
300     RState state = RState::SUCCESS;
301     DrawableDescriptor::DrawableType drawableType;
302     if (type == 1) {
303         std::string themeMask = mgr->resManager->GetThemeMask();
304         std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo;
305         std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo;
306         state = mgr->resManager->GetThemeIcons(0, foregroundInfo, backgroundInfo, density);
307         if (state == RState::SUCCESS) {
308             auto descriptor = DrawableDescriptorFactory::Create(foregroundInfo, backgroundInfo,
309             themeMask, drawableType, mgr->resManager);
310             *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
311             return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
312                 ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
313         }
314     }
315     if (type == 2) { // 2 means get the dynamic icon from theme
316         std::pair<std::unique_ptr<uint8_t[]>, size_t> iconInfo;
317         state = mgr->resManager->GetDynamicIcon(resName, iconInfo, density);
318         if (state == RState::SUCCESS) {
319             auto descriptor = std::make_unique<DrawableDescriptor>(std::move(iconInfo.first), iconInfo.second);
320             *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
321             return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
322                 ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
323         }
324     }
325 
326     auto descriptor = DrawableDescriptorFactory::Create(resName, mgr->resManager, state, drawableType, density);
327     if (state != RState::SUCCESS) {
328         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "Failed to Create drawableDescriptor");
329         return static_cast<ResourceManager_ErrorCode>(state);
330     }
331     *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
332     return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
333         ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
334 }
335 
OH_ResourceManager_GetSymbol(const NativeResourceManager * mgr,uint32_t resId,uint32_t * resultValue)336 ResourceManager_ErrorCode OH_ResourceManager_GetSymbol(const NativeResourceManager *mgr, uint32_t resId,
337     uint32_t *resultValue)
338 {
339     if (mgr == nullptr || resultValue == nullptr) {
340         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
341     }
342     uint32_t tempResultValue;
343     RState state = mgr->resManager->GetSymbolById(resId, tempResultValue);
344     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
345     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
346         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
347             "failed get symbol resource id = %{public}d, errorCode = %{public}d", resId, errorCode);
348         return errorCode;
349     }
350     *resultValue = tempResultValue;
351     return errorCode;
352 }
353 
OH_ResourceManager_GetSymbolByName(const NativeResourceManager * mgr,const char * resName,uint32_t * resultValue)354 ResourceManager_ErrorCode OH_ResourceManager_GetSymbolByName(const NativeResourceManager *mgr, const char *resName,
355     uint32_t *resultValue)
356 {
357     if (mgr == nullptr || resultValue == nullptr) {
358         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
359     }
360     uint32_t tempResultValue;
361     RState state = mgr->resManager->GetSymbolByName(resName, tempResultValue);
362     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
363     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
364         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
365             "failed get symbol resource name = %{public}s, errorCode = %{public}d", resName, errorCode);
366         return errorCode;
367     }
368     *resultValue = tempResultValue;
369     return errorCode;
370 }
371 
OH_ResourceManager_GetLocales(const NativeResourceManager * mgr,char *** resultValue,uint32_t * resultLen,bool includeSystem)372 ResourceManager_ErrorCode OH_ResourceManager_GetLocales(const NativeResourceManager *mgr, char ***resultValue,
373     uint32_t *resultLen, bool includeSystem)
374 {
375     return OH_ResourceManager_GetLocalesData(mgr, resultValue, resultLen, includeSystem);
376 }
377 
OH_ResourceManager_GetLocalesData(const NativeResourceManager * mgr,char *** resultValue,uint32_t * resultLen,bool includeSystem)378 ResourceManager_ErrorCode OH_ResourceManager_GetLocalesData(const NativeResourceManager *mgr, char ***resultValue,
379     uint32_t *resultLen, bool includeSystem)
380 {
381     if (mgr == nullptr || resultValue == nullptr) {
382         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
383     }
384     std::vector<std::string> tempResultValue;
385     mgr->resManager->GetLocales(tempResultValue, includeSystem);
386     return copyStringArray(resultValue, resultLen, tempResultValue, "GetLocales");
387 }
388 
OH_ResourceManager_GetConfiguration(const NativeResourceManager * mgr,ResourceManager_Configuration * configuration)389 ResourceManager_ErrorCode OH_ResourceManager_GetConfiguration(const NativeResourceManager *mgr,
390     ResourceManager_Configuration *configuration)
391 {
392     if (mgr == nullptr || configuration == nullptr) {
393         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
394     }
395     ResConfigImpl resConfig;
396     mgr->resManager->GetResConfig(resConfig);
397 
398     const icu::Locale *localeInfo = resConfig.GetLocaleInfo();
399     ResourceManager_ErrorCode errCode = copyLocalInfo(localeInfo, configuration);
400     if (errCode != ResourceManager_ErrorCode::SUCCESS) {
401         return errCode;
402     }
403     {
404         configuration->direction = static_cast<::ResourceManager_Direction>(resConfig.GetDirection());
405         configuration->deviceType = static_cast<::ResourceManager_DeviceType>(resConfig.GetDeviceType());
406         configuration->screenDensity = static_cast<::ScreenDensity>(resConfig.GetScreenDensity());
407         configuration->colorMode = static_cast<::ResourceManager_ColorMode>(resConfig.GetColorMode());
408         configuration->mcc = resConfig.GetMcc();
409         configuration->mnc = resConfig.GetMnc();
410     }
411     return ResourceManager_ErrorCode::SUCCESS;
412 }
413 
OH_ResourceManager_GetStringArray(const NativeResourceManager * mgr,uint32_t resId,char *** resultValue,uint32_t * resultValueLen)414 ResourceManager_ErrorCode OH_ResourceManager_GetStringArray(const NativeResourceManager *mgr,
415     uint32_t resId, char ***resultValue, uint32_t *resultValueLen)
416 {
417     if (mgr == nullptr || resultValue == nullptr) {
418         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
419     }
420     std::vector<std::string> tempResultValue;
421     RState state = mgr->resManager->GetStringArrayById(resId, tempResultValue);
422     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
423     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
424         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
425             "failed get string array id = %{public}d, errorCode = %{public}d", resId, errorCode);
426         return errorCode;
427     }
428     return copyStringArray(resultValue, resultValueLen, tempResultValue, "GetStringArray");
429 }
430 
OH_ResourceManager_GetStringArrayByName(const NativeResourceManager * mgr,const char * resName,char *** resultValue,uint32_t * resultLen)431 ResourceManager_ErrorCode OH_ResourceManager_GetStringArrayByName(const NativeResourceManager *mgr,
432     const char *resName, char ***resultValue, uint32_t *resultLen)
433 {
434     if (mgr == nullptr || resultValue == nullptr) {
435         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
436     }
437     std::vector<std::string> tempResultValue;
438     RState state = mgr->resManager->GetStringArrayByName(resName, tempResultValue);
439     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
440     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
441         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
442             "failed get string array resName = %{public}s, errorCode = %{public}d", resName, errorCode);
443         return errorCode;
444     }
445     return copyStringArray(resultValue, resultLen, tempResultValue, "GetStringArrayByName");
446 }
447 
OH_ResourceManager_GetPluralStringByName(const NativeResourceManager * mgr,const char * resName,uint32_t num,char ** resultValue)448 ResourceManager_ErrorCode OH_ResourceManager_GetPluralStringByName(const NativeResourceManager *mgr,
449     const char *resName, uint32_t num, char **resultValue)
450 {
451     if (mgr == nullptr || resultValue == nullptr) {
452         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
453     }
454     std::string tempResultValue;
455     RState state = mgr->resManager->GetPluralStringByNameFormat(tempResultValue, resName, num, num);
456     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
457     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
458         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
459             "failed get plural string name = %{public}s, errorCode = %{public}d", resName, errorCode);
460         return errorCode;
461     }
462     return copyString(resultValue, tempResultValue, "GetPluralStringByName");
463 }
464 
OH_ResourceManager_GetPluralString(const NativeResourceManager * mgr,uint32_t resId,uint32_t num,char ** resultValue)465 ResourceManager_ErrorCode OH_ResourceManager_GetPluralString(const NativeResourceManager *mgr,
466     uint32_t resId, uint32_t num, char **resultValue)
467 {
468     if (mgr == nullptr || resultValue == nullptr) {
469         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
470     }
471     std::string tempResultValue;
472     RState state = mgr->resManager->GetPluralStringByIdFormat(tempResultValue, resId, num, num);
473     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
474     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
475         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
476             "failed get plural string id = %{public}d, errorCode = %{public}d", resId, errorCode);
477         return errorCode;
478     }
479     return copyString(resultValue, tempResultValue, "GetPluralString");
480 }
481 
OH_ResourceManager_GetColor(const NativeResourceManager * mgr,uint32_t resId,uint32_t * resultValue)482 ResourceManager_ErrorCode OH_ResourceManager_GetColor(const NativeResourceManager *mgr, uint32_t resId,
483     uint32_t *resultValue)
484 {
485     if (mgr == nullptr || resultValue == nullptr) {
486         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
487     }
488     uint32_t tempResultValue;
489     RState state = mgr->resManager->GetColorById(resId, tempResultValue);
490     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
491     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
492         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "failed get color id = %{public}d, errorCode = %{public}d", resId, errorCode);
493         return errorCode;
494     }
495     *resultValue = tempResultValue;
496     return errorCode;
497 }
498 
OH_ResourceManager_GetColorByName(const NativeResourceManager * mgr,const char * resName,uint32_t * resultValue)499 ResourceManager_ErrorCode OH_ResourceManager_GetColorByName(const NativeResourceManager *mgr, const char *resName,
500     uint32_t *resultValue)
501 {
502     if (mgr == nullptr || resultValue == nullptr) {
503         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
504     }
505     uint32_t tempResultValue;
506     RState state = mgr->resManager->GetColorByName(resName, tempResultValue);
507     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
508     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
509         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
510             "failed get color name = %{public}s, errorCode = %{public}d", resName, errorCode);
511         return errorCode;
512     }
513     *resultValue = tempResultValue;
514     return errorCode;
515 }
516 
OH_ResourceManager_GetInt(const NativeResourceManager * mgr,uint32_t resId,int * resultValue)517 ResourceManager_ErrorCode OH_ResourceManager_GetInt(const NativeResourceManager *mgr, uint32_t resId,
518     int *resultValue)
519 {
520     if (mgr == nullptr || resultValue == nullptr) {
521         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
522     }
523     int tempResultValue;
524     RState state = mgr->resManager->GetIntegerById(resId, tempResultValue);
525     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
526     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
527         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
528             "failed get integer id = %{public}d, errorCode = %{public}d", resId, errorCode);
529         return errorCode;
530     }
531     *resultValue = tempResultValue;
532     return errorCode;
533 }
534 
OH_ResourceManager_GetIntByName(const NativeResourceManager * mgr,const char * resName,int * resultValue)535 ResourceManager_ErrorCode OH_ResourceManager_GetIntByName(const NativeResourceManager *mgr, const char *resName,
536     int *resultValue)
537 {
538     if (mgr == nullptr || resultValue == nullptr) {
539         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
540     }
541     int tempResultValue;
542     RState state = mgr->resManager->GetIntegerByName(resName, tempResultValue);
543     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
544     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
545         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
546             "failed get integer name = %{public}s, errorCode = %{public}d", resName, errorCode);
547         return errorCode;
548     }
549     *resultValue = tempResultValue;
550     return errorCode;
551 }
552 
OH_ResourceManager_GetFloat(const NativeResourceManager * mgr,uint32_t resId,float * resultValue)553 ResourceManager_ErrorCode OH_ResourceManager_GetFloat(const NativeResourceManager *mgr, uint32_t resId,
554     float *resultValue)
555 {
556     if (mgr == nullptr || resultValue == nullptr) {
557         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
558     }
559     float tempResultValue;
560     RState state = mgr->resManager->GetFloatById(resId, tempResultValue);
561     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
562     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
563         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "failed get float id = %{public}d, errorCode = %{public}d", resId, errorCode);
564         return errorCode;
565     }
566     *resultValue = tempResultValue;
567     return errorCode;
568 }
569 
OH_ResourceManager_GetFloatByName(const NativeResourceManager * mgr,const char * resName,float * resultValue)570 ResourceManager_ErrorCode OH_ResourceManager_GetFloatByName(const NativeResourceManager *mgr, const char *resName,
571     float *resultValue)
572 {
573     if (mgr == nullptr || resultValue == nullptr) {
574         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
575     }
576     float tempResultValue;
577     RState state = mgr->resManager->GetFloatByName(resName, tempResultValue);
578     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
579     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
580         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
581             "failed get float name = %{public}s, errorCode = %{public}d", resName, errorCode);
582         return errorCode;
583     }
584     *resultValue = tempResultValue;
585     return errorCode;
586 }
587 
OH_ResourceManager_GetBool(const NativeResourceManager * mgr,uint32_t resId,bool * resultValue)588 ResourceManager_ErrorCode OH_ResourceManager_GetBool(const NativeResourceManager *mgr, uint32_t resId,
589     bool *resultValue)
590 {
591     if (mgr == nullptr || resultValue == nullptr) {
592         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
593     }
594     bool tempResultValue;
595     RState state = mgr->resManager->GetBooleanById(resId, tempResultValue);
596     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
597     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
598         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "failed get bool id = %{public}d, errorCode = %{public}d", resId, errorCode);
599         return errorCode;
600     }
601     *resultValue = tempResultValue;
602     return errorCode;
603 }
604 
OH_ResourceManager_GetBoolByName(const NativeResourceManager * mgr,const char * resName,bool * resultValue)605 ResourceManager_ErrorCode OH_ResourceManager_GetBoolByName(const NativeResourceManager *mgr, const char *resName,
606     bool *resultValue)
607 {
608     if (mgr == nullptr || resultValue == nullptr) {
609         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
610     }
611     bool tempResultValue;
612     RState state = mgr->resManager->GetBooleanByName(resName, tempResultValue);
613     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
614     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
615         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
616             "failed get bool name = %{public}s, errorCode = %{public}d", resName, errorCode);
617         return errorCode;
618     }
619     *resultValue = tempResultValue;
620     return errorCode;
621 }
622 
OH_ResourceManager_AddResource(const NativeResourceManager * mgr,const char * path)623 ResourceManager_ErrorCode OH_ResourceManager_AddResource(const NativeResourceManager *mgr, const char *path)
624 {
625     if (mgr == nullptr || path == nullptr) {
626         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
627     }
628     if (!mgr->resManager->AddAppOverlay(path)) {
629         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "Failed to add overlay path = %{public}s", path);
630         return ResourceManager_ErrorCode::ERROR_CODE_OVERLAY_RES_PATH_INVALID;
631     }
632     return ResourceManager_ErrorCode::SUCCESS;
633 }
634 
OH_ResourceManager_RemoveResource(const NativeResourceManager * mgr,const char * path)635 ResourceManager_ErrorCode OH_ResourceManager_RemoveResource(const NativeResourceManager *mgr, const char *path)
636 {
637     if (mgr == nullptr || path == nullptr) {
638         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
639     }
640     if (!mgr->resManager->RemoveAppOverlay(path)) {
641         RESMGR_HILOGE(RESMGR_NATIVE_TAG, "Failed to remove overlay path = %{public}s", path);
642         return ResourceManager_ErrorCode::ERROR_CODE_OVERLAY_RES_PATH_INVALID;
643     }
644     return ResourceManager_ErrorCode::SUCCESS;
645 }
646 
OH_ResourceManager_ReleaseConfiguration(ResourceManager_Configuration * configuration)647 ResourceManager_ErrorCode OH_ResourceManager_ReleaseConfiguration(ResourceManager_Configuration *configuration)
648 {
649     if (configuration == nullptr) {
650         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
651     }
652     if (configuration->locale != nullptr) {
653         free(configuration->locale);
654         configuration->locale = nullptr;
655     }
656     return ResourceManager_ErrorCode::SUCCESS;
657 }
658 
OH_ResourceManager_ReleaseStringArray(char *** resValue,uint32_t len)659 ResourceManager_ErrorCode OH_ResourceManager_ReleaseStringArray(char ***resValue, uint32_t len)
660 {
661     if (resValue == nullptr || *resValue == nullptr) {
662         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
663     }
664     for (uint32_t i = 0; i < len; i++) {
665         if ((*resValue)[i] != nullptr) {
666             delete [] (*resValue)[i];
667             (*resValue)[i] = nullptr;
668         }
669     }
670     delete [] *resValue;
671     *resValue = nullptr;
672     return ResourceManager_ErrorCode::SUCCESS;
673 }
674 
OH_ResourceManager_GetString(const NativeResourceManager * mgr,uint32_t resId,char ** resultValue,...)675 ResourceManager_ErrorCode OH_ResourceManager_GetString(const NativeResourceManager *mgr, uint32_t resId,
676     char **resultValue, ...)
677 {
678     if (mgr == nullptr || resultValue == nullptr) {
679         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
680     }
681     string tempResultValue;
682 
683     va_list args;
684     va_start(args, resultValue);
685     RState state = mgr->resManager->GetStringFormatById(tempResultValue, resId, args);
686     va_end(args);
687     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
688     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
689         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
690             "failed get string id = %{public}d, errorCode = %{public}d", resId, errorCode);
691         return errorCode;
692     }
693     return copyString(resultValue, tempResultValue, "GetString");
694 }
695 
OH_ResourceManager_GetStringByName(const NativeResourceManager * mgr,const char * resName,char ** resultValue,...)696 ResourceManager_ErrorCode OH_ResourceManager_GetStringByName(const NativeResourceManager *mgr, const char *resName,
697     char **resultValue, ...)
698 {
699     if (mgr == nullptr || resultValue == nullptr) {
700         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
701     }
702     string tempResultValue;
703 
704     va_list args;
705     va_start(args, resultValue);
706     RState state = mgr->resManager->GetStringFormatByName(tempResultValue, resName, args);
707     va_end(args);
708     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
709     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
710         RESMGR_HILOGE(RESMGR_NATIVE_TAG,
711             "failed get string name = %{public}s, errorCode = %{public}d", resName, errorCode);
712         return errorCode;
713     }
714     return copyString(resultValue, tempResultValue, "GetStringByName");
715 }