1 /*
2 * Copyright (C) 2022 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 "json_utils.h"
17 #include <fstream>
18 #include <sstream>
19 #include "hilog_wrapper.h"
20 #include "accessibility_event_info.h"
21 #include "accessibility_ability_info.h"
22
23 using namespace std;
24
25 namespace OHOS {
26 namespace Accessibility {
27 const std::string AccessibleAbility_JSON_FILE_PATH = "/system/app/dummy_accessibility_ability_config.json";
28 const std::string AccessibleAbility_JSON_KEY_ACCESSIBILITY_EVENT_TYPES = "accessibilityEventTypes";
29 const std::string AccessibleAbility_JSON_KEY_TARGET_BUNDLE_NAMES = "targetBundleNames";
30 const std::string AccessibleAbility_JSON_KEY_ACCESSIBILITY_ABILITY_TYPES = "accessibilityAbilityTypes";
31 const std::string AccessibleAbility_JSON_KEY_NOTIFICATION_TIMEOUT = "notificationTimeout";
32 const std::string AccessibleAbility_JSON_KEY_UI_NONINTERACTIVE_TIMEOUT = "uiNoninteractiveTimeout";
33 const std::string AccessibleAbility_JSON_KEY_UI_INTERACTIVE_TIMEOUT = "uiInteractiveTimeout";
34 const std::string AccessibleAbility_JSON_KEY_ACCESSIBILITY_CAPABILITIES = "accessibilityCapabilities";
35 const std::string AccessibleAbility_JSON_KEY_DESCRIPTION = "description";
36 const std::string AccessibleAbility_JSON_KEY_SETTINGS_ABILITY = "settingsAbility";
37 const std::string AccessibleAbility_JSON_KEY_ACCESSIBILITY_CAPABILITIES_RATIONALE =
38 "accessibilityCapabilityRationale";
39 const std::string AccessibleAbility_Config_JSON_FILE_PATH = "/system/app/accessibility_config.json";
40
41 // The json value of event types
42 const std::string EVENT_TYPES_JSON_VALUE_CLICK = "click";
43 const std::string EVENT_TYPES_JSON_VALUE_LONG_CLICK = "longClick";
44 const std::string EVENT_TYPES_JSON_VALUE_SELECT = "select";
45 const std::string EVENT_TYPES_JSON_VALUE_FOCUS = "focus";
46 const std::string EVENT_TYPES_JSON_VALUE_TEXT_UPDATE = "textUpdate";
47 const std::string EVENT_TYPES_JSON_VALUE_PAGE_STATE_UPDATE = "pageStateUpdate";
48 const std::string EVENT_TYPES_JSON_VALUE_NOTIFICATION_UPDATE = "notificationUpdate";
49 const std::string EVENT_TYPES_JSON_VALUE_HOVER_ENTER = "hoverEnter";
50 const std::string EVENT_TYPES_JSON_VALUE_HOVER_EXIT = "hoverExit";
51 const std::string EVENT_TYPES_JSON_VALUE_TOUCH_GUIDE_BEGIN = "touchGuideBegin";
52 const std::string EVENT_TYPES_JSON_VALUE_TOUCH_GUIDE_END = "touchGuideEnd";
53 const std::string EVENT_TYPES_JSON_VALUE_PAGE_CONTENT_UPDATE = "pageContentUpdate";
54 const std::string EVENT_TYPES_JSON_VALUE_SCROLL = "scroll";
55 const std::string EVENT_TYPES_JSON_VALUE_TEXT_SELECTION_UPDATE = "textSelectionUpdate";
56 const std::string EVENT_TYPES_JSON_VALUE_PUBLIC_NOTICE = "publicNotice";
57 const std::string EVENT_TYPES_JSON_VALUE_ACCESSIBILITY_FOCUS = "accessibilityFocus";
58 const std::string EVENT_TYPES_JSON_VALUE_ACCESSIBILITY_FOCUS_CLEAR = "accessibilityFocusClear";
59 const std::string EVENT_TYPES_JSON_VALUE_TEXT_MOVE_UNIT = "textMoveUnit";
60 const std::string EVENT_TYPES_JSON_VALUE_TOUCH_GUIDE_GESTURE_BEGIN = "touchGuideGestureBegin";
61 const std::string EVENT_TYPES_JSON_VALUE_TOUCH_GUIDE_GESTURE_END = "touchGuideGestureEnd";
62 const std::string EVENT_TYPES_JSON_VALUE_TOUCH_BEGIN = "touchBegin";
63 const std::string EVENT_TYPES_JSON_VALUE_TOUCH_END = "touchEnd";
64 const std::string EVENT_TYPES_JSON_VALUE_WINDOW_UPDATE = "windowUpdate";
65 const std::string EVENT_TYPES_JSON_VALUE_INTERRUPT = "interrupt";
66 const std::string EVENT_TYPES_JSON_VALUE_GESTURE_EVENT = "gesture";
67 const std::string EVENT_TYPES_JSON_VALUE_ALL = "all";
68
69 // The json value of accessibilityAbility type
70 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_SPOKEN = "spoken";
71 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_HAPIC = "haptic";
72 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_AUDIBLE = "audible";
73 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_VISUAL = "visual";
74 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_GENERIC = "generic";
75 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_ALL = "all";
76
77 // The json value of capabilities
78 const std::string CAPABILITIES_JSON_VALUE_RETRIEVE = "retrieve";
79 const std::string CAPABILITIES_JSON_VALUE_TOUCH_GUIDE = "touchGuide";
80 const std::string CAPABILITIES_JSON_VALUE_KEY_EVENT_OBSERVER = "keyEventObserver";
81 const std::string CAPABILITIES_JSON_VALUE_ZOOM = "zoom";
82 const std::string CAPABILITIES_JSON_VALUE_GESTURE = "gesture";
83
GetJsonObjFromJson(nlohmann::json & jsonObj,const std::string & jsonPath)84 bool JsonUtils::GetJsonObjFromJson(nlohmann::json &jsonObj, const std::string &jsonPath)
85 {
86 HILOG_DEBUG("start.");
87
88 char realPath[PATH_MAX + 1] = { 0 };
89 if (jsonPath.length() > PATH_MAX) {
90 HILOG_ERROR("jsonPath is too long");
91 return false;
92 }
93 if (!realpath(jsonPath.c_str(), realPath)) {
94 HILOG_ERROR("Fail to get realpath of %{public}s", jsonPath.c_str());
95 return false;
96 }
97
98 std::ifstream jsonFileStream;
99 jsonFileStream.open(realPath, std::ios::in);
100 if (!jsonFileStream.is_open()) {
101 HILOG_ERROR("Open json file failed.");
102 return false;
103 }
104
105 std::ostringstream buf;
106 char ch;
107 while (buf && jsonFileStream.get(ch)) {
108 buf.put(ch);
109 }
110 jsonFileStream.close();
111
112 jsonObj = nlohmann::json::parse(buf.str(), nullptr, false);
113 if (!jsonObj.is_structured()) {
114 HILOG_ERROR("Parse json file into jsonObj failed.");
115 return false;
116 }
117 return true;
118 }
119
GetStringFromJson(const nlohmann::json & json,const std::string & key,std::string & value)120 bool JsonUtils::GetStringFromJson(const nlohmann::json &json, const std::string &key, std::string &value)
121 {
122 HILOG_DEBUG("start.");
123 if (!json.is_object()) {
124 HILOG_ERROR("json is not object.");
125 return false;
126 }
127 if (json.find(key) != json.end() && json.at(key).is_string()) {
128 HILOG_DEBUG("Find key[%{public}s] successful.", key.c_str());
129 value = json.at(key).get<std::string>();
130 }
131 return true;
132 }
133
GetIntFromJson(const nlohmann::json & json,const std::string & key,int & value)134 bool JsonUtils::GetIntFromJson(const nlohmann::json &json, const std::string &key, int &value)
135 {
136 HILOG_DEBUG("start.");
137 if (!json.is_object()) {
138 HILOG_ERROR("json is not object.");
139 return false;
140 }
141 if (json.find(key) != json.end() && json.at(key).is_number()) {
142 HILOG_DEBUG("Find key[%{public}s] successful.", key.c_str());
143 value = json.at(key).get<int>();
144 }
145 return true;
146 }
147
GetStringVecFromJson(const nlohmann::json & json,const std::string & key,std::vector<std::string> & value)148 bool JsonUtils::GetStringVecFromJson(const nlohmann::json &json, const std::string &key,
149 std::vector<std::string> &value)
150 {
151 HILOG_DEBUG("start.");
152 if (!json.is_object()) {
153 HILOG_ERROR("json is not object.");
154 return false;
155 }
156 if (json.find(key) != json.end() && json.at(key).is_array()) {
157 HILOG_DEBUG("Find key[%{public}s] successful.", key.c_str());
158 value = json.at(key).get<std::vector<std::string>>();
159 }
160 return true;
161 }
162
ParseObjVecFromJson(const nlohmann::json & json,const std::string & key,std::vector<nlohmann::json> & value)163 bool JsonUtils::ParseObjVecFromJson(const nlohmann::json &json, const std::string &key,
164 std::vector<nlohmann::json> &value)
165 {
166 HILOG_DEBUG("start.");
167 if (!json.is_object()) {
168 HILOG_ERROR("json is not object.");
169 return false;
170 }
171 if (json.find(key) != json.end() && json.at(key).is_array()) {
172 HILOG_DEBUG("Find key[%{public}s] successful.", key.c_str());
173 value = json.at(key).get<std::vector<nlohmann::json>>();
174 }
175 return true;
176 }
177
ParseEventTypesFromVec(const vector<string> & events,uint32_t & eventTypes)178 void PraseVecUtils::ParseEventTypesFromVec(const vector<string>& events, uint32_t& eventTypes)
179 {
180 HILOG_DEBUG("start.");
181 eventTypes = EventType::TYPE_VIEW_INVALID;
182 static std::map<std::string, uint32_t> eventTypesTable = {
183 {EVENT_TYPES_JSON_VALUE_CLICK, EventType::TYPE_VIEW_CLICKED_EVENT},
184 {EVENT_TYPES_JSON_VALUE_LONG_CLICK, EventType::TYPE_VIEW_LONG_CLICKED_EVENT},
185 {EVENT_TYPES_JSON_VALUE_SELECT, EventType::TYPE_VIEW_SELECTED_EVENT},
186 {EVENT_TYPES_JSON_VALUE_FOCUS, EventType::TYPE_VIEW_FOCUSED_EVENT},
187 {EVENT_TYPES_JSON_VALUE_TEXT_UPDATE, EventType::TYPE_VIEW_TEXT_UPDATE_EVENT},
188 {EVENT_TYPES_JSON_VALUE_PAGE_STATE_UPDATE, EventType::TYPE_PAGE_STATE_UPDATE},
189 {EVENT_TYPES_JSON_VALUE_NOTIFICATION_UPDATE, EventType::TYPE_NOTIFICATION_UPDATE_EVENT},
190 {EVENT_TYPES_JSON_VALUE_HOVER_ENTER, EventType::TYPE_VIEW_HOVER_ENTER_EVENT},
191 {EVENT_TYPES_JSON_VALUE_HOVER_EXIT, EventType::TYPE_VIEW_HOVER_EXIT_EVENT},
192 {EVENT_TYPES_JSON_VALUE_TOUCH_GUIDE_BEGIN, EventType::TYPE_TOUCH_GUIDE_GESTURE_BEGIN},
193 {EVENT_TYPES_JSON_VALUE_TOUCH_GUIDE_END, EventType::TYPE_TOUCH_GUIDE_GESTURE_END},
194 {EVENT_TYPES_JSON_VALUE_PAGE_CONTENT_UPDATE, EventType::TYPE_PAGE_CONTENT_UPDATE},
195 {EVENT_TYPES_JSON_VALUE_SCROLL, EventType::TYPE_VIEW_SCROLLED_EVENT},
196 {EVENT_TYPES_JSON_VALUE_TEXT_SELECTION_UPDATE, EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT},
197 {EVENT_TYPES_JSON_VALUE_PUBLIC_NOTICE, EventType::TYPE_PUBLIC_NOTICE_EVENT},
198 {EVENT_TYPES_JSON_VALUE_ACCESSIBILITY_FOCUS, EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT},
199 {EVENT_TYPES_JSON_VALUE_ACCESSIBILITY_FOCUS_CLEAR, EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT},
200 {EVENT_TYPES_JSON_VALUE_TEXT_MOVE_UNIT, EventType::TYPE_VIEW_TEXT_MOVE_UNIT_EVENT},
201 {EVENT_TYPES_JSON_VALUE_TOUCH_GUIDE_GESTURE_BEGIN, EventType::TYPE_TOUCH_GUIDE_BEGIN},
202 {EVENT_TYPES_JSON_VALUE_TOUCH_GUIDE_GESTURE_END, EventType::TYPE_TOUCH_GUIDE_END},
203 {EVENT_TYPES_JSON_VALUE_TOUCH_BEGIN, EventType::TYPE_TOUCH_BEGIN},
204 {EVENT_TYPES_JSON_VALUE_TOUCH_END, EventType::TYPE_TOUCH_END},
205 {EVENT_TYPES_JSON_VALUE_WINDOW_UPDATE, EventType::TYPE_WINDOW_UPDATE},
206 {EVENT_TYPES_JSON_VALUE_INTERRUPT, EventType::TYPE_INTERRUPT_EVENT},
207 {EVENT_TYPES_JSON_VALUE_GESTURE_EVENT, EventType::TYPE_GESTURE_EVENT},
208 {EVENT_TYPES_JSON_VALUE_ALL, EventType::TYPES_ALL_MASK}
209 };
210
211 for (auto& str : events) {
212 std::map<std::string, uint32_t>::iterator itr = eventTypesTable.find(str);
213 if (itr != eventTypesTable.end()) {
214 eventTypes |= itr->second;
215 }
216 }
217 }
218
ParseAbilityTypesFromVec(const vector<string> & abilities,uint32_t & abilityTypes)219 void PraseVecUtils::ParseAbilityTypesFromVec(const vector<string>& abilities, uint32_t& abilityTypes)
220 {
221 HILOG_DEBUG("start.");
222 abilityTypes = 0;
223
224 for (auto it = abilities.begin(); it != abilities.end(); it++) {
225 if (*it == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_SPOKEN) {
226 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN;
227 }
228
229 if (*it == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_HAPIC) {
230 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC;
231 }
232
233 if (*it == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_AUDIBLE) {
234 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE;
235 }
236
237 if (*it == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_VISUAL) {
238 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL;
239 }
240
241 if (*it == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_GENERIC) {
242 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC;
243 }
244
245 if (*it == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_ALL) {
246 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_ALL;
247 }
248 }
249 }
250
ParseCapabilitiesFromVec(const vector<string> & capabilities,uint32_t & capabilitiesValue)251 void PraseVecUtils::ParseCapabilitiesFromVec(const vector<string>& capabilities, uint32_t& capabilitiesValue)
252 {
253 HILOG_DEBUG("start.");
254 capabilitiesValue = 0;
255
256 for (auto it = capabilities.begin(); it != capabilities.end(); it++) {
257 if (*it == CAPABILITIES_JSON_VALUE_RETRIEVE) {
258 capabilitiesValue |= Capability::CAPABILITY_RETRIEVE;
259 }
260
261 if (*it == CAPABILITIES_JSON_VALUE_TOUCH_GUIDE) {
262 capabilitiesValue |= Capability::CAPABILITY_TOUCH_GUIDE;
263 }
264
265 if (*it == CAPABILITIES_JSON_VALUE_KEY_EVENT_OBSERVER) {
266 capabilitiesValue |= Capability::CAPABILITY_KEY_EVENT_OBSERVER;
267 }
268
269 if (*it == CAPABILITIES_JSON_VALUE_ZOOM) {
270 capabilitiesValue |= Capability::CAPABILITY_ZOOM;
271 }
272
273 if (*it == CAPABILITIES_JSON_VALUE_GESTURE) {
274 capabilitiesValue |= Capability::CAPABILITY_GESTURE;
275 }
276 }
277 }
278 } // namespace Accessibility
279 } // namespace OHOS