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