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 FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_RESOURCE_CONFIGURATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_RESOURCE_CONFIGURATION_H 18 19 #include <string> 20 #include <vector> 21 22 #include "base/json/json_util.h" 23 #include "base/utils/device_type.h" 24 #include "base/utils/device_config.h" 25 26 namespace OHOS::Ace { 27 28 class ResourceConfiguration { 29 public: 30 static constexpr uint32_t COLOR_MODE_UPDATED_FLAG = 0x00000001; 31 static constexpr uint32_t FONT_RATIO_UPDATED_FLAG = 0x00000002; TestFlag(uint32_t flags,uint32_t flag)32 static bool TestFlag(uint32_t flags, uint32_t flag) 33 { 34 return (flags & flag); 35 } 36 37 public: SetDeviceType(const DeviceType & deviceType)38 void SetDeviceType(const DeviceType& deviceType) 39 { 40 deviceType_ = deviceType; 41 } 42 GetDeviceType()43 DeviceType GetDeviceType() const 44 { 45 return deviceType_; 46 } 47 SetOrientation(const DeviceOrientation & orientation)48 void SetOrientation(const DeviceOrientation& orientation) 49 { 50 orientation_ = orientation; 51 } 52 GetOrientation()53 DeviceOrientation GetOrientation() const 54 { 55 return orientation_; 56 } 57 SetDensity(const double & density)58 void SetDensity(const double& density) 59 { 60 density_ = density; 61 } 62 GetDensity()63 double GetDensity() const 64 { 65 return density_; 66 } 67 SetFontRatio(double fontRatio)68 void SetFontRatio(double fontRatio) 69 { 70 fontRatio_ = fontRatio; 71 } 72 GetFontRatio()73 double GetFontRatio() const 74 { 75 return fontRatio_; 76 } 77 SetColorMode(const ColorMode & colorMode)78 void SetColorMode(const ColorMode& colorMode) 79 { 80 colorMode_ = colorMode; 81 } 82 GetColorMode()83 ColorMode GetColorMode() const 84 { 85 return colorMode_; 86 } 87 SetDeviceAccess(bool isDeviceAccess)88 void SetDeviceAccess(bool isDeviceAccess) 89 { 90 isDeviceAccess_ = isDeviceAccess; 91 } 92 GetDeviceAccess()93 bool GetDeviceAccess() const 94 { 95 return isDeviceAccess_; 96 } 97 98 bool UpdateFromJsonString(const std::string jsonStr, uint32_t& updateFlags); 99 100 private: 101 bool ParseJsonColorMode(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags); 102 bool ParseJsonFontRatio(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags); 103 104 private: 105 DeviceType deviceType_ = DeviceType::PHONE; 106 DeviceOrientation orientation_ = DeviceOrientation::PORTRAIT; 107 double density_ = 1.0; 108 double fontRatio_ = 1.0; 109 bool isDeviceAccess_ = false; 110 ColorMode colorMode_ = ColorMode::LIGHT; 111 }; 112 113 class ResourceInfo { 114 public: SetResourceConfiguration(const ResourceConfiguration & resourceConfiguration)115 void SetResourceConfiguration(const ResourceConfiguration& resourceConfiguration) 116 { 117 resourceConfiguration_ = resourceConfiguration; 118 } 119 GetResourceConfiguration()120 ResourceConfiguration GetResourceConfiguration() const 121 { 122 return resourceConfiguration_; 123 } 124 SetResourceHandlers(const std::vector<int64_t> & resourcehandlers)125 void SetResourceHandlers(const std::vector<int64_t>& resourcehandlers) 126 { 127 resourcehandlers_ = resourcehandlers; 128 } 129 GetResourceHandlers()130 std::vector<int64_t> GetResourceHandlers() const 131 { 132 return resourcehandlers_; 133 } 134 SetHapPath(const std::string & hapPath)135 void SetHapPath(const std::string& hapPath) 136 { 137 hapPath_ = hapPath; 138 } 139 GetHapPath()140 std::string GetHapPath() const 141 { 142 return hapPath_; 143 } 144 SetPackagePath(const std::string & packagePath)145 void SetPackagePath(const std::string& packagePath) 146 { 147 packagePath_ = packagePath; 148 } 149 GetPackagePath()150 std::string GetPackagePath() const 151 { 152 return packagePath_; 153 } 154 155 #if defined(PREVIEW) SetSystemPackagePath(const std::string & systemPackagePath)156 void SetSystemPackagePath(const std::string& systemPackagePath) 157 { 158 systemPackagePath_ = systemPackagePath; 159 } 160 GetSystemPackagePath()161 std::string GetSystemPackagePath() const 162 { 163 return systemPackagePath_; 164 } 165 #endif 166 SetThemeId(uint32_t themeId)167 void SetThemeId(uint32_t themeId) 168 { 169 themeId_ = static_cast<int32_t>(themeId); 170 } 171 GetThemeId()172 int32_t GetThemeId() const 173 { 174 return themeId_; 175 } 176 177 private: 178 ResourceConfiguration resourceConfiguration_; 179 std::vector<int64_t> resourcehandlers_; 180 std::string hapPath_; 181 std::string packagePath_; 182 #if defined(PREVIEW) 183 std::string systemPackagePath_; 184 #endif 185 int32_t themeId_ = -1; 186 }; 187 188 } // namespace OHOS::Ace 189 190 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_RESOURCE_CONFIGURATION_H 191