• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef CJ_ACCESSIBILITY_UTILS_H
16 #define CJ_ACCESSIBILITY_UTILS_H
17 
18 #include <cstdint>
19 #include "cj_accessibility_ffi.h"
20 #include "hilog_wrapper.h"
21 #include "accessibility_def.h"
22 #include "accessibility_event_info.h"
23 #include "accessibility_ability_info.h"
24 #include "native/ffi_remote_data.h"
25 
26 namespace OHOS {
27 namespace Accessibility {
28 namespace Utils {
ReverseMap(const std::unordered_map<K,V> & map)29 template <typename K, typename V> std::unordered_map<V, K> ReverseMap(const std::unordered_map<K, V> &map)
30 {
31     std::unordered_map<V, K> rMap;
32     for (auto pair : map) {
33         rMap[pair.second] = pair.first;
34     }
35     return rMap;
36 }
37 
38 template <typename T>
GetValueFromStr(const std::unordered_map<std::string,T> & rMap,const std::string & key,RetError & errCode,const T & defaultVal)39 T GetValueFromStr(const std::unordered_map<std::string, T> &rMap, const std::string &key, RetError &errCode,
40     const T &defaultVal)
41 {
42     auto it = rMap.find(key);
43     if (it != rMap.end()) {
44         return it->second;
45     } else {
46         HILOG_DEBUG("GetValueFromStr failed(%{public}s).", it->first.c_str());
47         return defaultVal;
48     }
49 }
50 
51 template <typename T>
52 std::string GetStrFromVal(const std::unordered_map<T, std::string> &map, const T &key, RetError &errCode,
53     const std::string &defaultVal = "")
54 {
55     auto it = map.find(key);
56     if (it != map.end()) {
57         return it->second;
58     } else {
59         return defaultVal;
60     }
61 }
62 
63 static std::unordered_map<Capability, std::string> capabilityMap = { { Capability::CAPABILITY_RETRIEVE, "retrieve" },
64     { Capability::CAPABILITY_TOUCH_GUIDE, "touchGuide" },
65     { Capability::CAPABILITY_KEY_EVENT_OBSERVER, "keyEventObserver" },
66     { Capability::CAPABILITY_ZOOM, "zoom" },
67     { Capability::CAPABILITY_GESTURE, "gesture" } };
68 static auto rCapabilityMap = ReverseMap(capabilityMap);
69 
70 static std::unordered_map<AccessibilityAbilityTypes, std::string> abilityTypeMap = {
71     { AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN, "spoken" },
72     { AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC, "haptic" },
73     { AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE, "audible" },
74     { AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL, "visual" },
75     { AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC, "generic" },
76     { AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_ALL, "all" }
77 };
78 static auto rAbilityTypeMap = ReverseMap(abilityTypeMap);
79 
80 static std::unordered_map<EventType, std::string> eventTypeMap = { { EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT,
81     "accessibilityFocus" },
82     { EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT, "accessibilityFocusClear" },
83     { EventType::TYPE_VIEW_CLICKED_EVENT, "click" },
84     { EventType::TYPE_VIEW_LONG_CLICKED_EVENT, "longClick" },
85     { EventType::TYPE_VIEW_SELECTED_EVENT, "select" },
86     { EventType::TYPE_VIEW_HOVER_ENTER_EVENT, "hoverEnter" },
87     { EventType::TYPE_VIEW_HOVER_EXIT_EVENT, "hoverExit" },
88     { EventType::TYPE_VIEW_FOCUSED_EVENT, "focus" },
89     { EventType::TYPE_VIEW_TEXT_UPDATE_EVENT, "textUpdate" },
90     { EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT, "textSelectionUpdate" },
91     { EventType::TYPE_VIEW_SCROLLED_EVENT, "scroll" },
92     { EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY, "requestFocusForAccessibility" },
93     { EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY, "announceForAccessibility" },
94     { EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY_NOT_INTERRUPT, "requestFocusForAccessibilityNotInterrupt" },
95     { EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY_NOT_INTERRUPT, "announceForAccessibilityNotInterrupt" },
96     { EventType::TYPE_VIEW_SCROLLING_EVENT, "scrolling" } };
97 static auto rEventTypeMap = ReverseMap(eventTypeMap);
98 
99 static std::unordered_map<ActionType, std::string> actionMap = {
100     { ActionType::ACCESSIBILITY_ACTION_CLICK, "click" },
101     { ActionType::ACCESSIBILITY_ACTION_LONG_CLICK, "longClick" },
102     { ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD, "scrollForward" },
103     { ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD, "scrollBackward" },
104     { ActionType::ACCESSIBILITY_ACTION_FOCUS, "focus" },
105     { ActionType::ACCESSIBILITY_ACTION_CLEAR_FOCUS, "clearFocus" },
106     { ActionType::ACCESSIBILITY_ACTION_CLEAR_SELECTION, "clearSelection" },
107     { ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS, "accessibilityFocus" },
108     { ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS, "clearAccessibilityFocus" },
109     { ActionType::ACCESSIBILITY_ACTION_CUT, "cut" },
110     { ActionType::ACCESSIBILITY_ACTION_COPY, "copy" },
111     { ActionType::ACCESSIBILITY_ACTION_PASTE, "paste" },
112     { ActionType::ACCESSIBILITY_ACTION_SELECT, "select" },
113     { ActionType::ACCESSIBILITY_ACTION_SET_TEXT, "setText" },
114     { ActionType::ACCESSIBILITY_ACTION_DELETED, "delete" },
115     { ActionType::ACCESSIBILITY_ACTION_SET_SELECTION, "setSelection" },
116     { ActionType::ACCESSIBILITY_ACTION_COMMON, "common" },
117     { ActionType::ACCESSIBILITY_ACTION_HOME, "home" },
118     { ActionType::ACCESSIBILITY_ACTION_BACK, "back" },
119     { ActionType::ACCESSIBILITY_ACTION_RECENTTASK, "recentTask" },
120     { ActionType::ACCESSIBILITY_ACTION_NOTIFICATIONCENTER, "notificationCenter" },
121     { ActionType::ACCESSIBILITY_ACTION_CONTROLCENTER, "controlCenter" },
122     { ActionType::ACCESSIBILITY_ACTION_SET_CURSOR_POSITION, "setCursorPosition" },
123 };
124 static auto rActionMap = ReverseMap(actionMap);
125 
126 static std::unordered_map<WindowUpdateType, std::string> windowUpdateTypeMap = {
127     { WindowUpdateType::WINDOW_UPDATE_FOCUSED, "focus" },
128     { WindowUpdateType::WINDOW_UPDATE_ACTIVE, "active" },
129     { WindowUpdateType::WINDOW_UPDATE_ADDED, "add" },
130     { WindowUpdateType::WINDOW_UPDATE_REMOVED, "remove" },
131     { WindowUpdateType::WINDOW_UPDATE_BOUNDS, "bounds" }
132 };
133 static auto rWindowUpdateTypeMap = ReverseMap(windowUpdateTypeMap);
134 
135 static std::unordered_map<TextMoveUnit, std::string> textMoveUnitMap = { { TextMoveUnit::STEP_CHARACTER, "char" },
136     { TextMoveUnit::STEP_WORD, "word" },
137     { TextMoveUnit::STEP_LINE, "line" },
138     { TextMoveUnit::STEP_PAGE, "page" },
139     { TextMoveUnit::STEP_PARAGRAPH, "paragraph" } };
140 static auto rTextMoveUnitMap = ReverseMap(textMoveUnitMap);
141 
142 // base
143 char *MallocCString(const std::string &origin, RetError &errCode);
144 CArrString VectorToCArrString(std::vector<std::string> &vec, RetError &errCode);
145 std::vector<std::string> CArrStringToVector(CArrString cArrStr);
146 
147 // enum to string
148 CArrString GetAbilityTypesStr(uint32_t abilityTypes, RetError &errCode);
149 CArrString GetCapabilityStr(uint32_t capabilities, RetError &errCode);
150 CArrString GetEventTypeStr(uint32_t eventType, RetError &errCode);
151 
152 bool CheckAbilityType(const std::string &abilityType);
153 bool CheckStateType(const std::string &stateType);
154 
155 void FreecAbility(CAccessibilityAbilityInfo *cAbility);
156 // func
157 CAccessibilityAbilityInfo ConvertAccAbilityInfo2C(AccessibilityAbilityInfo &abilityInfo, RetError &errCode);
158 CArrAccessibilityAbilityInfo ConvertArrAccAbilityInfo2CArr(std::vector<AccessibilityAbilityInfo> &abilityList,
159     RetError &errCode);
160 CEventInfo ConvertEventInfo2C(const AccessibilityEventInfo &eventInfo, RetError &errCode);
161 AccessibilityEventInfo ConvertEventInfo(const CEventInfo &cEventInfo, RetError &errCode);
162 } // namespace Utils
163 } // namespace Accessibility
164 } // namespace OHOS
165 #endif // CJ_ACCESSIBILITY_UTILS_H