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