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