• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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().activeWindowId_ << 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_TEXT_SELECTION_UPDATE_EVENT, "textSelectionUpdate"},
173         {EventType::TYPE_PUBLIC_NOTICE_EVENT, "publicNotice"},
174         {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT, "accessibilityFocus"},
175         {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT, "accessibilityFocusClear"},
176         {EventType::TYPE_VIEW_TEXT_MOVE_UNIT_EVENT, "textMoveUnit"},
177         {EventType::TYPE_TOUCH_GUIDE_BEGIN, "touchGuideBegin"},
178         {EventType::TYPE_TOUCH_GUIDE_END, "touchGuideEnd"},
179         {EventType::TYPE_TOUCH_BEGIN, "touchBegin"},
180         {EventType::TYPE_TOUCH_END, "touchEnd"},
181         {EventType::TYPE_WINDOW_UPDATE, "windowUpdate"},
182         {EventType::TYPE_INTERRUPT_EVENT, "interrupt"},
183         {EventType::TYPE_GESTURE_EVENT, "gesture"}};
184 
185     for (auto itr = accessibilityEventTable.begin(); itr != accessibilityEventTable.end(); ++itr) {
186         if (value & itr->first) {
187             eventTypes += itr->second;
188             eventTypes += "/";
189         }
190     }
191 }
192 
DumpAccessibilityClientInfo(std::string & dumpInfo) const193 int AccessibilityDumper::DumpAccessibilityClientInfo(std::string& dumpInfo) const
194 {
195     HILOG_INFO();
196     sptr<AccessibilityAccountData> currentAccount =
197         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
198     if (!currentAccount) {
199         HILOG_ERROR("currentAccount is null");
200         return -1;
201     }
202 
203     auto connectedAbilities = currentAccount->GetConnectedA11yAbilities();
204     std::ostringstream oss;
205     oss << "client num:  " << connectedAbilities.size() << std::endl;
206 
207     // Dump client info details
208     size_t index = 0;
209     for (const auto &iter : connectedAbilities) {
210         oss << std::endl << "client[" << index++ << "] info details: " << std::endl;
211         if (!iter.second) {
212             HILOG_ERROR("The connected ability[%{public}s] is null", iter.first.c_str());
213             continue;
214         }
215         AccessibilityAbilityInfo accessibilityAbilityInfo = iter.second->GetAbilityInfo();
216         oss << "    bundleName: " << accessibilityAbilityInfo.GetPackageName() << std::endl;
217         oss << "    moduleName: " << accessibilityAbilityInfo.GetModuleName() << std::endl;
218         oss << "    abilityName: " << accessibilityAbilityInfo.GetName() << std::endl;
219         oss << "    description: " << accessibilityAbilityInfo.GetDescription() << std::endl;
220 
221         std::string capabilities;
222         ConvertCapabilities(accessibilityAbilityInfo.GetCapabilityValues(), capabilities);
223         oss << "    capabilities: " << capabilities << std::endl;
224 
225         std::string abilityTypes;
226         ConvertAbilityTypes(accessibilityAbilityInfo.GetAccessibilityAbilityType(), abilityTypes);
227         oss << "    abilityTypes: " << abilityTypes << std::endl;
228 
229         std::string eventTypes;
230         ConvertEventTypes(accessibilityAbilityInfo.GetEventTypes(), eventTypes);
231         oss << "    eventTypes: " << eventTypes << std::endl;
232 
233         std::vector<std::string> targetBundleNames = accessibilityAbilityInfo.GetFilterBundleNames();
234         if (targetBundleNames.empty()) {
235             oss << "    targetBundleNames: " << "all" << std::endl;
236         } else {
237             oss << "    targetBundleNames: " << std::endl;
238             for (const auto &targetBundleName : targetBundleNames) {
239                 oss << "        " << targetBundleName << std::endl;
240             }
241         }
242 
243         if (index != connectedAbilities.size()) {
244             oss << std::endl << "    -------------------------------" << std::endl << std::endl;
245         }
246     }
247     dumpInfo.append(oss.str());
248     return 0;
249 }
250 
DumpAccessibilityUserInfo(std::string & dumpInfo) const251 int AccessibilityDumper::DumpAccessibilityUserInfo(std::string& dumpInfo) const
252 {
253     HILOG_INFO();
254     sptr<AccessibilityAccountData> currentAccount =
255         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
256     if (!currentAccount) {
257         HILOG_ERROR("currentAccount is null");
258         return -1;
259     }
260 
261     std::ostringstream oss;
262     oss << "id:  " << currentAccount->GetAccountId() << std::endl;
263 
264     std::shared_ptr<AccessibilitySettingsConfig> config = currentAccount->GetConfig();
265     if (!config) {
266         HILOG_ERROR("config is null");
267         return -1;
268     }
269 
270     // Dump Capabilities
271     oss << "accessible:  " << config->GetEnabledState() << std::endl;
272     oss << "touchGuide:  " << config->GetTouchGuideState() << std::endl;
273     oss << "gesture:  " << config->GetGestureState() << std::endl;
274     oss << "keyEventObserver:  " << config->GetKeyEventObserverState() << std::endl;
275 
276     // Dump setting info
277     oss << "screenMagnification:  " << config->GetScreenMagnificationState() << std::endl;
278     oss << "mouseKey:  " << config->GetMouseKeyState() << std::endl;
279     oss << "shortKey:  " << config->GetShortKeyState() << std::endl;
280     oss << "animationOff:  " << config->GetAnimationOffState() << std::endl;
281     oss << "invertColor:  " << config->GetInvertColorState() << std::endl;
282     oss << "highContrastText:  " << config->GetHighContrastTextState() << std::endl;
283     oss << "audioMono:  " << config->GetAudioMonoState() << std::endl;
284     oss << "shortkeyTarget:  " << config->GetShortkeyTarget() << std::endl;
285     oss << "mouseAutoClick:  " << config->GetMouseAutoClick() << std::endl;
286     oss << "daltonizationColorFilter:  " << config->GetDaltonizationColorFilter() << std::endl;
287     oss << "contentTimeout:  " << config->GetContentTimeout() << std::endl;
288     oss << "brightnessDiscount:  " << config->GetBrightnessDiscount() << std::endl;
289     oss << "audioBalance:  " << config->GetAudioBalance() << std::endl;
290 
291     // Dump caption info
292     oss << "captionState:  " << config->GetCaptionState() << std::endl;
293     if (config->GetCaptionState()) {
294         AccessibilityConfig::CaptionProperty captionProperty = config->GetCaptionProperty();
295         oss << "    fontFamily:  " << captionProperty.GetFontFamily() << std::endl;
296         oss << "    fontScale:  " << captionProperty.GetFontScale() << std::endl;
297         oss << "    fontColor:  " << captionProperty.GetFontColor() << std::endl;
298         oss << "    fontEdgeType:  " << captionProperty.GetFontEdgeType() << std::endl;
299         oss << "    backgroundColor:  " << captionProperty.GetBackgroundColor() << std::endl;
300         oss << "    windowColor:  " << captionProperty.GetWindowColor() << std::endl;
301     }
302     dumpInfo.append(oss.str());
303     return 0;
304 }
305 
DumpAccessibilityInfo(const std::vector<std::string> & args,std::string & dumpInfo) const306 int AccessibilityDumper::DumpAccessibilityInfo(const std::vector<std::string>& args, std::string& dumpInfo) const
307 {
308     if (args.empty()) {
309         return -1;
310     }
311     DumpType dumpType = DumpType::DUMP_NONE;
312     if (args[0] == ARG_DUMP_USER) {
313         dumpType = DumpType::DUMP_USER;
314     } else if (args[0] == ARG_DUMP_CLIENT) {
315         dumpType = DumpType::DUMP_CLIENT;
316     } else if (args[0] == ARG_DUMP_ACCESSIBILITY_WINDOW) {
317         dumpType = DumpType::DUMP_ACCESSIBILITY_WINDOW;
318     }
319     int ret = 0;
320     switch (dumpType) {
321         case DumpType::DUMP_USER:
322             ret = DumpAccessibilityUserInfo(dumpInfo);
323             break;
324         case DumpType::DUMP_CLIENT:
325             ret = DumpAccessibilityClientInfo(dumpInfo);
326             break;
327         case DumpType::DUMP_ACCESSIBILITY_WINDOW:
328             ret = DumpAccessibilityWindowInfo(dumpInfo);
329             break;
330         default:
331             ret = -1;
332             break;
333     }
334     return ret;
335 }
336 
ShowIllegalArgsInfo(std::string & dumpInfo) const337 void AccessibilityDumper::ShowIllegalArgsInfo(std::string& dumpInfo) const
338 {
339     dumpInfo.append("The arguments are illegal and you can enter '-h' for help.");
340 }
341 
ShowHelpInfo(std::string & dumpInfo) const342 void AccessibilityDumper::ShowHelpInfo(std::string& dumpInfo) const
343 {
344     dumpInfo.append("Usage:\n")
345         .append(" -h                    ")
346         .append("|help text for the tool\n")
347         .append(" -u                    ")
348         .append("|dump accessibility current user in the system\n")
349         .append(" -c                    ")
350         .append("|dump accessibility client in the system\n")
351         .append(" -w                    ")
352         .append("|dump accessibility window info in the system\n");
353 }
354 } // Accessibility
355 } // OHOS