• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_MOCK_RESOURCE_MANAGER_RESOURCEMANAGERIMPL_H
16 #define OHOS_MOCK_RESOURCE_MANAGER_RESOURCEMANAGERIMPL_H
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 #include <tuple>
22 #include "resource_manager.h"
23 
24 namespace OHOS {
25 namespace Global {
26 namespace Resource {
27 class ResourceManagerImpl : public ResourceManager {
28 public:
29     ResourceManagerImpl();
30 
31     ~ResourceManagerImpl();
32 
33     bool Init();
34 
35     /**
36      * Add resource path to hap paths
37      * @param path the resource path
38      * @return true if add resource path success, else false
39      */
40     virtual bool AddResource(const char *path);
41 
42     /**
43      * Add resource path to overlay paths
44      * @param path the resource path
45      * @param overlayPaths the exist overlay resource path
46      * @return true if add resource path success, else false
47      */
48     virtual bool AddResource(const std::string &path, const std::vector<std::string> &overlayPaths);
49 
50     /**
51      * Remove resource path to overlay paths
52      * @param path the resource path
53      * @param overlayPaths the exist overlay resource path
54      * @return true if add resource path success, else false
55      */
56     virtual bool RemoveResource(const std::string &path, const std::vector<std::string> &overlayPaths);
57 
58     /**
59      * Update the resConfig
60      * @param resConfig the resource config
61      * @return SUCCESS if the resConfig updated success, else HAP_INIT_FAILED
62      */
63     virtual RState UpdateResConfig(ResConfig &resConfig, bool isUpdateTheme = false);
64 
65     /**
66      * Get the resConfig
67      * @param resConfig the resource config
68      */
69     virtual void GetResConfig(ResConfig &resConfig);
70 
71     /**
72      * Get string resource by Id
73      * @param id the resource Id
74      * @param outValue the string resource write to
75      * @return SUCCESS if resource exist, else NOT_FOUND
76      */
77     virtual RState GetStringById(uint32_t id, std::string &outValue);
78 
79     /**
80      * Get string by resource name
81      * @param name the resource name
82      * @param outValue the resource write to
83      * @return SUCCESS if resource exist, else NOT_FOUND
84      */
85     virtual RState GetStringByName(const char *name, std::string &outValue);
86 
87     /**
88      * Get string format by resource id
89      * @param id the resource id
90      * @param outValue the resource write to
91      * @return SUCCESS if resource exist, else NOT_FOUND
92      */
93     virtual RState GetStringFormatById(std::string &outValue, uint32_t id, ...);
94 
95     /**
96      * Get string format by resource name
97      * @param name the resource name
98      * @param outValue the resource write to
99      * @return SUCCESS if resource exist, else NOT_FOUND
100      */
101     virtual RState GetStringFormatByName(std::string &outValue, const char *name, ...);
102 
103     /**
104      * Get the STRINGARRAY resource by resource id
105      * @param id the resource id
106      * @param outValue the resource write to
107      * @return SUCCESS if resource exist, else NOT_FOUND
108      */
109     virtual RState GetStringArrayById(uint32_t id, std::vector<std::string> &outValue);
110 
111     /**
112      * Get the STRINGARRAY resource by resource name
113      * @param name the resource name
114      * @param outValue the resource write to
115      * @return SUCCESS if resource exist, else NOT_FOUND
116      */
117     virtual RState GetStringArrayByName(const char *name, std::vector<std::string> &outValue);
118 
119     /**
120      * Get the PATTERN resource by resource id
121      * @param id the resource id
122      * @param outValue the resource write to
123      * @return SUCCESS if resource exist, else NOT_FOUND
124      */
125     virtual RState GetPatternById(uint32_t id, std::map<std::string, std::string> &outValue);
126 
127     /**
128      * Get the PATTERN resource by resource name
129      * @param name the resource name
130      * @param outValue the resource write to
131      * @return SUCCESS if resource exist, else NOT_FOUND
132      */
133     virtual RState GetPatternByName(const char *name, std::map<std::string, std::string> &outValue);
134 
135     /**
136      * Get the plural string by resource id
137      * @param id the resource id
138      * @param quantity the language quantity
139      * @param outValue the resource write to
140      * @return SUCCESS if resource exist, else NOT_FOUND
141      */
142     virtual RState GetPluralStringById(uint32_t id, int quantity, std::string &outValue);
143 
144     /**
145      * Get the plural string by resource name
146      * @param name the resource name
147      * @param quantity the language quantity
148      * @param outValue the resource write to
149      * @return SUCCESS if resource exist, else NOT_FOUND
150      */
151     virtual RState GetPluralStringByName(const char *name, int quantity, std::string &outValue);
152 
153     /**
154      * Get the plural format string by resource id
155      * @param outValue the resource write to
156      * @param id the resource id
157      * @param quantity the language quantity
158      * @return SUCCESS if resource exist, else NOT_FOUND
159      */
160     virtual RState GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...);
161 
162     /**
163      * Get the plural format string by resource name
164      * @param outValue the resource write to
165      * @param id the resource id
166      * @param quantity the language quantity
167      * @return SUCCESS if resource exist, else NOT_FOUND
168      */
169     virtual RState GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...);
170 
171     /**
172      * Get the THEME resource by resource id
173      * @param id the resource id
174      * @param outValue the resource write to
175      * @return SUCCESS if resource exist, else NOT_FOUND
176      */
177     virtual RState GetThemeById(uint32_t id, std::map<std::string, std::string> &outValue);
178 
179     /**
180      * Get the THEME resource by resource name
181      * @param name the resource name
182      * @param outValue the resource write to
183      * @return SUCCESS if resource exist, else NOT_FOUND
184      */
185     virtual RState GetThemeByName(const char *name, std::map<std::string, std::string> &outValue);
186 
187     /**
188      * Get the BOOLEAN resource by resource id
189      * @param id the resource id
190      * @param outValue the obtain boolean value write to
191      * @return SUCCESS if resource exist, else NOT_FOUND
192      */
193     virtual RState GetBooleanById(uint32_t id, bool &outValue);
194 
195     /**
196      * Get the BOOLEAN resource by resource name
197      * @param name the resource name
198      * @param outValue the obtain boolean value write to
199      * @return SUCCESS if resource exist, else NOT_FOUND
200      */
201     virtual RState GetBooleanByName(const char *name, bool &outValue);
202 
203     /**
204      * Get the INTEGER resource by resource id
205      * @param id the resource id
206      * @param outValue the obtain Integer value write to
207      * @return SUCCESS if resource exist, else NOT_FOUND
208      */
209     virtual RState GetIntegerById(uint32_t id, int &outValue);
210 
211     /**
212      * Get the INTEGER resource by resource name
213      * @param name the resource name
214      * @param outValue the obtain Integer value write to
215      * @return SUCCESS if resource exist, else NOT_FOUND
216      */
217     virtual RState GetIntegerByName(const char *name, int &outValue);
218 
219     /**
220      * Get the FLOAT resource by resource id
221      * @param id the resource id
222      * @param outValue the obtain float value write to
223      * @return SUCCESS if resource exist, else NOT_FOUND
224      */
225     virtual RState GetFloatById(uint32_t id, float &outValue);
226 
227     /**
228      * Get the FLOAT resource by resource id
229      * @param id the resource id
230      * @param outValue the obtain float value write to
231      * @param unit the unit do not in parsing
232      * @return SUCCESS if resource exist, else NOT_FOUND
233      */
234     virtual RState GetFloatById(uint32_t id, float &outValue, std::string &unit);
235 
236     /**
237      * Get the FLOAT resource by resource name
238      * @param name the resource name
239      * @param outValue the obtain float value write to
240      * @return SUCCESS if resource exist, else NOT_FOUND
241      */
242     virtual RState GetFloatByName(const char *name, float &outValue);
243 
244     /**
245      * Get the FLOAT resource by resource id
246      * @param id the resource id
247      * @param outValue the obtain float value write to
248      * @param unit the string do not in parsing
249      * @return SUCCESS if resource exist, else NOT_FOUND
250      */
251     virtual RState GetFloatByName(const char *name, float &outValue, std::string &unit);
252 
253     /**
254      * Get the INTARRAY resource by resource id
255      * @param id the resource id
256      * @param outValue the obtain resource value convert to vector<int> write to
257      * @return SUCCESS if resource exist, else NOT_FOUND
258      */
259     virtual RState GetIntArrayById(uint32_t id, std::vector<int> &outValue);
260 
261     /**
262      * Get the INTARRAY resource by resource name
263      * @param name the resource name
264      * @param outValue the obtain resource value convert to vector<int> write to
265      * @return SUCCESS if resource exist, else NOT_FOUND
266      */
267     virtual RState GetIntArrayByName(const char *name, std::vector<int> &outValue);
268 
269     /**
270      * Get the COLOR resource by resource id
271      * @param id the resource id
272      * @param outValue the obtain resource value convert to uint32_t write to
273      * @return SUCCESS if resource exist, else NOT_FOUND
274      */
275     virtual RState GetColorById(uint32_t id, uint32_t &outValue);
276 
277     /**
278      * Get the COLOR resource by resource name
279      * @param name the resource name
280      * @param outValue the obtain resource value convert to uint32_t write to
281      * @return SUCCESS if resource exist, else NOT_FOUND
282      */
283     virtual RState GetColorByName(const char *name, uint32_t &outValue);
284 
285     /**
286      * Get the PROF resource by resource id
287      * @param id the resource id
288      * @param outValue the obtain resource path write to
289      * @return SUCCESS if resource exist, else NOT_FOUND
290      */
291     virtual RState GetProfileById(uint32_t id, std::string &outValue);
292 
293     /**
294      * Get the PROF resource by resource name
295      * @param name the resource name
296      * @param outValue the obtain resource path write to
297      * @return SUCCESS if resource exist, else NOT_FOUND
298      */
299     virtual RState GetProfileByName(const char *name, std::string &outValue);
300 
301     /**
302      * Get the MEDIA resource by resource id
303      * @param id the resource id
304      * @param outValue the obtain resource path write to
305      * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity
306      * @return SUCCESS if resource exist, else NOT_FOUND
307      */
308     virtual RState GetMediaById(uint32_t id, std::string &outValue, uint32_t density = 0);
309 
310     /**
311      * Get the MEDIA resource by resource name
312      * @param name the resource name
313      * @param outValue the obtain resource path write to
314      * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity
315      * @return SUCCESS if resource exist, else NOT_FOUND
316      */
317     virtual RState GetMediaByName(const char *name, std::string &outValue, uint32_t density = 0);
318 
319     /**
320      * Get the SYMBOL resource by resource id
321      * @param id the resource id
322      * @param outValue the obtain resource value convert to uint32_t write to
323      * @return SUCCESS if resource exist, else NOT_FOUND
324      */
325     virtual RState GetSymbolById(uint32_t id, uint32_t &outValue);
326 
327     /**
328      * Get the Symbol resource by resource name
329      * @param name the resource name
330      * @param outValue the obtain resource value convert to uint32_t write to
331      * @return SUCCESS if resource exist, else NOT_FOUND
332      */
333     virtual RState GetSymbolByName(const char *name, uint32_t &outValue);
334 
335     /**
336      * Get the raw file path by resource name
337      * @param name the resource name
338      * @param outValue the obtain resource path write to
339      * @return SUCCESS if resource exist, else NOT_FOUND
340      */
341     virtual RState GetRawFilePathByName(const std::string &name, std::string &outValue);
342 
343     /**
344      * Get the rawFile descriptor by resource name
345      * @param name the resource name
346      * @param descriptor the obtain raw file member fd, length, offet write to
347      * @return SUCCESS if resource exist, else ERROR
348      */
349     virtual RState GetRawFileDescriptor(const std::string &name, RawFileDescriptor &descriptor);
350 
351     /**
352      * Close rawFile descriptor by resource name
353      * @param name the resource name
354      * @return SUCCESS if close the rawFile descriptor, else ERROR
355      */
356     virtual RState CloseRawFileDescriptor(const std::string &name);
357 
358     /**
359      * Get all resource paths
360      * @return The vector of resource paths
361      */
362     std::vector<std::string> GetResourcePaths();
363 
364     /**
365      * Get the MEDIA data by resource id
366      * @param id the resource id
367      * @param len the data len write to
368      * @param outValue the obtain resource path write to
369      * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity
370      * @return SUCCESS if resource exist, else NOT_FOUND
371      */
372     virtual RState GetMediaDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue,
373         uint32_t density = 0);
374 
375     /**
376      * Get the MEDIA data by resource name
377      * @param name the resource name
378      * @param len the data len write to
379      * @param outValue the obtain resource path write to
380      * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity
381      * @return SUCCESS if resource exist, else NOT_FOUND
382      */
383     virtual RState GetMediaDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue,
384         uint32_t density = 0);
385 
386     /**
387      * Get the MEDIA base64 data resource by resource id
388      * @param id the resource id
389      * @param outValue the media base64 data
390      * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity
391      * @return SUCCESS if resource exist, else NOT_FOUND
392      */
393     virtual RState GetMediaBase64DataById(uint32_t id, std::string &outValue, uint32_t density = 0);
394 
395     /**
396      * Get the MEDIA base64 data resource by resource id
397      * @param name the resource name
398      * @param outValue the media base64 data
399      * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity
400      * @return SUCCESS if resource exist, else NOT_FOUND
401      */
402     virtual RState GetMediaBase64DataByName(const char *name, std::string &outValue, uint32_t density = 0);
403 
404     /**
405      * Get the PROF resource by resource id
406      * @param name the resource id
407      * @param len the data len write to
408      * @param outValue the obtain resource path write to
409      * @return SUCCESS if resource exist, else NOT_FOUND
410      */
411     virtual RState GetProfileDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue);
412 
413     /**
414      * Get the PROF resource by resource name
415      * @param name the resource name
416      * @param len the data len write to
417      * @param outValue the obtain resource path write to
418      * @return SUCCESS if resource exist, else NOT_FOUND
419      */
420     virtual RState GetProfileDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue);
421 
422     /**
423      * Get the rawFile base64 from hap by rawFile name
424      * @param rawFileName the rawFile name
425      * @param len the data len write to
426      * @param outValue the obtain resource path write to
427      * @return SUCCESS if resource exist, else NOT_FOUND
428      */
429     virtual RState GetRawFileFromHap(const std::string &rawFileName, size_t &len,
430         std::unique_ptr<uint8_t[]> &outValue);
431 
432     /**
433      * Get the rawFile Descriptor from hap by rawFile name
434      * @param rawFileName the rawFile name
435      * @param descriptor the raw file member fd, length, offet write to
436      * @return SUCCESS if resource exist, else NOT_FOUND
437      */
438     virtual RState GetRawFileDescriptorFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor);
439 
440     /**
441      * Is load hap
442      * @param hapPath the hap path
443      */
444     virtual RState IsLoadHap(std::string &hapPath);
445 
446     /**
447      * Get the raw file list
448      * @param rawDirPath the rawfile directory path
449      * @param rawfileList the rawfile list write to
450      * @return SUCCESS if resource exist, else not found
451      */
452     virtual RState GetRawFileList(const std::string &rawDirPath, std::vector<std::string>& rawfileList);
453 
454     /**
455      * Get the drawable information for given resId, mainly about type, len, buffer
456      * @param id the resource id
457      * @param type the drawable type
458      * @param len the drawable buffer length
459      * @param outValue the drawable buffer write to
460      * @param density the drawable density
461      * @return SUCCESS if resource exist, else not found
462      */
463     virtual RState GetDrawableInfoById(uint32_t id, std::string &type, size_t &len,
464         std::unique_ptr<uint8_t[]> &outValue, uint32_t density = 0);
465 
466     /**
467      * Get the drawable information for given resName, mainly about type, len, buffer
468      * @param name the resource Name
469      * @param type the drawable type
470      * @param len the drawable buffer length
471      * @param outValue the drawable buffer write to
472      * @param density the drawable density
473      * @return SUCCESS if resource exist, else not found
474      */
475     virtual RState GetDrawableInfoByName(const char *name, std::string &type, size_t &len,
476         std::unique_ptr<uint8_t[]> &outValue, uint32_t density = 0);
477 
478     /**
479      * Get string format by resource id
480      * @param id the resource id
481      * @param outValue the resource write to
482      * @param jsParams the formatting string resource js parameters, the tuple first parameter represents the type,
483      *     napi_number is denoted by NAPI_NUMBER, napi_string is denoted by NAPI_STRING,
484      *     the tuple second parameter represents the value
485      * @return SUCCESS if resource exists and was formatted successfully, else ERROR
486      */
487     virtual RState GetStringFormatById(uint32_t id, std::string &outValue,
488         std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams);
489 
490     /**
491      * Get string format by resource name
492      * @param name the resource name
493      * @param outValue the resource write to
494      * @param jsParams the formatting string resource js parameters, the tuple first parameter represents the type,
495      *     napi_number is denoted by NAPI_NUMBER, napi_string is denoted by NAPI_STRING,
496      *     the tuple second parameter represents the value
497      * @return SUCCESS if resource exists and was formatted successfully, else ERROR
498      */
499     virtual RState GetStringFormatByName(const char *name, std::string &outValue,
500         std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams);
501 
502     /**
503      * Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType}
504      *
505      * @return the resource limit keys
506      */
507     virtual uint32_t GetResourceLimitKeys();
508 
509     /**
510      * Add the overlay resource for current application
511      * @param path the overlay resource path
512      * @return true if add resource path success, else false
513      */
514     virtual bool AddAppOverlay(const std::string &path);
515 
516     /**
517      * Remove the overlay resource for current application
518      * @param path the overlay resource path
519      * @return true if add resource path success, else false
520      */
521     virtual bool RemoveAppOverlay(const std::string &path);
522 
523     /**
524      * Get the rawFile descriptor from resource name
525      *
526      * @param name the resource name
527      * @param descriptor the obtain raw file member fd, length, offet write to
528      * @return SUCCESS if resource exist, else ERROR
529      */
530     virtual RState GetRawFdNdkFromHap(const std::string &name, RawFileDescriptor &descriptor);
531 
532     /**
533      * Get the resource id by resType and resName
534      *
535      * @param resTypeName the resType and resName
536      * @param resId the resId write to
537      * @return SUCCESS if resource exist, else ERROR
538      */
539     virtual RState GetResId(const std::string &resTypeName, uint32_t &resId);
540 
541     /**
542      * Get locale list
543      *
544      * @param outValue the locales write to, the locale string is divided into three parts: language,
545      *     script (optional), and region (optional), concatenated by the connector (-).
546      * @param includeSystem the parameter controls whether to include system resources,
547      *     the default value is false, it has no effect when only system resources query the locales list.
548      */
549     virtual void GetLocales(std::vector<std::string> &outValue, bool includeSystem = false);
550 
551     /**
552      * Get the drawable information for given resId, mainly about type, len, buffer
553      * @param id the resource id
554      * @param drawableInfo the drawable info
555      * @param outValue the drawable buffer write to
556      * @param iconType the drawable type
557      * @param density the drawable density
558      * @return SUCCESS if resource exist, else not found
559      */
560     virtual RState GetDrawableInfoById(uint32_t id,
561         std::tuple<std::string, size_t, std::string> &drawableInfo,
562         std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density = 0);
563 
564     /**
565      * Get the drawable information for given resName, mainly about type, len, buffer
566      * @param name the resource Name
567      * @param drawableInfo the drawable info
568      * @param outValue the drawable buffer write to
569      * @param iconType the drawable type
570      * @param density the drawable density
571      * @return SUCCESS if resource exist, else not found
572      */
573     virtual RState GetDrawableInfoByName(const char *name,
574         std::tuple<std::string, size_t, std::string> &drawableInfo,
575         std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density = 0);
576 };
577 } // namespace Resource
578 } // namespace Global
579 } // namespace OHOS
580 #endif