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 #ifndef OHOS_RESOURCE_MANAGER_RESCONFIG_IMPL_H 16 #define OHOS_RESOURCE_MANAGER_RESCONFIG_IMPL_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 /** 34 * Whether this resConfig more match request resConfig 35 * @param other the other resConfig 36 * @param request the request resConfig 37 * @param density the input screen density 38 * @return true if this resConfig more match request resConfig than other resConfig, else false 39 */ 40 bool IsMoreSuitable(const ResConfigImpl *other, const ResConfigImpl *request, uint32_t density = 0) const; 41 42 /** 43 * Set locale information 44 * @param language the locale language 45 * @param script the locale script 46 * @param region the locale region 47 * @return SUCCESS if set locale information success, else false 48 */ 49 RState SetLocaleInfo(const char *language, const char *script, const char *region); 50 51 #ifdef SUPPORT_GRAPHICS 52 RState SetPreferredLocaleInfo(Locale &preferredLocaleInfo); 53 54 RState SetLocaleInfo(Locale &localeInfo); 55 #endif 56 /** 57 * Set resConfig device type 58 * @param deviceType the device type 59 */ 60 void SetDeviceType(DeviceType deviceType); 61 62 /** 63 * Set resConfig direction 64 * @param direction the resConfig direction 65 */ 66 void SetDirection(Direction direction); 67 68 /** 69 * Set resConfig colorMode 70 * @param colorMode the resConfig colorMode 71 */ 72 void SetColorMode(ColorMode colorMode); 73 74 /** 75 * Set resConfig mcc 76 * @param mcc the resConfig mcc 77 */ 78 void SetMcc(uint32_t mcc); 79 80 /** 81 * Set resConfig mnc 82 * @param mnc the resConfig mnc 83 */ 84 void SetMnc(uint32_t mnc); 85 86 /** 87 * Set resConfig screenDensity 88 * @param screenDensity the resConfig screenDensity 89 */ 90 void SetScreenDensity(float screenDensity); 91 92 #ifdef SUPPORT_GRAPHICS 93 const ResLocale *GetResPreferredLocale() const; 94 95 const Locale *GetPreferredLocaleInfo() const; 96 97 const Locale *GetLocaleInfo() const; 98 #endif 99 100 const ResLocale *GetResLocale() const; 101 102 Direction GetDirection() const; 103 104 float GetScreenDensity() const; 105 106 ColorMode GetColorMode() const; 107 108 uint32_t GetMcc() const; 109 110 uint32_t GetMnc() const; 111 112 DeviceType GetDeviceType() const; 113 114 /** 115 * Whether this resConfig match other resConfig 116 * @param other the other resConfig 117 * @return true if this resConfig match other resConfig, else false 118 */ 119 bool Match(const ResConfigImpl *other) const; 120 121 /** 122 * Copy other resConfig to this resConfig 123 * @param other the other resConfig 124 * @return true if copy other resConfig to this resConfig success, else false 125 */ 126 bool Copy(ResConfig &other); 127 128 /** 129 * Complete the local script 130 */ 131 void CompleteScript(); 132 133 /** 134 * Whether this resLocal script completed 135 * @return true if resLocal script completed, else false 136 */ 137 bool IsCompletedScript() const; 138 139 /** 140 * Set resConfig input device 141 * @param inputDevice the resConfig input device 142 */ 143 void SetInputDevice(InputDevice inputDevice); 144 145 /** 146 * Get resConfig input device 147 * @return the resConfig input device 148 */ 149 InputDevice GetInputDevice() const; 150 151 /** 152 * Matching screen density by screen density ratio 153 * @return ScreenDensity 154 */ 155 ScreenDensity ConvertDensity(float density); 156 157 virtual ~ResConfigImpl(); 158 159 private: 160 bool IsMoreSpecificThan(const ResConfigImpl *other, uint32_t density = 0) const; 161 162 bool CopyLocale(ResConfig &other); 163 164 #ifdef SUPPORT_GRAPHICS 165 bool CopyPreferredLocale(ResConfig &other); 166 167 bool CopyLocale(Locale **currentLocaleInfo, ResLocale **currentResLocale, 168 const Locale *otherLocaleInfo); 169 #endif 170 171 bool IsMccMncMatch(uint32_t mcc, uint32_t mnc) const; 172 173 bool IsDirectionMatch(Direction direction) const; 174 175 bool IsDeviceTypeMatch(DeviceType deviceType) const; 176 177 bool IsColorModeMatch(ColorMode colorMode) const; 178 179 bool IsInputDeviceMatch(InputDevice inputDevice) const; 180 181 int IsMccMncMoreSuitable(uint32_t otherMcc, uint32_t otherMnc, uint32_t requestMcc, uint32_t requestMnc) const; 182 183 int IsDensityMoreSuitable(ScreenDensity otherDensity, ScreenDensity requestDensity, uint32_t density = 0) const; 184 185 bool IsDensityMoreSuitable(int thisDistance, int otherDistance) const; 186 187 int IsDensityMoreSpecificThan(ScreenDensity otherDensity, uint32_t density = 0) const; 188 189 RState BuildResLocale(const char *language, const char *script, const char *region, ResLocale **resLocale); 190 191 RState BuildLocaleInfo(const ResLocale *resLocale, Locale **localeInfo); 192 private: 193 ResLocale *resLocale_; 194 Direction direction_; 195 float density_; 196 ScreenDensity screenDensityDpi_; 197 ColorMode colorMode_; 198 uint32_t mcc_; 199 uint32_t mnc_; 200 DeviceType deviceType_; 201 InputDevice inputDevice_; 202 #ifdef SUPPORT_GRAPHICS 203 ResLocale *resPreferredLocale_; 204 Locale *preferredLocaleInfo_; 205 Locale *localeInfo_; 206 #endif 207 bool isCompletedScript_; 208 }; 209 } // namespace Resource 210 } // namespace Global 211 } // namespace OHOS 212 #endif