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 "analog_clock_component.h"
22 #include "keys.h"
23 #include "ui_image_view.h"
24
25 namespace OHOS {
26 namespace ACELite {
AnalogClockComponent(jerry_value_t options,jerry_value_t children,AppStyleManager * styleManager)27 AnalogClockComponent::AnalogClockComponent(jerry_value_t options, jerry_value_t children, AppStyleManager* styleManager)
28 : Component(options, children, styleManager), hour_(0), min_(0), sec_(0), children_(children), clockView_(nullptr),
29 {
30 SetComponentName(K_ANALOG_CLOCK);
31 }
32
CreateNativeViews()33 bool AnalogClockComponent::CreateNativeViews()
34 {
35 clockView_ = new UIAnalogClock();
36 if (clockView_ == nullptr) {
37 HILOG_ERROR(HILOG_MODULE_ACE, "AnalogClockComponent: create native view failed!");
38 return false;
39 }
40 return true;
41 }
42
ReleaseNativeViews()43 void AnalogClockComponent::ReleaseNativeViews()
44 {
45 if (clockView_) {
46 delete clockView_;
47 clockView_ = nullptr;
48 }
49 }
50
GetComponentRootView() const51 inline UIView* AnalogClockComponent::GetComponentRootView() const
52 {
53 return clockView_;
54 }
55
SetPrivateAttribute(uint16_t attrKeyId,jerry_value_t attrValue)56 bool AnalogClockComponent::SetPrivateAttribute(uint16_t attrKeyId, jerry_value_t attrValue)
57 {
58 if ((clockView_ == nullptr) || !KeyParser::IsKeyValid(attrKeyId) || IS_UNDEFINED(attrValue)) {
59 HILOG_ERROR(HILOG_MODULE_ACE, "AnalogClockComponent: set private attribute failed!");
60 return false;
61 }
62 switch (attrKeyId) {
63 case K_HOUR:
64 hour_ = IntegerOf(attrValue);
65 break;
66 case K_MIN:
67 min_ = IntegerOf(attrValue);
68 break;
69 case K_SEC:
70 sec_ = IntegerOf(attrValue);
71 break;
72 default:
73 return false;
74 }
75 UpdateClock();
76 return true;
77 }
78
UpdateClock()79 void AnalogClockComponent::UpdateClock()
80 {
81 if (clockView_ == nullptr) {
82 HILOG_ERROR(HILOG_MODULE_ACE, "AnalogClockComponent: update clock failed!");
83 return;
84 }
85 clockView_->SetTime24Hour(hour_, min_, sec_);
86 }
87
ProcessChildren()88 bool AnalogClockComponent::ProcessChildren()
89 {
90 if (jerry_value_is_undefined(children_) || (clockView_ == nullptr)) {
91 HILOG_ERROR(HILOG_MODULE_ACE, "AnalogClockComponent: process children failed!");
92 return false;
93 }
94
95 uint16_t length = jerry_get_array_length(children_);
96 for (uint16_t index = 0; index < length; index++) {
97 jerry_value_t child = jerry_get_property_by_index(children_, index);
98 if (jerry_value_is_undefined(child)) {
99 continue;
100 }
101 UIView* childView = ComponentUtils::GetViewFromBindingObject(child);
102 if (childView == nullptr) {
103 jerry_release_value(child);
104 continue;
105 }
106
107 uint16_t handType = GetIntegerProperty(child, ATTR_TYPE);
108 if (handType > 0) { // clock-hand
109 bool isImage = GetBoolProperty(child, CLOCK_HAND_IS_IMAGE);
110 if (isImage) {
111 SetImageHand(handType, child, childView);
112 } else {
113 SetRectHand(handType, child, childView);
114 }
115 } else { // clock background
116 clockView_->Add(childView);
117 }
118 jerry_release_value(child);
119 }
120 clockView_->SetInitTime24Hour(hour_, min_, sec_);
121 clockView_->SetDragParentInstead(true);
122 return true;
123 }
124
SetImageHand(uint16_t handType,jerry_value_t child,UIView * childView)125 void AnalogClockComponent::SetImageHand(uint16_t handType, jerry_value_t child, UIView* childView)
126 {
127 UIImageView* imageView = reinterpret_cast<UIImageView*>(childView);
128 if (imageView == nullptr) {
129 HILOG_ERROR(HILOG_MODULE_ACE, "AnalogClockComponent: childView reinterpret_cast failed!");
130 return;
131 }
132 int16_t posX = imageView->GetX();
133 int16_t posY = imageView->GetY();
134 int16_t pivotX = GetIntegerProperty(child, clockHandPivotX);
135 int16_t pivotY = GetIntegerProperty(child, clockHandPivotY);
136 switch (handType) {
137 case K_HOUR:
138 clockView_->SetHandImage(UIAnalogClock::HandType::HOUR_HAND,
139 *imageView, {posX, posY}, {pivotX, pivotY});
140 break;
141 case K_MIN:
142 clockView_->SetHandImage(UIAnalogClock::HandType::MINUTE_HAND,
143 *imageView, {posX, posY}, {pivotX, pivotY});
144 break;
145 case K_SEC:
146 clockView_->SetHandImage(UIAnalogClock::HandType::SECOND_HAND,
147 *imageView, {posX, posY}, {pivotX, pivotY});
148 break;
149 default:
150 HILOG_ERROR(HILOG_MODULE_ACE, "AnalogClockComponent: UIImageView hand type error!");
151 break;
152 }
153 }
154
SetRectHand(uint16_t handType,jerry_value_t child,UIView * childView)155 void AnalogClockComponent::SetRectHand(uint16_t handType, jerry_value_t child, UIView* childView)
156 {
157 int16_t posX = childView->GetX();
158 int16_t posY = childView->GetY();
159 int16_t width = childView->GetWidth();
160 int16_t height = childView->GetHeight();
161 int16_t pivotX = GetIntegerProperty(child, clockHandPivotX);
162 int16_t pivotY = GetIntegerProperty(child, clockHandPivotY);
163 int32_t fillColor = GetIntegerProperty(child, COMMON_STYLE_COLOR);
164 ColorType colorRGB = GetRGBColor(fillColor);
165 uint16_t opacity = GetIntegerProperty(child, COMMON_STYLE_OPACITY);
166 switch (handType) {
167 case K_HOUR:
168 clockView_->SetHandLine(UIAnalogClock::HandType::HOUR_HAND, {posX, posY},
169 {pivotX, pivotY}, colorRGB, width, height, opacity);
170 break;
171 case K_MIN:
172 clockView_->SetHandLine(UIAnalogClock::HandType::MINUTE_HAND, {posX, posY},
173 {pivotX, pivotY}, colorRGB, width, height, opacity);
174 break;
175 case K_SEC:
176 clockView_->SetHandLine(UIAnalogClock::HandType::SECOND_HAND, {posX, posY},
177 {pivotX, pivotY}, colorRGB, width, height, opacity);
178 break;
179 default:
180 HILOG_ERROR(HILOG_MODULE_ACE, "AnalogClockComponent: rectangle hand type error!");
181 break;
182 }
183 }
184
GetIntegerProperty(jerry_value_t obj,const char * const name)185 int32_t AnalogClockComponent::GetIntegerProperty(jerry_value_t obj, const char * const name)
186 {
187 jerry_value_t propName = jerry_create_string((const jerry_char_t*)name);
188 jerry_value_t propValue = jerry_get_property(obj, propName);
189
190 jerry_value_t target = jerry_value_to_number(propValue);
191 int32_t res = (int32_t)jerry_get_number_value(target);
192 jerry_release_value(target);
193 jerry_release_value(propValue);
194 jerry_release_value(propName);
195 return res;
196 }
197
GetBoolProperty(jerry_value_t obj,const char * const name)198 bool AnalogClockComponent::GetBoolProperty(jerry_value_t obj, const char * const name)
199 {
200 jerry_value_t propName = jerry_create_string((const jerry_char_t*)name);
201 jerry_value_t propValue = jerry_get_property(obj, propName);
202
203 bool res = jerry_value_to_boolean(propValue);
204 jerry_release_value(propValue);
205 jerry_release_value(propName);
206 return res;
207 }
208 } // namespace ACELite
209 } // namespace OHOS
210
211 #endif // FEATURE_COMPONENT_ANALOG_CLOCK
212