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