1 /* 2 * Copyright (c) 2021-2025 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_RESCONFIGIMPL_H 16 #define OHOS_RESOURCE_MANAGER_RESCONFIGIMPL_H 17 18 #include <stdint.h> 19 #include "res_locale.h" 20 #include "res_common.h" 21 #include "res_config.h" 22 23 #ifdef SUPPORT_GRAPHICS 24 using icu::Locale; 25 #endif 26 namespace OHOS { 27 namespace Global { 28 namespace Resource { 29 class ResConfigImpl : public ResConfig { 30 public: 31 ResConfigImpl(); 32 33 static std::string GetCurrentDeviceType(); 34 35 static std::vector<std::string> GetAppSupportDeviceTypes(); 36 37 static DeviceType ParseDeviceTypeStr(const std::string &deviceType); 38 39 /** 40 * Whether this resConfig more match request resConfig 41 * @param other the other resConfig 42 * @param request the request resConfig 43 * @param density the input screen density 44 * @return true if this resConfig more match request resConfig than other resConfig, else false 45 */ 46 bool IsMoreSuitable(const std::shared_ptr<ResConfigImpl> other, const std::shared_ptr<ResConfigImpl> request, 47 uint32_t density = 0) const; 48 49 /** 50 * Set locale information 51 * @param language the locale language 52 * @param script the locale script 53 * @param region the locale region 54 * @return SUCCESS if set locale information success, else false 55 */ 56 RState SetLocaleInfo(const char *language, const char *script, const char *region); 57 58 /** 59 * Set locale information 60 * @param localeStr the locale info, for example, zh-Hans-CN. 61 * @return SUCCESS if set locale information success, else false 62 */ 63 RState SetLocaleInfo(const char *localeStr); 64 65 #ifdef SUPPORT_GRAPHICS 66 RState SetPreferredLocaleInfo(Locale &preferredLocaleInfo); 67 68 RState SetLocaleInfo(Locale &localeInfo); 69 #endif 70 /** 71 * Set resConfig device type 72 * @param deviceType the device type 73 */ 74 void SetDeviceType(DeviceType deviceType); 75 76 /** 77 * Set resConfig direction 78 * @param direction the resConfig direction 79 */ 80 void SetDirection(Direction direction); 81 82 /** 83 * Set resConfig colorMode 84 * @param colorMode the resConfig colorMode 85 */ 86 void SetColorMode(ColorMode colorMode); 87 88 /** 89 * Set resConfig mcc 90 * @param mcc the resConfig mcc 91 */ 92 void SetMcc(uint32_t mcc); 93 94 /** 95 * Set resConfig mnc 96 * @param mnc the resConfig mnc 97 */ 98 void SetMnc(uint32_t mnc); 99 100 /** 101 * Set resConfig screenDensity 102 * @param screenDensity the resConfig screenDensity 103 */ 104 void SetScreenDensity(float screenDensity); 105 106 /** 107 * Set resConfig screenDensityDpi 108 * @param screenDensityDpi the resConfig screenDensityDpi 109 */ 110 void SetScreenDensityDpi(ScreenDensity screenDensityDpi); 111 112 /** 113 * Set resConfig themeId 114 * @param themeId the resConfig themeId 115 */ 116 void SetThemeId(uint32_t themeId); 117 118 /** 119 * Set resConfig themeIcon 120 * @param isIcon the resConfig themeIcon flag 121 */ 122 void SetThemeIcon(bool isIcon); 123 124 #ifdef SUPPORT_GRAPHICS 125 const ResLocale *GetResPreferredLocale() const; 126 127 const Locale *GetPreferredLocaleInfo() const; 128 129 const Locale *GetLocaleInfo() const; 130 #endif 131 132 const ResLocale *GetResLocale() const; 133 134 Direction GetDirection() const; 135 136 float GetScreenDensity() const; 137 138 ScreenDensity GetScreenDensityDpi() const; 139 140 bool CopyLocaleAndPreferredLocale(const ResConfig &other); 141 142 bool isLocaleInfoSet(); 143 144 ColorMode GetColorMode() const; 145 146 uint32_t GetMcc() const; 147 148 uint32_t GetMnc() const; 149 150 uint32_t GetThemeId() const; 151 152 bool GetThemeIcon() const; 153 154 DeviceType GetDeviceType() const; 155 156 /** 157 * Whether this resConfig match other resConfig 158 * @param other the other resConfig 159 * @param isCheckDarkAdaptation whether check the color mode match the dark mode 160 * @return true if this resConfig match other resConfig, else false 161 */ 162 bool Match(const std::shared_ptr<ResConfigImpl> other, bool isCheckDarkAdaptation = true) const; 163 164 /** 165 * Copy other resConfig to this resConfig 166 * @param other the other resConfig 167 * @param isRead the isAppDarkRes_ is read-only 168 * @return true if copy other resConfig to this resConfig success, else false 169 */ 170 bool Copy(ResConfig &other, bool isRead = false); 171 172 /** 173 * Complete the local script 174 */ 175 void CompleteScript(); 176 177 /** 178 * Whether this resLocal script completed 179 * @return true if resLocal script completed, else false 180 */ 181 bool IsCompletedScript() const; 182 183 /** 184 * Set resConfig input device 185 * @param inputDevice the resConfig input device 186 */ 187 void SetInputDevice(InputDevice inputDevice); 188 189 /** 190 * Get resConfig input device 191 * @return the resConfig input device 192 */ 193 InputDevice GetInputDevice() const; 194 195 /** 196 * Matching screen density by screen density ratio 197 * @return ScreenDensity 198 */ 199 ScreenDensity ConvertDensity(float density); 200 201 virtual ~ResConfigImpl(); 202 203 /** 204 * Set colorMode is setted by app 205 * @param isAppColorMode whether colorMode is setted by app 206 */ 207 void SetAppColorMode(bool isAppColorMode); 208 209 /** 210 * Get whether colorMode is setted by app 211 * @return True if colorMode is setted by app 212 */ 213 bool GetAppColorMode() const; 214 215 /** 216 * Set whether the app has dark resources 217 * @param isAppDarkRes whether the app has dark resources 218 */ 219 void SetAppDarkRes(bool isAppDarkRes); 220 221 /** 222 * Get whether the app has dark resources 223 * @return True if the app has dark resources 224 */ 225 bool GetAppDarkRes() const; 226 227 bool MatchLocal(const ResConfig& other) const; 228 229 std::string ToString() const; 230 231 std::string GetDeviceTypeStr() const; 232 233 std::string GetScreenDensityStr() const; 234 235 /** 236 * Get whether the resconfig is invalid 237 * 238 * @return True if the resconfig is invalid 239 */ 240 bool IsInvalidResConfig(); 241 private: 242 bool IsMoreSpecificThan(const std::shared_ptr<ResConfigImpl> other, uint32_t density = 0) const; 243 244 bool CopyLocale(const ResConfig &other); 245 246 #ifdef SUPPORT_GRAPHICS 247 bool CopyPreferredLocale(const ResConfig &other); 248 249 bool CopyLocale(Locale **currentLocaleInfo, ResLocale **currentResLocale, 250 const Locale *otherLocaleInfo); 251 #endif 252 253 bool IsMccMncMatch(uint32_t mcc, uint32_t mnc) const; 254 255 bool IsDirectionMatch(Direction direction) const; 256 257 bool IsDeviceTypeMatch(DeviceType deviceType) const; 258 259 bool IsColorModeMatch(ColorMode colorMode, bool isCheckDarkAdaptation) const; 260 261 bool IsInputDeviceMatch(InputDevice inputDevice) const; 262 263 int IsMccMncMoreSuitable(uint32_t otherMcc, uint32_t otherMnc, uint32_t requestMcc, uint32_t requestMnc) const; 264 265 int IsDensityMoreSuitable(ScreenDensity otherDensity, ScreenDensity requestDensity, uint32_t density = 0) const; 266 267 bool IsDensityMoreSuitable(int thisDistance, int otherDistance) const; 268 269 int IsDensityMoreSpecificThan(ScreenDensity otherDensity, uint32_t density = 0) const; 270 271 #ifdef SUPPORT_GRAPHICS 272 RState BuildResLocale(const char *language, const char *script, const char *region, ResLocale **resLocale); 273 274 RState BuildLocaleInfo(const ResLocale *resLocale, Locale **localeInfo); 275 #endif 276 private: 277 ResLocale *resLocale_; 278 Direction direction_; 279 float density_; 280 ScreenDensity screenDensityDpi_; 281 ColorMode colorMode_; 282 uint32_t mcc_; 283 uint32_t mnc_; 284 DeviceType deviceType_; 285 InputDevice inputDevice_; 286 #ifdef SUPPORT_GRAPHICS 287 ResLocale *resPreferredLocale_; 288 Locale *preferredLocaleInfo_; 289 Locale *localeInfo_; 290 #endif 291 uint32_t themeId_; 292 bool isCompletedScript_; 293 bool isAppColorMode_; 294 bool isAppDarkRes_; 295 bool isThemeIcon_; 296 }; 297 } // namespace Resource 298 } // namespace Global 299 } // namespace OHOS 300 #endif