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