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 auto pickerDate = pattern->GetCurrentDate();
37
38 std::string result;
39 if (pattern->GetIsShowInDialog() && pattern->ShowMonthDays()) {
40 result = GetColumnsText(frameNode, true);
41 result.append(" ");
42 result.append(GetShowTimePickerText());
43 } else {
44 if (!pattern->IsShowLunar()) {
45 result = std::to_string(pickerDate.GetYear()) + "-" + std::to_string(pickerDate.GetMonth()) + "-" +
46 std::to_string(pickerDate.GetDay());
47 } else {
48 result = GetColumnsText(frameNode, false);
49 }
50 }
51 return result;
52 }
53
GetColumnsText(const RefPtr<FrameNode> & frameNode,bool isMonthDaysDateNode) const54 std::string DatePickerAccessibilityProperty::GetColumnsText(
55 const RefPtr<FrameNode>& frameNode, bool isMonthDaysDateNode) const
56 {
57 CHECK_NULL_RETURN(frameNode, "");
58 std::string result = "";
59 for (const auto& child : frameNode->GetChildren()) {
60 auto stackMonthDays = AceType::DynamicCast<FrameNode>(child);
61 CHECK_NULL_RETURN(stackMonthDays, "");
62 auto blendMonthDays = AceType::DynamicCast<FrameNode>(stackMonthDays->GetLastChild());
63 CHECK_NULL_RETURN(blendMonthDays, "");
64 auto monthDaysColumnNode = AceType::DynamicCast<FrameNode>(blendMonthDays->GetLastChild());
65 CHECK_NULL_RETURN(monthDaysColumnNode, "");
66 auto columnPattern = monthDaysColumnNode->GetPattern<DatePickerColumnPattern>();
67 CHECK_NULL_RETURN(columnPattern, "");
68 auto index = columnPattern->GetCurrentIndex();
69 auto options = columnPattern->GetOptions();
70 auto it = options.find(monthDaysColumnNode);
71 if (it != options.end() && index >= 0 && index < it->second.size()) {
72 auto date = it->second.at(index);
73 result.append(DatePickerPattern::GetFormatString(date));
74 }
75 if (isMonthDaysDateNode) {
76 break;
77 }
78 }
79 return result;
80 }
81
GetShowTimePickerText() const82 std::string DatePickerAccessibilityProperty::GetShowTimePickerText() const
83 {
84 auto frameNode = host_.Upgrade();
85 CHECK_NULL_RETURN(frameNode, "");
86 auto parentNode = frameNode->GetParentFrameNode();
87 auto timeNode = AceType::DynamicCast<FrameNode>(parentNode->GetChildAtIndex(1));
88 CHECK_NULL_RETURN(timeNode, "");
89 auto timePickerRowPattern = timeNode->GetPattern<NG::TimePickerRowPattern>();
90 CHECK_NULL_RETURN(timePickerRowPattern, "");
91 auto allChildNode = timePickerRowPattern->GetAllChildNode();
92 auto hourColumn = allChildNode["hour"].Upgrade();
93 CHECK_NULL_RETURN(hourColumn, "");
94 auto hourPickerColumnPattern = hourColumn->GetPattern<TimePickerColumnPattern>();
95 CHECK_NULL_RETURN(hourPickerColumnPattern, "");
96
97 std::string result;
98 auto hour = static_cast<int32_t>(hourPickerColumnPattern->GetCurrentIndex());
99 if (!timePickerRowPattern->GetHour24()) {
100 hour += 1;
101 }
102 std::string textHour = std::to_string(hour);
103 if (hour < DOUBLE_DIGIT) {
104 if (!Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
105 textHour = ZERO + textHour;
106 } else if (timePickerRowPattern->GetHour24()) {
107 if (timePickerRowPattern->GetPrefixHour() != ZeroPrefixType::HIDE) {
108 textHour = ZERO + textHour;
109 }
110 } else {
111 if (timePickerRowPattern->GetPrefixHour() == ZeroPrefixType::SHOW) {
112 textHour = ZERO + textHour;
113 }
114 }
115 }
116 result += textHour;
117
118 GetMinuteText(timeNode, result);
119 GetSecondText(timeNode, result);
120 if (!timePickerRowPattern->GetHour24()) {
121 auto amPmColumn = allChildNode["amPm"].Upgrade();
122 CHECK_NULL_RETURN(amPmColumn, "");
123 auto amPmPickerColumnPattern = amPmColumn->GetPattern<TimePickerColumnPattern>();
124 CHECK_NULL_RETURN(amPmPickerColumnPattern, "");
125 auto optionIndex = amPmPickerColumnPattern->GetCurrentIndex();
126 if (optionIndex >= 0) {
127 result = timePickerRowPattern->GetOptionValue(amPmColumn, optionIndex).append(" ") + result;
128 }
129 }
130 return result;
131 }
132
GetMinuteText(const RefPtr<FrameNode> & frameNode,std::string & result) const133 void DatePickerAccessibilityProperty::GetMinuteText(const RefPtr<FrameNode>& frameNode, std::string& result) const
134 {
135 CHECK_NULL_VOID(frameNode);
136 auto timePickerRowPattern = frameNode->GetPattern<NG::TimePickerRowPattern>();
137 CHECK_NULL_VOID(timePickerRowPattern);
138 auto allChildNode = timePickerRowPattern->GetAllChildNode();
139 auto minuteColumn = allChildNode["minute"].Upgrade();
140 CHECK_NULL_VOID(minuteColumn);
141
142 auto minutePickerColumnPattern = minuteColumn->GetPattern<TimePickerColumnPattern>();
143 CHECK_NULL_VOID(minutePickerColumnPattern);
144 int minute = static_cast<int>(minutePickerColumnPattern->GetCurrentIndex());
145 std::string textMinute = std::to_string(minute);
146 if (minute < DOUBLE_DIGIT) {
147 if (timePickerRowPattern->GetPrefixMinute() != ZeroPrefixType::HIDE) {
148 textMinute = ZERO + textMinute;
149 }
150 }
151 result += COLON + textMinute;
152 }
153
GetSecondText(const RefPtr<FrameNode> & frameNode,std::string & result) const154 void DatePickerAccessibilityProperty::GetSecondText(const RefPtr<FrameNode>& frameNode, std::string& result) const
155 {
156 CHECK_NULL_VOID(frameNode);
157 auto timePickerRowPattern = frameNode->GetPattern<NG::TimePickerRowPattern>();
158 CHECK_NULL_VOID(timePickerRowPattern);
159 if (timePickerRowPattern->GetHasSecond()) {
160 auto allChildNode = timePickerRowPattern->GetAllChildNode();
161 auto secondColumn = allChildNode["second"].Upgrade();
162 CHECK_NULL_VOID(secondColumn);
163 auto secondPickerColumnPattern = secondColumn->GetPattern<TimePickerColumnPattern>();
164 CHECK_NULL_VOID(secondPickerColumnPattern);
165 int second = static_cast<int>(secondPickerColumnPattern->GetCurrentIndex());
166 std::string textSecond = std::to_string(second);
167 if (second < DOUBLE_DIGIT) {
168 if (timePickerRowPattern->GetPrefixSecond() != ZeroPrefixType::HIDE) {
169 textSecond = ZERO + textSecond;
170 }
171 }
172 result += COLON + textSecond;
173 }
174 }
175 } // namespace OHOS::Ace::NG
176