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_ACE_ENGINE_ADAPTER_OHOS_ENTRANCE_UTILS_H
17 #define FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_ENTRANCE_UTILS_H
18
19 #include <string>
20
21 #include "base/log/log.h"
22 #include "base/geometry/rect.h"
23 #include "core/common/asset_manager_impl.h"
24 #include "core/components_ng/property/safe_area_insets.h"
25
26 namespace OHOS::Rosen {
27 class AvoidArea;
28 struct DMRect;
29 }
30 namespace OHOS::Ace {
31
32 std::string GetStringFromFile(const std::string& packagePathStr, const std::string& fileName);
33 std::string GetStringFromHap(const std::string& hapPath, const std::string& fileName);
34 bool CheckUrlValid(const std::string& url, const std::string& hapPath);
35
36 RefPtr<AssetProviderImpl> CreateAssetProviderImpl(
37 const std::string& packagePath, const std::vector<std::string>& assetBasePaths, bool useCache = true);
38
GenerateFullPath(const std::string& prePath, const std::string& postPath)39 inline const std::string GenerateFullPath(const std::string& prePath, const std::string& postPath)
40 {
41 if (prePath.empty() && postPath.empty()) {
42 return "";
43 }
44 std::string tmpPostPath = postPath;
45 if (tmpPostPath.back() == '/') {
46 tmpPostPath.pop_back();
47 }
48 std::string fullPath = prePath;
49 if (fullPath.empty() || fullPath.back() == '/') {
50 fullPath += tmpPostPath;
51 } else {
52 fullPath += "/" + tmpPostPath;
53 }
54 return fullPath;
55 }
56
57 enum class ResultSetDataType {
58 TYPE_NULL = 0,
59 TYPE_STRING,
60 TYPE_INT32,
61 TYPE_INT64,
62 TYPE_DOUBLE
63 };
64
65 class ResultSetUtils {
66 public:
67 template<typename T>
GetValFromColumn(const std::string &columnName, T &resultSet, ResultSetDataType type)68 static std::variant<int32_t, std::string, int64_t, double> GetValFromColumn(const std::string &columnName,
69 T &resultSet, ResultSetDataType type)
70 {
71 if (resultSet == nullptr) {
72 return DefaultVariantVal(type);
73 }
74
75 int32_t err = 0;
76 int32_t index = 0;
77 err = resultSet->GetColumnIndex(columnName, index);
78 if (err) {
79 return DefaultVariantVal(type);
80 }
81
82 std::variant<int32_t, std::string, int64_t, double> data;
83 switch (type) {
84 case ResultSetDataType::TYPE_STRING: {
85 data = GetStringValFromColumn(index, resultSet);
86 break;
87 }
88 case ResultSetDataType::TYPE_INT32: {
89 data = GetIntValFromColumn(index, resultSet);
90 break;
91 }
92 case ResultSetDataType::TYPE_INT64: {
93 data = GetLongValFromColumn(index, resultSet);
94 break;
95 }
96 case ResultSetDataType::TYPE_DOUBLE: {
97 data = GetDoubleValFromColumn(index, resultSet);
98 break;
99 }
100 default: {
101 return DefaultVariantVal(type);
102 break;
103 }
104 }
105
106 return data;
107 }
108
109 template<typename T>
GetStringValFromColumn(int index, T &resultSet)110 static inline std::string GetStringValFromColumn(int index, T &resultSet)
111 {
112 std::string stringVal;
113 if (resultSet->GetString(index, stringVal)) {
114 return "";
115 }
116 return stringVal;
117 }
118
119 template<typename T>
GetIntValFromColumn(int index, T &resultSet)120 static inline int32_t GetIntValFromColumn(int index, T &resultSet)
121 {
122 int32_t integerVal;
123 if (resultSet->GetInt(index, integerVal)) {
124 return 0;
125 }
126 return integerVal;
127 }
128
129 template<typename T>
GetLongValFromColumn(int index, T &resultSet)130 static inline int64_t GetLongValFromColumn(int index, T &resultSet)
131 {
132 int64_t integer64Val;
133 if (resultSet->GetLong(index, integer64Val)) {
134 return 0;
135 }
136 return integer64Val;
137 }
138
139 template<typename T>
GetDoubleValFromColumn(int index, T &resultSet)140 static inline double GetDoubleValFromColumn(int index, T &resultSet)
141 {
142 double doubleVal;
143 if (resultSet->GetDouble(index, doubleVal)) {
144 return 0;
145 }
146 return doubleVal;
147 }
148
149 private:
DefaultVariantVal(ResultSetDataType type)150 static std::variant<int32_t, std::string, int64_t, double> DefaultVariantVal(ResultSetDataType type)
151 {
152 switch (type) {
153 case ResultSetDataType::TYPE_STRING:
154 return std::string();
155 case ResultSetDataType::TYPE_INT32:
156 return 0;
157 case ResultSetDataType::TYPE_INT64:
158 return static_cast<int64_t>(0);
159 case ResultSetDataType::TYPE_DOUBLE:
160 return static_cast<double>(0.0);
161 default:
162 return 0;
163 }
164
165 return 0;
166 }
167 };
168
169 NG::SafeAreaInsets ConvertAvoidArea(const OHOS::Rosen::AvoidArea& avoidArea);
170 Rect ConvertDMRect2Rect(const OHOS::Rosen::DMRect& displayAvailableRect);
171 } // namespace OHOS::Ace
172
173 #endif // FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_ENTRANCE_UTILS_H
174