• 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 static auto rEventTypeMap = ReverseMap(eventTypeMap);
95 
96 static std::unordered_map<ActionType, std::string> actionMap = {
97     { ActionType::ACCESSIBILITY_ACTION_CLICK, "click" },
98     { ActionType::ACCESSIBILITY_ACTION_LONG_CLICK, "longClick" },
99     { ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD, "scrollForward" },
100     { ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD, "scrollBackward" },
101     { ActionType::ACCESSIBILITY_ACTION_FOCUS, "focus" },
102     { ActionType::ACCESSIBILITY_ACTION_CLEAR_FOCUS, "clearFocus" },
103     { ActionType::ACCESSIBILITY_ACTION_CLEAR_SELECTION, "clearSelection" },
104     { ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS, "accessibilityFocus" },
105     { ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS, "clearAccessibilityFocus" },
106     { ActionType::ACCESSIBILITY_ACTION_CUT, "cut" },
107     { ActionType::ACCESSIBILITY_ACTION_COPY, "copy" },
108     { ActionType::ACCESSIBILITY_ACTION_PASTE, "paste" },
109     { ActionType::ACCESSIBILITY_ACTION_SELECT, "select" },
110     { ActionType::ACCESSIBILITY_ACTION_SET_TEXT, "setText" },
111     { ActionType::ACCESSIBILITY_ACTION_DELETED, "delete" },
112     { ActionType::ACCESSIBILITY_ACTION_SET_SELECTION, "setSelection" },
113     { ActionType::ACCESSIBILITY_ACTION_COMMON, "common" },
114     { ActionType::ACCESSIBILITY_ACTION_HOME, "home" },
115     { ActionType::ACCESSIBILITY_ACTION_BACK, "back" },
116     { ActionType::ACCESSIBILITY_ACTION_RECENTTASK, "recentTask" },
117     { ActionType::ACCESSIBILITY_ACTION_NOTIFICATIONCENTER, "notificationCenter" },
118     { ActionType::ACCESSIBILITY_ACTION_CONTROLCENTER, "controlCenter" },
119     { ActionType::ACCESSIBILITY_ACTION_SET_CURSOR_POSITION, "setCursorPosition" },
120 };
121 static auto rActionMap = ReverseMap(actionMap);
122 
123 static std::unordered_map<WindowUpdateType, std::string> windowUpdateTypeMap = {
124     { WindowUpdateType::WINDOW_UPDATE_FOCUSED, "focus" },
125     { WindowUpdateType::WINDOW_UPDATE_ACTIVE, "active" },
126     { WindowUpdateType::WINDOW_UPDATE_ADDED, "add" },
127     { WindowUpdateType::WINDOW_UPDATE_REMOVED, "remove" },
128     { WindowUpdateType::WINDOW_UPDATE_BOUNDS, "bounds" }
129 };
130 static auto rWindowUpdateTypeMap = ReverseMap(windowUpdateTypeMap);
131 
132 static std::unordered_map<TextMoveUnit, std::string> textMoveUnitMap = { { TextMoveUnit::STEP_CHARACTER, "char" },
133     { TextMoveUnit::STEP_WORD, "word" },
134     { TextMoveUnit::STEP_LINE, "line" },
135     { TextMoveUnit::STEP_PAGE, "page" },
136     { TextMoveUnit::STEP_PARAGRAPH, "paragraph" } };
137 static auto rTextMoveUnitMap = ReverseMap(textMoveUnitMap);
138 
139 // base
140 char *MallocCString(const std::string &origin, RetError &errCode);
141 CArrString VectorToCArrString(std::vector<std::string> &vec, RetError &errCode);
142 std::vector<std::string> CArrStringToVector(CArrString cArrStr);
143 
144 // enum to string
145 CArrString GetAbilityTypesStr(uint32_t abilityTypes, RetError &errCode);
146 CArrString GetCapabilityStr(uint32_t capabilities, RetError &errCode);
147 CArrString GetEventTypeStr(uint32_t eventType, RetError &errCode);
148 
149 bool CheckAbilityType(const std::string &abilityType);
150 bool CheckStateType(const std::string &stateType);
151 
152 // func
153 CAccessibilityAbilityInfo ConvertAccAbilityInfo2C(AccessibilityAbilityInfo &abilityInfo, RetError &errCode);
154 CArrAccessibilityAbilityInfo ConvertArrAccAbilityInfo2CArr(std::vector<AccessibilityAbilityInfo> &abilityList,
155     RetError &errCode);
156 CEventInfo ConvertEventInfo2C(const AccessibilityEventInfo &eventInfo, RetError &errCode);
157 AccessibilityEventInfo ConvertEventInfo(const CEventInfo &cEventInfo, RetError &errCode);
158 } // namespace Utils
159 } // namespace Accessibility
160 } // namespace OHOS
161 #endif // CJ_ACCESSIBILITY_UTILS_H