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_HAPMANAGER_H
16 #define OHOS_RESOURCE_MANAGER_HAPMANAGER_H
17 
18 #include "res_config_impl.h"
19 #include "hap_resource.h"
20 #include "res_desc.h"
21 #include "lock.h"
22 
23 #include <locale_info.h>
24 #include <plural_format.h>
25 #include <vector>
26 
27 namespace OHOS {
28 namespace Global {
29 namespace Resource {
30 class HapManager {
31 public:
32     /**
33      * The constructor of HapManager
34      */
35     explicit HapManager(ResConfigImpl *resConfig);
36 
37     /**
38      * The destructor of HapManager
39      */
40     ~HapManager();
41 
42     /**
43      * Update the resConfig
44      * @param resConfig the resource config
45      * @return SUCCESS if the resConfig updated success, else HAP_INIT_FAILED
46      */
47     RState UpdateResConfig(ResConfig &resConfig);
48 
49     /**
50      * Get the resConfig
51      * @param resConfig the resource config
52      */
53     void GetResConfig(ResConfig &resConfig);
54 
55     /**
56      * Add resource path to hap paths
57      * @param path the resource path
58      * @return true if add resource path success, else false
59      */
60     bool AddResource(const char *path);
61 
62     /**
63      * Find resource by resource id
64      * @param id the resource id
65      * @return the resources related to resource id
66      */
67     const IdItem *FindResourceById(uint32_t id);
68 
69     /**
70      * Find resource by resource name
71      * @param name the resource name
72      * @param resType the resource type
73      * @return the resources related to resource name
74      */
75     const IdItem *FindResourceByName(const char *name, const ResType resType);
76 
77     /**
78      * Find best resource path by resource id
79      * @param id the resource id
80      * @return the best resource path
81      */
82     const HapResource::ValueUnderQualifierDir *FindQualifierValueById(uint32_t id);
83 
84     /**
85      * Find best resource path by resource name
86      * @param name the resource name
87      * @param resType the resource type
88      * @return the best resource path
89      */
90     const HapResource::ValueUnderQualifierDir *FindQualifierValueByName(const char *name, const ResType resType);
91 
92     /**
93      * Get the language pluralRule related to quantity
94      * @param quantity the language quantity
95      * @return the language pluralRule related to quantity
96      */
97     std::string GetPluralRulesAndSelect(int quantity);
98 
99 private:
100     void UpdateResConfigImpl(ResConfigImpl &resConfig);
101 
102     void GetResConfigImpl(ResConfigImpl &resConfig);
103 
104     const HapResource::IdValues *GetResourceList(uint32_t ident) const;
105 
106     const HapResource::IdValues *GetResourceListByName(const char *name, const ResType resType) const;
107 
108     bool AddResourcePath(const char *path);
109 
110     // when resConfig_ updated we must call ReloadAll()
111     RState ReloadAll();
112 
113     // app res config
114     ResConfigImpl *resConfig_;
115 
116     // set of hap Resources
117     std::vector<HapResource *> hapResources_;
118 
119     // set of loaded hap path
120     std::vector<std::string> loadedHapPaths_;
121 
122     // key is language
123     std::vector<std::pair<std::string, OHOS::I18N::PluralFormat *>> plurRulesCache_;
124 
125     Lock lock_;
126 };
127 } // namespace Resource
128 } // namespace Global
129 } // namespace OHOS
130 #endif