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/time_picker/timepicker_row_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/time_picker/timepicker_row_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 AM = "上午";
27 const std::string PM = "下午";
28 const std::string ZERO = "0";
29 } // namespace
30 
GetText() const31 std::string TimePickerRowAccessibilityProperty::GetText() const
32 {
33     auto frameNode = host_.Upgrade();
34     CHECK_NULL_RETURN(frameNode, "");
35     auto timePickerRowPattern = frameNode->GetPattern<NG::TimePickerRowPattern>();
36     CHECK_NULL_RETURN(timePickerRowPattern, "");
37     auto allChildNode = timePickerRowPattern->GetAllChildNode();
38     auto hourColumn = allChildNode["hour"].Upgrade();
39     CHECK_NULL_RETURN(hourColumn, "");
40     auto hourPickerColumnPattern = hourColumn->GetPattern<TimePickerColumnPattern>();
41     CHECK_NULL_RETURN(hourPickerColumnPattern, "");
42 
43     std::string result;
44     auto hour = static_cast<int32_t>(hourPickerColumnPattern->GetCurrentIndex()); // + 1;
45     if (!timePickerRowPattern->GetHour24()) {
46         hour += 1;
47     }
48     std::string textHour = std::to_string(hour);
49     if (hour < DOUBLE_DIGIT) {
50         if (!Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
51             textHour = ZERO + textHour;
52         } else if (timePickerRowPattern->GetHour24()) {
53             if (timePickerRowPattern->GetPrefixHour() != ZeroPrefixType::HIDE) {
54                 textHour = ZERO + textHour;
55             }
56         } else {
57             if (timePickerRowPattern->GetPrefixHour() == ZeroPrefixType::SHOW) {
58                 textHour = ZERO + textHour;
59             }
60         }
61     }
62     result += textHour;
63 
64     GetMinuteText(result);
65     GetSecondText(result);
66     if (!timePickerRowPattern->GetHour24()) {
67         auto amPmColumn = allChildNode["amPm"].Upgrade();
68         CHECK_NULL_RETURN(amPmColumn, "");
69         auto amPmPickerColumnPattern = amPmColumn->GetPattern<TimePickerColumnPattern>();
70         if (amPmPickerColumnPattern->GetCurrentIndex() == 0) {
71             result = AM + result;
72         } else {
73             result = PM + result;
74         }
75     }
76     return result;
77 }
78 
GetMinuteText(std::string & result) const79 void TimePickerRowAccessibilityProperty::GetMinuteText(std::string& result) const
80 {
81     auto frameNode = host_.Upgrade();
82     CHECK_NULL_VOID(frameNode);
83     auto timePickerRowPattern = frameNode->GetPattern<NG::TimePickerRowPattern>();
84     CHECK_NULL_VOID(timePickerRowPattern);
85     auto allChildNode = timePickerRowPattern->GetAllChildNode();
86     auto minuteColumn = allChildNode["minute"].Upgrade();
87     CHECK_NULL_VOID(minuteColumn);
88 
89     auto minutePickerColumnPattern = minuteColumn->GetPattern<TimePickerColumnPattern>();
90     CHECK_NULL_VOID(minutePickerColumnPattern);
91     int minute = static_cast<int>(minutePickerColumnPattern->GetCurrentIndex());
92     std::string textMinute = std::to_string(minute);
93     if (minute < DOUBLE_DIGIT) {
94         if (timePickerRowPattern->GetPrefixMinute() != ZeroPrefixType::HIDE) {
95             textMinute = ZERO + textMinute;
96         }
97     }
98     result += COLON + textMinute;
99 }
100 
GetSecondText(std::string & result) const101 void TimePickerRowAccessibilityProperty::GetSecondText(std::string& result) const
102 {
103     auto frameNode = host_.Upgrade();
104     CHECK_NULL_VOID(frameNode);
105     auto timePickerRowPattern = frameNode->GetPattern<NG::TimePickerRowPattern>();
106     CHECK_NULL_VOID(timePickerRowPattern);
107     if (timePickerRowPattern->GetHasSecond()) {
108         auto allChildNode = timePickerRowPattern->GetAllChildNode();
109         auto secondColumn = allChildNode["second"].Upgrade();
110         CHECK_NULL_VOID(secondColumn);
111         auto secondPickerColumnPattern = secondColumn->GetPattern<TimePickerColumnPattern>();
112         CHECK_NULL_VOID(secondPickerColumnPattern);
113         int second = static_cast<int>(secondPickerColumnPattern->GetCurrentIndex());
114         std::string textSecond = std::to_string(second);
115         if (second < DOUBLE_DIGIT) {
116             if (timePickerRowPattern->GetPrefixSecond() != ZeroPrefixType::HIDE) {
117                 textSecond = ZERO + textSecond;
118             }
119         }
120         result += COLON + textSecond;
121     }
122 }
123 } // namespace OHOS::Ace::NG
124