1 /*
2 * Copyright (c) 2020-2021 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 #include "acelite_config.h"
17
18 #if (FEATURE_COMPONENT_ANALOG_CLOCK == 1)
19
20 #include "ace_log.h"
21 #include "ace_mem_base.h"
22 #include "clock_hand_component.h"
23 #include "js_app_context.h"
24 #include "key_parser.h"
25 #include "keys.h"
26 #include "ui_image_view.h"
27
28 namespace OHOS {
29 namespace ACELite {
ClockHandComponent(jerry_value_t options,jerry_value_t children,AppStyleManager * styleManager)30 ClockHandComponent::ClockHandComponent(jerry_value_t options, jerry_value_t children, AppStyleManager *styleManager)
31 : Component(options, children, styleManager),
32 options_(options),
33 layoutType_(LayoutType::RECT),
34 clockHandView_(nullptr),
35 pivotX_(0),
36 pivotY_(0),
37 fillColor_(0),
38 opacity_(OPA_MAX),
39 handType_(nullptr)
40 {
41 SetComponentName(K_CLOCK_HAND);
42 combinedStyle_ = styleManager->GetCombinedStyle();
43 }
44
CreateNativeViews()45 bool ClockHandComponent::CreateNativeViews()
46 {
47 // Get clock hand layout type
48 layoutType_ = GetLayoutType();
49 if (layoutType_ == LayoutType::RECT) {
50 clockHandView_ = new UIView();
51 } else {
52 clockHandView_ = new UIImageView();
53 }
54
55 if (clockHandView_ == nullptr) {
56 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: create native view failed!");
57 return false;
58 }
59 return true;
60 }
61
ReleaseNativeViews()62 void ClockHandComponent::ReleaseNativeViews()
63 {
64 if (clockHandView_) {
65 delete clockHandView_;
66 clockHandView_ = nullptr;
67 }
68
69 if (handType_) {
70 ace_free(handType_);
71 handType_ = nullptr;
72 }
73 }
74
GetComponentRootView() const75 inline UIView *ClockHandComponent::GetComponentRootView() const
76 {
77 return clockHandView_;
78 }
79
PostRender()80 void ClockHandComponent::PostRender()
81 {
82 const char * const stylePivotX = "pivot-x";
83 const char * const stylePivotY = "pivot-y";
84 if ((combinedStyle_ == nullptr) || (clockHandView_ == nullptr)) {
85 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: post render failed!");
86 return;
87 }
88
89 // Verify hand type
90 if ((handType_ == nullptr)) {
91 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: hand type invalid!");
92 return;
93 }
94
95 // Set params to current js object
96 jerry_value_t pivotXValue = jerry_create_number(pivotX_);
97 jerry_value_t pivotYValue = jerry_create_number(pivotY_);
98 uint16_t typeId = KeyParser::ParseKeyId(handType_, strlen(handType_));
99 jerry_value_t typeIdValue = jerry_create_number(typeId);
100
101 jerry_value_t nativeObj = GetNativeElement();
102 if (jerry_value_is_undefined(nativeObj)) {
103 ReleaseJerryValue(pivotXValue, pivotYValue, typeIdValue, VA_ARG_END_FLAG);
104 return;
105 }
106 SetNamedProperty(nativeObj, stylePivotX, pivotXValue);
107 SetNamedProperty(nativeObj, stylePivotY, pivotYValue);
108 SetNamedProperty(nativeObj, ATTR_TYPE, typeIdValue);
109
110 jerry_value_t isImage;
111 if (layoutType_ == LayoutType::RECT) {
112 isImage = jerry_create_boolean(false);
113 jerry_value_t fillColorValue = jerry_create_number(fillColor_);
114 SetNamedProperty(nativeObj, COMMON_STYLE_COLOR, fillColorValue);
115 jerry_value_t opacityValue = jerry_create_number(opacity_);
116 SetNamedProperty(nativeObj, COMMON_STYLE_OPACITY, opacityValue);
117 } else {
118 isImage = jerry_create_boolean(true);
119 }
120 SetNamedProperty(nativeObj, CLOCK_HAND_IS_IMAGE, isImage);
121 }
122
ApplyPrivateStyle(const AppStyleItem * style)123 bool ClockHandComponent::ApplyPrivateStyle(const AppStyleItem *style)
124 {
125 if ((combinedStyle_ == nullptr) || (clockHandView_ == nullptr)) {
126 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: apply private style failed!");
127 return false;
128 }
129
130 uint16_t styleNameId = GetStylePropNameId(style);
131 if (!KeyParser::IsKeyValid(styleNameId)) {
132 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: id invalid when apply private style!");
133 return false;
134 }
135
136 switch (styleNameId) {
137 case K_PIVOT_X:
138 pivotX_ = GetStyleNumValue(style);
139 break;
140 case K_PIVOT_Y:
141 pivotY_ = GetStyleNumValue(style);
142 break;
143 case K_COLOR:
144 fillColor_ = GetStyleNumValue(style);
145 break;
146 case K_OPACITY:
147 opacity_ = GetStyleNumValue(style);
148 break;
149 default:
150 HILOG_DEBUG(HILOG_MODULE_ACE, "ClockHandComponent: style invalid when apply private style!");
151 return false;
152 }
153 return true;
154 }
155
SetPrivateAttribute(uint16_t attrKeyId,jerry_value_t attrValue)156 bool ClockHandComponent::SetPrivateAttribute(uint16_t attrKeyId, jerry_value_t attrValue)
157 {
158 if ((clockHandView_ == nullptr) || !KeyParser::IsKeyValid(attrKeyId) || IS_UNDEFINED(attrValue)) {
159 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: set private attribute failed!");
160 return false;
161 }
162
163 bool setResult = true;
164 switch (attrKeyId) {
165 case K_SRC: {
166 uint16_t length = 0;
167 char *attrValueStr = MallocStringOf(attrValue, &length);
168 if (attrValueStr == nullptr) {
169 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: image path invalid!");
170 setResult = false;
171 break;
172 }
173
174 if (length != 0) {
175 UIImageView *image = reinterpret_cast<UIImageView *>(clockHandView_);
176 if (image == nullptr) {
177 ace_free(attrValueStr);
178 attrValueStr = nullptr;
179 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: reinterpret_cast UIImageView failed!");
180 setResult = false;
181 break;
182 }
183
184 char *imagePath = JsAppContext::GetInstance()->GetResourcePath(attrValueStr);
185 if (imagePath != nullptr) {
186 image->SetSrc(imagePath);
187 ace_free(imagePath);
188 imagePath = nullptr;
189 }
190 }
191
192 ace_free(attrValueStr);
193 attrValueStr = nullptr;
194 break;
195 }
196 case K_TYPE:
197 ACE_FREE(handType_);
198 handType_ = MallocStringOf(attrValue);
199 break;
200 default:
201 setResult = false;
202 break;
203 }
204 return setResult;
205 }
206
SetNamedProperty(jerry_value_t obj,const char * const name,jerry_value_t value)207 void ClockHandComponent::SetNamedProperty(jerry_value_t obj, const char * const name, jerry_value_t value)
208 {
209 jerry_value_t propName = jerry_create_string((const jerry_char_t *)name);
210 jerry_release_value(jerry_set_property(obj, propName, value));
211 jerry_release_value(propName);
212 jerry_release_value(value);
213 }
214
GetStringAttrByName(const char * const name)215 char *ClockHandComponent::GetStringAttrByName(const char * const name)
216 {
217 if (jerry_value_is_undefined(options_)) {
218 HILOG_ERROR(HILOG_MODULE_ACE, "ClockHandComponent: options undefined!");
219 return nullptr;
220 }
221
222 char *strValue = nullptr;
223 jerry_value_t attrsPropName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(ATTR_ATTRS));
224 jerry_value_t attrsPropValue = jerry_get_property(options_, attrsPropName);
225 if (!jerry_value_is_undefined(attrsPropValue)) {
226 jerry_value_t attrPropName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(name));
227 jerry_value_t attrPropValue = jerry_get_property(attrsPropValue, attrPropName);
228 if (jerry_value_is_string(attrPropValue)) {
229 strValue = MallocStringOf(attrPropValue);
230 } else {
231 HILOG_INFO(HILOG_MODULE_ACE, "ClockHandComponent: attribute: %{public}s does not exits", name);
232 }
233 jerry_release_value(attrPropValue);
234 jerry_release_value(attrPropName);
235 } else {
236 HILOG_INFO(HILOG_MODULE_ACE, "ClockHandComponent: attrs undefined");
237 }
238 jerry_release_value(attrsPropValue);
239 jerry_release_value(attrsPropName);
240
241 return strValue;
242 }
243
GetLayoutType()244 ClockHandComponent::LayoutType ClockHandComponent::GetLayoutType()
245 {
246 LayoutType type = LayoutType::RECT;
247 char *handSrc = GetStringAttrByName(ATTR_SRC);
248 if (handSrc != nullptr) {
249 type = LayoutType::IMAGE;
250 ace_free(handSrc);
251 handSrc = nullptr;
252 }
253 return type;
254 }
255 } // namespace ACELite
256 } // namespace OHOS
257
258 #endif // FEATURE_COMPONENT_ANALOG_CLOCK
259