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 
16 #ifndef OHOS_ABILITY_BASE_CONFIGURATION_H
17 #define OHOS_ABILITY_BASE_CONFIGURATION_H
18 
19 #include <mutex>
20 #include <set>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "parcel.h"
26 #include "global_configuration_key.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace ConfigurationInner {
31     constexpr const char* CONNECTION_SYMBOL = "#";
32     constexpr const char* EMPTY_STRING = "";
33     constexpr const char* APPLICATION_DIRECTION = "ohos.application.direction";
34     constexpr const char* APPLICATION_DENSITYDPI = "ohos.application.densitydpi";
35     constexpr const char* APPLICATION_DISPLAYID = "ohos.application.displayid";
36     constexpr const char* APPLICATION_FONT = "ohos.application.font";
37 
38     constexpr const char* COLOR_MODE_LIGHT = "light";
39     constexpr const char* COLOR_MODE_DARK = "dark";
40     constexpr const char* COLOR_MODE_AUTO = "auto";
41     constexpr const char* DEVICE_TYPE_DEFAULT = "default";
42     constexpr const char* DIRECTION_VERTICAL = "vertical";
43     constexpr const char* DIRECTION_HORIZONTAL = "horizontal";
44     constexpr const char* IS_SET_BY_APP = "isSetByApp";
45     constexpr const char* NEED_REMOVE_SET_BY_SA = "needRemoveSetBySa";
46     constexpr const char* IS_SET_BY_SA = "isSetBySa";
47     constexpr const char* IS_APP_FONT_FOLLOW_SYSTEM = "followSystem";
48     constexpr const char* SYSTEM_DEFAULT_FONTSIZE_SCALE = "1.0";
49 };
50 
51 class Configuration final: public Parcelable {
52 public:
53     Configuration();
54 
55     Configuration(const Configuration &other);
56 
57     Configuration& operator=(const Configuration &other);
58 
59     ~Configuration();
60 
61     /**
62      * @brief Compare the difference between the current and the passed in object.
63      *
64      * @param diffKeyV Out Ginseng. get the current difference item keys.
65      * @param other Comparisons obj
66      *
67      * @return void
68      */
69     void CompareDifferent(std::vector<std::string> &diffKeyV, const Configuration &other);
70 
71     /**
72      * @brief Update the content according to the key.
73      *
74      * @param mergeItemKey The key of the element currently to be updated.
75      * @param other Provide updated content obj
76      *
77      * @return void
78      */
79     void Merge(const std::vector<std::string> &diffKeyV, const Configuration &other);
80 
81     /**
82      * @brief obtain the value according to the display number and storage key.
83      *
84      * @param displayId Currently displayed id.
85      * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
86      * Means you want to change the language part
87      * @param value Changed value
88      * @return return true if the deposit is successful, otherwise return false
89      */
90     bool AddItem(int displayId, const std::string &key, const std::string &value);
91 
92     /**
93      * @brief obtain the value according to the display number and storage key.
94      *
95      * @param displayId Currently displayed id.
96      * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
97      * Means you want to change the language part
98      *
99      * @return return empty string if not found | return val if found
100      */
101     std::string GetItem(int displayId, const std::string &key) const;
102 
103     /**
104      * @brief Delete element.
105      *
106      * @param displayId Currently displayed id.
107      * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
108      * Means you want to change the language part
109      *
110      * @return Return an integer greater than 0 if the deletion succeeds, otherwise it returns 0.
111      */
112     int RemoveItem(int displayId, const std::string &key);
113 
114     /**
115      * @brief obtain the value according to the display number and storage key.
116      *
117      * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
118      * Means you want to change the language part
119      * @param value Changed value
120      * @return return true if the deposit is successful, otherwise return false
121      */
122     bool AddItem(const std::string &key, const std::string &value);
123 
124     /**
125      * @brief obtain the value according to the display number and storage key.
126      *
127      * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
128      * Means you want to change the language part
129      *
130      * @return return empty string if not found | return val if found
131      */
132     std::string GetItem(const std::string &key) const;
133 
134     /**
135      * @brief Delete element.
136      *
137      * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
138      * Means you want to change the language part
139      *
140      * @return Return an integer greater than 0 if the deletion succeeds, otherwise it returns 0.
141      */
142     int RemoveItem(const std::string &key);
143 
144     /**
145      * @brief Get the currently existing key-value pairs.
146      *
147      * @return return currently item size.
148      */
149     int GetItemSize() const;
150 
151     /**
152      * @brief Return all current key-value pairs.
153      *
154      */
155     const std::string GetName() const;
156 
157     /**
158      * @brief read this Sequenceable object from a Parcel.
159      *
160      * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
161      * @return Returns true if read successed; returns false otherwise.
162      */
163     bool ReadFromParcel(Parcel &parcel);
164 
165     /**
166      * @brief Marshals this Sequenceable object into a Parcel.
167      *
168      * @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled.
169      */
170     virtual bool Marshalling(Parcel &parcel) const override;
171 
172     /**
173      * @brief Unmarshals this Sequenceable object from a Parcel.
174      *
175      * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
176      */
177     static Configuration *Unmarshalling(Parcel &parcel);
178 
179 private:
180 
181     /**
182      * @brief Make the key by id and param
183      *
184      * @param getKey Key made.
185      * @param id displayId.
186      * @param param The key of the item to access configura.
187      *
188      */
189     bool MakeTheKey(std::string &getKey, int id, const std::string &param) const;
190 
191     /**
192      * @brief Get all current keys.
193      *
194      * @param keychain Out Ginseng. Contains all current keys.
195      */
196     void GetAllKey(std::vector<std::string> &keychain) const;
197 
198     /**
199      * @brief Get value by key.
200      *
201      * @param key the key to get value.
202      */
203     std::string GetValue(const std::string &key) const;
204 
205 private:
206     int defaultDisplayId_ {0};
207 
208     mutable std::recursive_mutex configParameterMutex_;
209     std::unordered_map<std::string, std::string> configParameter_;
210 };
211 }  // namespace AppExecFwk
212 }  // namespace OHOS
213 #endif  // OHOS_ABILITY_BASE_CONFIGURATION_H
214