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 "accessibility_dumper.h"
17 
18 #include <cinttypes>
19 #include <csignal>
20 #include <iomanip>
21 #include <map>
22 #include <sstream>
23 
24 #include "accessibility_account_data.h"
25 #include "accessibility_window_manager.h"
26 #include "accessible_ability_manager_service.h"
27 #include "hilog_wrapper.h"
28 #include "string_ex.h"
29 #include "unique_fd.h"
30 
31 namespace OHOS {
32 namespace Accessibility {
33 namespace {
34     const std::string ARG_DUMP_HELP = "-h";
35     const std::string ARG_DUMP_USER = "-u";
36     const std::string ARG_DUMP_CLIENT = "-c";
37     const std::string ARG_DUMP_ACCESSIBILITY_WINDOW = "-w";
38 }
39 
Dump(int fd,const std::vector<std::u16string> & args) const40 int AccessibilityDumper::Dump(int fd, const std::vector<std::u16string>& args) const
41 {
42     HILOG_DEBUG("Dump begin fd: %{public}d", fd);
43     if (fd < 0) {
44         return -1;
45     }
46     (void) signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE crash
47     std::vector<std::string> params;
48     std::transform(args.begin(), args.end(), std::back_inserter(params),
49         [](const std::u16string &arg) { return Str16ToStr8(arg); });
50 
51     std::string dumpInfo;
52     if (params.empty()) {
53         ShowIllegalArgsInfo(dumpInfo);
54     } else if (params[0] == ARG_DUMP_HELP) {
55         ShowHelpInfo(dumpInfo);
56     } else {
57         DumpAccessibilityInfo(params, dumpInfo);
58     }
59     int ret = dprintf(fd, "%s\n", dumpInfo.c_str());
60     if (ret < 0) {
61         HILOG_ERROR("dprintf error");
62         return -1;
63     }
64     HILOG_INFO("Dump end");
65     return 0;
66 }
67 
68 
DumpAccessibilityWindowInfo(std::string & dumpInfo) const69 int AccessibilityDumper::DumpAccessibilityWindowInfo(std::string& dumpInfo) const
70 {
71     HILOG_INFO();
72     sptr<AccessibilityAccountData> currentAccount =
73         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
74     if (!currentAccount) {
75         HILOG_ERROR("currentAccount is null");
76         return -1;
77     }
78 
79     std::ostringstream oss;
80     size_t index = 0;
81     std::map<int32_t, sptr<AccessibilityWindowConnection>> connectedWindowList = currentAccount->GetAsacConnections();
82     oss << "connected window id: ";
83     for (const auto &iter : connectedWindowList) {
84         index++;
85         oss << iter.first;
86 
87         if (index != connectedWindowList.size()) {
88             oss << ", ";
89         }
90     }
91 
92     index = 0;
93     const std::map<int32_t, AccessibilityWindowInfo> &windows = Singleton<AccessibilityWindowManager>::
94         GetInstance().a11yWindows_;
95     oss << std::endl << "a11yWindows_ id: ";
96     for (const auto &iter : windows) {
97         index++;
98         oss << iter.first;
99 
100         if (index != connectedWindowList.size()) {
101             oss << ", ";
102         }
103     }
104 
105     oss << std::endl << "active window id: " <<
106         Singleton<AccessibilityWindowManager>::GetInstance().activeWindowId_ << std::endl;
107     oss << "accessibility focused window id: " <<
108         Singleton<AccessibilityWindowManager>::GetInstance().a11yFocusedWindowId_ << std::endl;
109 
110     dumpInfo.append(oss.str());
111     return 0;
112 }
113 
ConvertCapabilities(const uint32_t value,std::string & capabilities)114 void ConvertCapabilities(const uint32_t value, std::string &capabilities)
115 {
116     capabilities = "";
117     if (value & Capability::CAPABILITY_RETRIEVE) {
118         capabilities += "retrieve/";
119     }
120     if (value & Capability::CAPABILITY_TOUCH_GUIDE) {
121         capabilities += "touchGuide/";
122     }
123     if (value & Capability::CAPABILITY_KEY_EVENT_OBSERVER) {
124         capabilities += "keyEventObserver/";
125     }
126     if (value & Capability::CAPABILITY_ZOOM) {
127         capabilities += "zoom/";
128     }
129     if (value & Capability::CAPABILITY_GESTURE) {
130         capabilities += "gesture/";
131     }
132 }
133 
ConvertAbilityTypes(const uint32_t value,std::string & abilityTypes)134 void ConvertAbilityTypes(const uint32_t value, std::string &abilityTypes)
135 {
136     abilityTypes = "";
137     if (value & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN) {
138         abilityTypes += "spoken/";
139     }
140     if (value & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC) {
141         abilityTypes += "haptic/";
142     }
143     if (value & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE) {
144         abilityTypes += "audible/";
145     }
146     if (value & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL) {
147         abilityTypes += "visual/";
148     }
149     if (value & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC) {
150         abilityTypes += "generic/";
151     }
152 }
153 
ConvertEventTypes(const uint32_t value,std::string & eventTypes)154 void ConvertEventTypes(const uint32_t value, std::string &eventTypes)
155 {
156     eventTypes = "";
157     std::map<EventType, std::string> accessibilityEventTable = {{EventType::TYPE_VIEW_CLICKED_EVENT, "click"},
158         {EventType::TYPE_VIEW_LONG_CLICKED_EVENT, "longClick"},
159         {EventType::TYPE_VIEW_SELECTED_EVENT, "select"},
160         {EventType::TYPE_VIEW_FOCUSED_EVENT, "focus"},
161         {EventType::TYPE_VIEW_TEXT_UPDATE_EVENT, "textUpdate"},
162         {EventType::TYPE_PAGE_STATE_UPDATE, "pageStateUpdate"},
163         {EventType::TYPE_NOTIFICATION_UPDATE_EVENT, "notificationUpdate"},
164         {EventType::TYPE_VIEW_HOVER_ENTER_EVENT, "hoverEnter"},
165         {EventType::TYPE_VIEW_HOVER_EXIT_EVENT, "hoverExit"},
166         {EventType::TYPE_TOUCH_GUIDE_GESTURE_BEGIN, "touchGuideGestureBegin"},
167         {EventType::TYPE_TOUCH_GUIDE_GESTURE_END, "touchGuideGestureEnd"},
168         {EventType::TYPE_PAGE_CONTENT_UPDATE, "pageContentUpdate"},
169         {EventType::TYPE_VIEW_SCROLLED_EVENT, "scroll"},
170         {EventType::TYPE_VIEW_SCROLLED_START, "scrollStart"},
171         {EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT, "textSelectionUpdate"},
172         {EventType::TYPE_PUBLIC_NOTICE_EVENT, "publicNotice"},
173         {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT, "accessibilityFocus"},
174         {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT, "accessibilityFocusClear"},
175         {EventType::TYPE_VIEW_TEXT_MOVE_UNIT_EVENT, "textMoveUnit"},
176         {EventType::TYPE_TOUCH_GUIDE_BEGIN, "touchGuideBegin"},
177         {EventType::TYPE_TOUCH_GUIDE_END, "touchGuideEnd"},
178         {EventType::TYPE_TOUCH_BEGIN, "touchBegin"},
179         {EventType::TYPE_TOUCH_END, "touchEnd"},
180         {EventType::TYPE_WINDOW_UPDATE, "windowUpdate"},
181         {EventType::TYPE_INTERRUPT_EVENT, "interrupt"},
182         {EventType::TYPE_GESTURE_EVENT, "gesture"},
183         {EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY, "requestFocusForAccessibility"},
184         {EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY, "announceForAccessibility"},
185         {EventType::TYPE_PAGE_OPEN, "pageOpen"},
186         {EventType::TYPE_PAGE_CLOSE, "pageClose"},
187         {EventType::TYPE_ELEMENT_INFO_CHANGE, "elementInfoChange"}};
188 
189     for (auto itr = accessibilityEventTable.begin(); itr != accessibilityEventTable.end(); ++itr) {
190         if (value & itr->first) {
191             eventTypes += itr->second;
192             eventTypes += "/";
193         }
194     }
195 }
196 
DumpAccessibilityClientInfo(std::string & dumpInfo) const197 int AccessibilityDumper::DumpAccessibilityClientInfo(std::string& dumpInfo) const
198 {
199     HILOG_INFO();
200     sptr<AccessibilityAccountData> currentAccount =
201         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
202     if (!currentAccount) {
203         HILOG_ERROR("currentAccount is null");
204         return -1;
205     }
206 
207     auto connectedAbilities = currentAccount->GetConnectedA11yAbilities();
208     std::ostringstream oss;
209     oss << "client num:  " << connectedAbilities.size() << std::endl;
210 
211     // Dump client info details
212     size_t index = 0;
213     for (const auto &iter : connectedAbilities) {
214         oss << std::endl << "client[" << index++ << "] info details: " << std::endl;
215         if (!iter.second) {
216             HILOG_ERROR("The connected ability[%{public}s] is null", iter.first.c_str());
217             continue;
218         }
219         AccessibilityAbilityInfo accessibilityAbilityInfo = iter.second->GetAbilityInfo();
220         oss << "    bundleName: " << accessibilityAbilityInfo.GetPackageName() << std::endl;
221         oss << "    moduleName: " << accessibilityAbilityInfo.GetModuleName() << std::endl;
222         oss << "    abilityName: " << accessibilityAbilityInfo.GetName() << std::endl;
223         oss << "    description: " << accessibilityAbilityInfo.GetDescription() << std::endl;
224 
225         std::string capabilities;
226         ConvertCapabilities(accessibilityAbilityInfo.GetCapabilityValues(), capabilities);
227         oss << "    capabilities: " << capabilities << std::endl;
228 
229         std::string abilityTypes;
230         ConvertAbilityTypes(accessibilityAbilityInfo.GetAccessibilityAbilityType(), abilityTypes);
231         oss << "    abilityTypes: " << abilityTypes << std::endl;
232 
233         std::string eventTypes;
234         ConvertEventTypes(accessibilityAbilityInfo.GetEventTypes(), eventTypes);
235         oss << "    eventTypes: " << eventTypes << std::endl;
236 
237         std::vector<std::string> targetBundleNames = accessibilityAbilityInfo.GetFilterBundleNames();
238         if (targetBundleNames.empty()) {
239             oss << "    targetBundleNames: " << "all" << std::endl;
240         } else {
241             oss << "    targetBundleNames: " << std::endl;
242             for (const auto &targetBundleName : targetBundleNames) {
243                 oss << "        " << targetBundleName << std::endl;
244             }
245         }
246 
247         if (index != connectedAbilities.size()) {
248             oss << std::endl << "    -------------------------------" << std::endl << std::endl;
249         }
250     }
251     dumpInfo.append(oss.str());
252     return 0;
253 }
254 
DumpAccessibilityUserInfo(std::string & dumpInfo) const255 int AccessibilityDumper::DumpAccessibilityUserInfo(std::string& dumpInfo) const
256 {
257     HILOG_INFO();
258     sptr<AccessibilityAccountData> currentAccount =
259         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
260     if (!currentAccount) {
261         HILOG_ERROR("currentAccount is null");
262         return -1;
263     }
264 
265     std::ostringstream oss;
266     oss << "id:  " << currentAccount->GetAccountId() << std::endl;
267 
268     std::shared_ptr<AccessibilitySettingsConfig> config = currentAccount->GetConfig();
269     if (!config) {
270         HILOG_ERROR("config is null");
271         return -1;
272     }
273 
274     // Dump Capabilities
275     oss << "accessible:  " << config->GetEnabledState() << std::endl;
276     oss << "touchGuide:  " << config->GetTouchGuideState() << std::endl;
277     oss << "gesture:  " << config->GetGestureState() << std::endl;
278     oss << "keyEventObserver:  " << config->GetKeyEventObserverState() << std::endl;
279 
280     // Dump setting info
281     oss << "screenMagnification:  " << config->GetScreenMagnificationState() << std::endl;
282     oss << "mouseKey:  " << config->GetMouseKeyState() << std::endl;
283     oss << "shortKey:  " << config->GetShortKeyState() << std::endl;
284     oss << "shortKeyOnLockScreen:  " << config->GetShortKeyOnLockScreenState() << std::endl;
285     oss << "animationOff:  " << config->GetAnimationOffState() << std::endl;
286     oss << "invertColor:  " << config->GetInvertColorState() << std::endl;
287     oss << "highContrastText:  " << config->GetHighContrastTextState() << std::endl;
288     oss << "audioMono:  " << config->GetAudioMonoState() << std::endl;
289     oss << "shortkeyTarget:  " << config->GetShortkeyTarget() << std::endl;
290     // need to add
291     oss << "mouseAutoClick:  " << config->GetMouseAutoClick() << std::endl;
292     oss << "daltonizationState:  " << config->GetDaltonizationState() << std::endl;
293     oss << "daltonizationColorFilter:  " << config->GetDaltonizationColorFilter() << std::endl;
294     oss << "contentTimeout:  " << config->GetContentTimeout() << std::endl;
295     oss << "brightnessDiscount:  " << config->GetBrightnessDiscount() << std::endl;
296     oss << "audioBalance:  " << config->GetAudioBalance() << std::endl;
297     oss << "clickResponseTime:  " << config->GetClickResponseTime() << std::endl;
298     oss << "ignoreRepeatClickState:  " << config->GetIgnoreRepeatClickState() << std::endl;
299     oss << "ignoreRepeatClickTime:  " << config->GetIgnoreRepeatClickTime() << std::endl;
300 
301     // Dump caption info
302     oss << "captionState:  " << config->GetCaptionState() << std::endl;
303     if (config->GetCaptionState()) {
304         AccessibilityConfig::CaptionProperty captionProperty = config->GetCaptionProperty();
305         oss << "    fontFamily:  " << captionProperty.GetFontFamily() << std::endl;
306         oss << "    fontScale:  " << captionProperty.GetFontScale() << std::endl;
307         oss << "    fontColor:  " << captionProperty.GetFontColor() << std::endl;
308         oss << "    fontEdgeType:  " << captionProperty.GetFontEdgeType() << std::endl;
309         oss << "    backgroundColor:  " << captionProperty.GetBackgroundColor() << std::endl;
310         oss << "    windowColor:  " << captionProperty.GetWindowColor() << std::endl;
311     }
312     dumpInfo.append(oss.str());
313     return 0;
314 }
315 
DumpAccessibilityInfo(const std::vector<std::string> & args,std::string & dumpInfo) const316 int AccessibilityDumper::DumpAccessibilityInfo(const std::vector<std::string>& args, std::string& dumpInfo) const
317 {
318     if (args.empty()) {
319         return -1;
320     }
321     DumpType dumpType = DumpType::DUMP_NONE;
322     if (args[0] == ARG_DUMP_USER) {
323         dumpType = DumpType::DUMP_USER;
324     } else if (args[0] == ARG_DUMP_CLIENT) {
325         dumpType = DumpType::DUMP_CLIENT;
326     } else if (args[0] == ARG_DUMP_ACCESSIBILITY_WINDOW) {
327         dumpType = DumpType::DUMP_ACCESSIBILITY_WINDOW;
328     }
329     int ret = 0;
330     switch (dumpType) {
331         case DumpType::DUMP_USER:
332             ret = DumpAccessibilityUserInfo(dumpInfo);
333             break;
334         case DumpType::DUMP_CLIENT:
335             ret = DumpAccessibilityClientInfo(dumpInfo);
336             break;
337         case DumpType::DUMP_ACCESSIBILITY_WINDOW:
338             ret = DumpAccessibilityWindowInfo(dumpInfo);
339             break;
340         default:
341             ret = -1;
342             break;
343     }
344     return ret;
345 }
346 
ShowIllegalArgsInfo(std::string & dumpInfo) const347 void AccessibilityDumper::ShowIllegalArgsInfo(std::string& dumpInfo) const
348 {
349     dumpInfo.append("The arguments are illegal and you can enter '-h' for help.");
350 }
351 
ShowHelpInfo(std::string & dumpInfo) const352 void AccessibilityDumper::ShowHelpInfo(std::string& dumpInfo) const
353 {
354     dumpInfo.append("Usage:\n")
355         .append(" -h                    ")
356         .append("|help text for the tool\n")
357         .append(" -u                    ")
358         .append("|dump accessibility current user in the system\n")
359         .append(" -c                    ")
360         .append("|dump accessibility client in the system\n")
361         .append(" -w                    ")
362         .append("|dump accessibility window info in the system\n");
363 }
364 } // Accessibility
365 } // OHOS