1 /*
2 * Copyright (c) 2023 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 "core/components_ng/pattern/picker/datepicker_accessibility_property.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/pattern/picker/datepicker_pattern.h"
21
22 namespace OHOS::Ace::NG {
23 namespace {
24 const int DOUBLE_DIGIT = 10;
25 const std::string COLON = ":";
26 const std::string ZERO = "0";
27 } // namespace
28
GetText() const29 std::string DatePickerAccessibilityProperty::GetText() const
30 {
31 auto frameNode = host_.Upgrade();
32 CHECK_NULL_RETURN(frameNode, "");
33 auto pattern = frameNode->GetPattern<DatePickerPattern>();
34 CHECK_NULL_RETURN(pattern, "");
35
36 std::string result;
37 if (pattern->GetIsShowInDialog() && pattern->ShowMonthDays()) {
38 result = GetColumnsText(frameNode, true);
39 result.append(" ");
40 result.append(GetShowTimePickerText());
41 } else {
42 result = GetColumnsText(frameNode, false);
43 }
44 return result;
45 }
46
GetColumnsText(const RefPtr<FrameNode> & frameNode,bool isMonthDaysDateNode) const47 std::string DatePickerAccessibilityProperty::GetColumnsText(
48 const RefPtr<FrameNode>& frameNode, bool isMonthDaysDateNode) const
49 {
50 CHECK_NULL_RETURN(frameNode, "");
51 std::string result = "";
52 for (const auto& child : frameNode->GetChildren()) {
53 auto stackColumn = AceType::DynamicCast<FrameNode>(child);
54 CHECK_NULL_RETURN(stackColumn, "");
55 auto blendColumn = AceType::DynamicCast<FrameNode>(stackColumn->GetLastChild());
56 CHECK_NULL_RETURN(blendColumn, "");
57 auto blendColumnLayoutProperty = blendColumn->GetLayoutProperty<LayoutProperty>();
58 CHECK_NULL_RETURN(blendColumnLayoutProperty, "");
59 if (blendColumnLayoutProperty->GetVisibility() == VisibleType::GONE) {
60 continue;
61 }
62 auto dataColumnNode = AceType::DynamicCast<FrameNode>(blendColumn->GetLastChild());
63 CHECK_NULL_RETURN(dataColumnNode, "");
64 auto columnPattern = dataColumnNode->GetPattern<DatePickerColumnPattern>();
65 CHECK_NULL_RETURN(columnPattern, "");
66 auto index = columnPattern->GetCurrentIndex();
67 auto options = columnPattern->GetOptions();
68 auto it = options.find(dataColumnNode);
69 if (it != options.end() && index >= 0 && index < it->second.size()) {
70 auto date = it->second.at(index);
71 result.append(DatePickerPattern::GetFormatString(date));
72 }
73 if (isMonthDaysDateNode) {
74 break;
75 }
76 }
77 return result;
78 }
79
GetShowTimePickerText() const80 std::string DatePickerAccessibilityProperty::GetShowTimePickerText() const
81 {
82 auto frameNode = host_.Upgrade();
83 CHECK_NULL_RETURN(frameNode, "");
84 auto parentNode = frameNode->GetParentFrameNode();
85 auto timeNode = AceType::DynamicCast<FrameNode>(parentNode->GetChildAtIndex(1));
86 CHECK_NULL_RETURN(timeNode, "");
87 auto timePickerRowPattern = timeNode->GetPattern<NG::TimePickerRowPattern>();
88 CHECK_NULL_RETURN(timePickerRowPattern, "");
89 auto allChildNode = timePickerRowPattern->GetAllChildNode();
90 auto hourColumn = allChildNode["hour"].Upgrade();
91 CHECK_NULL_RETURN(hourColumn, "");
92 auto hourPickerColumnPattern = hourColumn->GetPattern<TimePickerColumnPattern>();
93 CHECK_NULL_RETURN(hourPickerColumnPattern, "");
94
95 std::string result;
96 auto hour = static_cast<int32_t>(hourPickerColumnPattern->GetCurrentIndex());
97 if (!timePickerRowPattern->GetHour24()) {
98 hour += 1;
99 }
100 std::string textHour = std::to_string(hour);
101 if (hour < DOUBLE_DIGIT) {
102 if (!Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
103 textHour = ZERO + textHour;
104 } else if (timePickerRowPattern->GetHour24()) {
105 if (timePickerRowPattern->GetPrefixHour() != ZeroPrefixType::HIDE) {
106 textHour = ZERO + textHour;
107 }
108 } else {
109 if (timePickerRowPattern->GetPrefixHour() == ZeroPrefixType::SHOW) {
110 textHour = ZERO + textHour;
111 }
112 }
113 }
114 result += textHour;
115
116 GetMinuteText(timeNode, result);
117 GetSecondText(timeNode, result);
118 if (!timePickerRowPattern->GetHour24()) {
119 auto amPmColumn = allChildNode["amPm"].Upgrade();
120 CHECK_NULL_RETURN(amPmColumn, "");
121 auto amPmPickerColumnPattern = amPmColumn->GetPattern<TimePickerColumnPattern>();
122 CHECK_NULL_RETURN(amPmPickerColumnPattern, "");
123 auto optionIndex = amPmPickerColumnPattern->GetCurrentIndex();
124 if (optionIndex >= 0) {
125 result = timePickerRowPattern->GetOptionValue(amPmColumn, optionIndex).append(" ") + result;
126 }
127 }
128 return result;
129 }
130
GetMinuteText(const RefPtr<FrameNode> & frameNode,std::string & result) const131 void DatePickerAccessibilityProperty::GetMinuteText(const RefPtr<FrameNode>& frameNode, std::string& result) const
132 {
133 CHECK_NULL_VOID(frameNode);
134 auto timePickerRowPattern = frameNode->GetPattern<NG::TimePickerRowPattern>();
135 CHECK_NULL_VOID(timePickerRowPattern);
136 auto allChildNode = timePickerRowPattern->GetAllChildNode();
137 auto minuteColumn = allChildNode["minute"].Upgrade();
138 CHECK_NULL_VOID(minuteColumn);
139
140 auto minutePickerColumnPattern = minuteColumn->GetPattern<TimePickerColumnPattern>();
141 CHECK_NULL_VOID(minutePickerColumnPattern);
142 int minute = static_cast<int>(minutePickerColumnPattern->GetCurrentIndex());
143 std::string textMinute = std::to_string(minute);
144 if (minute < DOUBLE_DIGIT) {
145 if (timePickerRowPattern->GetPrefixMinute() != ZeroPrefixType::HIDE) {
146 textMinute = ZERO + textMinute;
147 }
148 }
149 result += COLON + textMinute;
150 }
151
GetSecondText(const RefPtr<FrameNode> & frameNode,std::string & result) const152 void DatePickerAccessibilityProperty::GetSecondText(const RefPtr<FrameNode>& frameNode, std::string& result) const
153 {
154 CHECK_NULL_VOID(frameNode);
155 auto timePickerRowPattern = frameNode->GetPattern<NG::TimePickerRowPattern>();
156 CHECK_NULL_VOID(timePickerRowPattern);
157 if (timePickerRowPattern->GetHasSecond()) {
158 auto allChildNode = timePickerRowPattern->GetAllChildNode();
159 auto secondColumn = allChildNode["second"].Upgrade();
160 CHECK_NULL_VOID(secondColumn);
161 auto secondPickerColumnPattern = secondColumn->GetPattern<TimePickerColumnPattern>();
162 CHECK_NULL_VOID(secondPickerColumnPattern);
163 int second = static_cast<int>(secondPickerColumnPattern->GetCurrentIndex());
164 std::string textSecond = std::to_string(second);
165 if (second < DOUBLE_DIGIT) {
166 if (timePickerRowPattern->GetPrefixSecond() != ZeroPrefixType::HIDE) {
167 textSecond = ZERO + textSecond;
168 }
169 }
170 result += COLON + textSecond;
171 }
172 }
173 } // namespace OHOS::Ace::NG
174