Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QtAccessibilityDelegate.java
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
5package org.qtproject.qt.android;
6
7import android.graphics.Rect;
8import android.os.Build;
9import android.os.Bundle;
10import android.system.Os;
11import android.text.TextUtils;
12import android.util.Log;
13import android.view.MotionEvent;
14import android.view.View;
15import android.view.ViewGroup;
16import android.view.ViewParent;
17import android.view.accessibility.AccessibilityEvent;
18import android.view.accessibility.AccessibilityManager;
19import android.view.accessibility.AccessibilityNodeInfo;
20import android.view.accessibility.AccessibilityNodeInfo.CollectionInfo;
21import android.view.accessibility.AccessibilityNodeProvider;
22
23class QtAccessibilityDelegate extends View.AccessibilityDelegate
24{
25 private static final String TAG = "Qt A11Y";
26
27 // Qt uses the upper half of the unsigned integers
28 // all low positive ints should be fine.
29 static final int INVALID_ID = 333; // half evil
30
31 private View m_view = null;
32 private AccessibilityManager m_manager;
33 private QtLayout m_layout;
34
35 // The accessible object that currently has the "accessibility focus"
36 // usually indicated by a yellow rectangle on screen.
37 private int m_focusedVirtualViewId = INVALID_ID;
38 // When exploring the screen by touch, the item "hovered" by the finger.
39 private int m_hoveredVirtualViewId = INVALID_ID;
40
41 // Cache coordinates of the view to know the global offset
42 // this is because the Android platform window does not take
43 // the offset of the view on screen into account (eg status bar on top)
44 private final int[] m_globalOffset = new int[2];
45 private int m_oldOffsetX = 0;
46 private int m_oldOffsetY = 0;
47
48 private class HoverEventListener implements View.OnHoverListener
49 {
50 @Override
51 public boolean onHover(View v, MotionEvent event)
52 {
53 return dispatchHoverEvent(event);
54 }
55 }
56 // TODO do we want to have one QtAccessibilityDelegate for the whole app (QtRootLayout) or
57 // e.g. one per window?
58 // FIXME make QtAccessibilityDelegate window based or verify current way works
59 // also for child windows: QTBUG-120685
60 QtAccessibilityDelegate() { }
61
62 void initLayoutAccessibility(QtLayout layout)
63 {
64 if (layout == null) {
65 Log.w(TAG, "Unable to initialize the accessibility delegate with a null layout");
66 return;
67 }
68
69 m_layout = layout;
70
71 m_manager = m_layout.getContext().getSystemService(AccessibilityManager.class);
72 if (m_manager != null) {
73 AccessibilityManagerListener accServiceListener = new AccessibilityManagerListener();
74 if (!m_manager.addAccessibilityStateChangeListener(accServiceListener))
75 Log.w("Qt A11y", "Could not register a11y state change listener");
76 if (m_manager.isEnabled())
77 accServiceListener.onAccessibilityStateChanged(true);
78 }
79 }
80
81 private class AccessibilityManagerListener
82 implements AccessibilityManager.AccessibilityStateChangeListener
83 {
84 @Override
85 public void onAccessibilityStateChanged(boolean enabled)
86 {
87 if (m_layout == null)
88 return;
89
90 final String isA11yOff = Os.getenv("QT_ANDROID_DISABLE_ACCESSIBILITY");
91 if (isA11yOff != null && (isA11yOff.equalsIgnoreCase("true") || isA11yOff.equals("1")))
92 return;
93
94 if (!enabled) {
95 if (m_view != null) {
96 m_layout.removeView(m_view);
97 m_view = null;
98 }
99 QtNativeAccessibility.setActive(enabled);
100 return;
101 }
102
103 try {
104 View view = m_view;
105 if (view == null) {
106 view = new View(m_layout.getContext());
107 view.setId(View.NO_ID);
108 }
109
110 // ### Keep this for debugging for a while. It allows us to visually see that our View
111 // ### is on top of the surface(s)
112 //noinspection CommentedOutCode
113 {
114 // ColorDrawable color = new ColorDrawable(0x80ff8080); //0xAARRGGBB
115 // view.setBackground(color);
116 }
117 view.setAccessibilityDelegate(QtAccessibilityDelegate.this);
118
119 // if all is fine, add it to the layout
120 if (m_view == null) {
121 //m_layout.addAccessibilityView(view);
122 m_layout.addView(view, m_layout.getChildCount(),
123 new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
124 ViewGroup.LayoutParams.MATCH_PARENT));
125 }
126 m_view = view;
127
128 m_view.setOnHoverListener(new HoverEventListener());
129 } catch (Exception e) {
130 // Unknown exception means something went wrong.
131 Log.w("Qt A11y", "Unknown exception: " + e);
132 }
133
134 QtNativeAccessibility.setActive(enabled);
135 }
136 }
137
138 @Override
139 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host)
140 {
141 return m_nodeProvider;
142 }
143
144 // For "explore by touch" we need all movement events here first
145 // (user moves finger over screen to discover items on screen).
146 private boolean dispatchHoverEvent(MotionEvent event)
147 {
148 if (m_manager == null || !m_manager.isTouchExplorationEnabled()) {
149 return false;
150 }
151
152 int virtualViewId = QtNativeAccessibility.hitTest(event.getX(), event.getY());
153 if (virtualViewId == INVALID_ID) {
154 virtualViewId = View.NO_ID;
155 }
156
157 switch (event.getAction()) {
158 case MotionEvent.ACTION_HOVER_ENTER:
159 case MotionEvent.ACTION_HOVER_MOVE:
160 case MotionEvent.ACTION_HOVER_EXIT:
161 setHoveredVirtualViewId(virtualViewId);
162 break;
163 }
164
165 return true;
166 }
167
168 void notifyScrolledEvent(int viewId)
169 {
170 QtNative.runAction(() -> sendEventForVirtualViewId(viewId,
171 AccessibilityEvent.TYPE_VIEW_SCROLLED));
172 }
173
174 void notifyLocationChange(int viewId)
175 {
176 QtNative.runAction(() -> {
177 if (m_focusedVirtualViewId == viewId)
178 invalidateVirtualViewId(m_focusedVirtualViewId);
179 });
180 }
181
182 void notifyObjectHide(int viewId, int parentId)
183 {
184 QtNative.runAction(() -> {
185 // If the object had accessibility focus, we need to clear it.
186 // Note: This code is mostly copied from
187 // AccessibilityNodeProvider::performAction, but we remove the
188 // focus only if the focused view id matches the one that was hidden.
189 if (m_view != null && m_focusedVirtualViewId == viewId) {
190 m_focusedVirtualViewId = INVALID_ID;
191 m_view.invalidate();
192 sendEventForVirtualViewId(viewId,
193 AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
194 }
195 // When the object is hidden, we need to notify its parent about
196 // content change, not the hidden object itself
197 invalidateVirtualViewId(parentId);
198 });
199 }
200
201 void notifyObjectShow(int parentId)
202 {
203 QtNative.runAction(() -> {
204 // When the object is shown, we need to notify its parent about
205 // content change, not the shown object itself
206 invalidateVirtualViewId(parentId);
207 });
208 }
209
210 void notifyObjectFocus(int viewId)
211 {
212 QtNative.runAction(() -> {
213 if (m_view == null)
214 return;
215 m_focusedVirtualViewId = viewId;
216 m_view.invalidate();
217 sendEventForVirtualViewId(viewId,
218 AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
219 });
220 }
221
222 void notifyValueChanged(int viewId, String value)
223 {
224 if (m_manager == null)
225 return;
226
227 QtNative.runAction(() -> {
228 if (m_view == null)
229 return;
230
231 // Send a TYPE_ANNOUNCEMENT event with the new value
232
233 if ((viewId == INVALID_ID) || !m_manager.isEnabled()) {
234 Log.w(TAG, "notifyValueChanged() for invalid view");
235 return;
236 }
237
238 final ViewGroup group = (ViewGroup) m_view.getParent();
239 if (group == null) {
240 Log.w(TAG, "Could not announce value because ViewGroup was null.");
241 return;
242 }
243
244 final AccessibilityEvent event =
245 obtainAccessibilityEvent(AccessibilityEvent.TYPE_ANNOUNCEMENT);
246
247 event.setEnabled(true);
248 event.setClassName(getNodeForVirtualViewId(viewId).getClassName());
249
250 event.setContentDescription(value);
251
252 if (event.getText().isEmpty() && TextUtils.isEmpty(event.getContentDescription())) {
253 Log.w(TAG, "No value to announce for " + event.getClassName());
254 return;
255 }
256
257 event.setPackageName(m_view.getContext().getPackageName());
258 event.setSource(m_view, viewId);
259
260 if (!group.requestSendAccessibilityEvent(m_view, event))
261 Log.w(TAG, "Failed to send value change announcement for " + event.getClassName());
262 });
263 }
264
265 void notifyDescriptionOrNameChanged(int viewId, String value)
266 {
267 if (viewId == m_focusedVirtualViewId)
268 notifyValueChanged(viewId, value);
269 }
270
271 void notifyAnnouncementEvent(int viewId, String message)
272 {
273 QtNative.runAction(() -> {
274 if (m_view == null)
275 return;
276
277 if (viewId == INVALID_ID) {
278 Log.w(TAG, "notifyAnnouncementEvent() for invalid view");
279 return;
280 }
281
282 if (!m_manager.isEnabled()) {
283 Log.w(TAG, "notifyAnnouncementEvent for disabled AccessibilityManager");
284 return;
285 }
286
287 final AccessibilityEvent event =
288 obtainAccessibilityEvent(AccessibilityEvent.TYPE_ANNOUNCEMENT);
289 event.getText().add(message);
290 event.setClassName(getNodeForVirtualViewId(viewId).getClassName());
291 event.setPackageName(m_view.getContext().getPackageName());
292 sendAccessibilityEvent(event);
293 });
294 }
295
296 void sendEventForVirtualViewId(int virtualViewId, int eventType)
297 {
298 final AccessibilityEvent event = getEventForVirtualViewId(virtualViewId, eventType);
299 sendAccessibilityEvent(event);
300 }
301
302 void sendAccessibilityEvent(AccessibilityEvent event)
303 {
304 if (m_view == null || event == null)
305 return;
306
307 final ViewGroup group = (ViewGroup) m_view.getParent();
308 if (group == null) {
309 Log.w(TAG, "Could not send AccessibilityEvent because group was null. " +
310 "This should really not happen.");
311 return;
312 }
313
314 group.requestSendAccessibilityEvent(m_view, event);
315 }
316
317 void invalidateVirtualViewId(int virtualViewId)
318 {
319 final AccessibilityEvent event = getEventForVirtualViewId(virtualViewId,
320 AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
321
322 if (event == null)
323 return;
324
325 event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
326 sendAccessibilityEvent(event);
327 }
328
329 private void setHoveredVirtualViewId(int virtualViewId)
330 {
331 if (m_hoveredVirtualViewId == virtualViewId) {
332 return;
333 }
334
335 final int previousVirtualViewId = m_hoveredVirtualViewId;
336 m_hoveredVirtualViewId = virtualViewId;
337 sendEventForVirtualViewId(virtualViewId, AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
338 sendEventForVirtualViewId(previousVirtualViewId, AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
339 }
340
341 private AccessibilityEvent getEventForVirtualViewId(int virtualViewId, int eventType)
342 {
343 final boolean isManagerEnabled = m_manager != null && m_manager.isEnabled();
344 if (m_view == null || !isManagerEnabled || (virtualViewId == INVALID_ID)) {
345 Log.w(TAG, "getEventForVirtualViewId for invalid view");
346 return null;
347 }
348
349 if (m_layout == null || m_layout.getChildCount() == 0)
350 return null;
351
352 final AccessibilityEvent event = obtainAccessibilityEvent(eventType);
353
354 event.setEnabled(true);
355 event.setClassName(getNodeForVirtualViewId(virtualViewId).getClassName());
356
357 event.setContentDescription(QtNativeAccessibility.descriptionForAccessibleObject(virtualViewId));
358 if (event.getText().isEmpty() && TextUtils.isEmpty(event.getContentDescription()))
359 Log.w(TAG, "AccessibilityEvent with empty description");
360
361 event.setPackageName(m_view.getContext().getPackageName());
362 event.setSource(m_view, virtualViewId);
363 return event;
364 }
365
366 // This can be used for debug by performActionForVirtualViewId()
368 private void dumpNodes(int parentId)
369 {
370 Log.i(TAG, "A11Y hierarchy: " + parentId + " parent: " + QtNativeAccessibility.parentId(parentId));
371 Log.i(TAG, " desc: " + QtNativeAccessibility.descriptionForAccessibleObject(parentId) + " rect: " + QtNativeAccessibility.screenRect(parentId));
372 Log.i(TAG, " NODE: " + getNodeForVirtualViewId(parentId));
373 int[] ids = QtNativeAccessibility.childIdListForAccessibleObject(parentId);
374 for (int id : ids) {
375 Log.i(TAG, parentId + " has child: " + id);
376 dumpNodes(id);
377 }
378 }
379
380 private AccessibilityNodeInfo getNodeForView()
381 {
382 if (m_view == null || m_layout == null)
383 return obtainAccessibilityNodeInfo();
384
385 // Since we don't want the parent to be focusable, but we can't remove
386 // actions from a node, copy over the necessary fields.
387 final AccessibilityNodeInfo result = obtainAccessibilityNodeInfo(m_view);
388 final AccessibilityNodeInfo source = obtainAccessibilityNodeInfo(m_view);
389 m_view.onInitializeAccessibilityNodeInfo(source);
390
391 // Get the actual position on screen, taking the status bar into account.
392 m_view.getLocationOnScreen(m_globalOffset);
393 final int offsetX = m_globalOffset[0];
394 final int offsetY = m_globalOffset[1];
395
396 // Copy over parent and screen bounds.
397 final Rect m_tempParentRect = new Rect();
398 getBoundsInParent(source, m_tempParentRect);
399 setBoundsInParent(result, m_tempParentRect);
400
401 final Rect m_tempScreenRect = new Rect();
402 source.getBoundsInScreen(m_tempScreenRect);
403 m_tempScreenRect.offset(offsetX, offsetY);
404 result.setBoundsInScreen(m_tempScreenRect);
405
406 // Set up the parent view, if applicable.
407 final ViewParent parent = m_view.getParent();
408 if (parent instanceof View) {
409 result.setParent((View) parent);
410 }
411
412 result.setVisibleToUser(source.isVisibleToUser());
413 result.setPackageName(source.getPackageName());
414 result.setClassName(source.getClassName());
415
416 // Spit out the entire hierarchy for debugging purposes
417 // dumpNodes(-1);
418
419 if (m_layout.getChildCount() != 0) {
420 int[] ids = QtNativeAccessibility.childIdListForAccessibleObject(-1);
421 for (int id : ids)
422 result.addChild(m_view, id);
423 }
424
425 // The offset values have changed, so we need to re-focus the
426 // currently focused item, otherwise it will have an incorrect
427 // focus frame
428 if ((m_oldOffsetX != offsetX) || (m_oldOffsetY != offsetY)) {
429 m_oldOffsetX = offsetX;
430 m_oldOffsetY = offsetY;
431 if (m_focusedVirtualViewId != INVALID_ID) {
432 m_nodeProvider.performAction(m_focusedVirtualViewId,
433 AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS,
434 new Bundle());
435 m_nodeProvider.performAction(m_focusedVirtualViewId,
436 AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS,
437 new Bundle());
438 }
439 }
440
441 return result;
442 }
443
444 private AccessibilityNodeInfo getNodeForVirtualViewId(int virtualViewId)
445 {
446 if (m_view == null || m_layout == null)
447 return obtainAccessibilityNodeInfo();
448
449 final AccessibilityNodeInfo node = obtainAccessibilityNodeInfo();
450
451 node.setPackageName(m_view.getContext().getPackageName());
452
453 if (m_layout.getChildCount() == 0 || !QtNativeAccessibility.populateNode(virtualViewId, node)) {
454 return node;
455 }
456
457 // set only if valid, otherwise we return a node that is invalid and will crash when accessed
458 node.setSource(m_view, virtualViewId);
459
460 if (TextUtils.isEmpty(node.getText()) && TextUtils.isEmpty(node.getContentDescription()))
461 Log.w(TAG, "AccessibilityNodeInfo with empty contentDescription: " + virtualViewId);
462
463 int parentId = QtNativeAccessibility.parentId(virtualViewId);
464 node.setParent(m_view, parentId);
465
466 Rect screenRect = QtNativeAccessibility.screenRect(virtualViewId);
467 final int offsetX = m_globalOffset[0];
468 final int offsetY = m_globalOffset[1];
469 screenRect.offset(offsetX, offsetY);
470 node.setBoundsInScreen(screenRect);
471
472 Rect parentScreenRect = QtNativeAccessibility.screenRect(parentId);
473 screenRect.offset(-parentScreenRect.left, -parentScreenRect.top);
474 setBoundsInParent(node, screenRect);
475
476 // Manage internal accessibility focus state.
477 if (m_focusedVirtualViewId == virtualViewId) {
478 node.setAccessibilityFocused(true);
479 node.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
480 } else {
481 node.setAccessibilityFocused(false);
482 node.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_ACCESSIBILITY_FOCUS);
483 }
484
485 int[] ids = QtNativeAccessibility.childIdListForAccessibleObject(virtualViewId);
486 for (int id : ids)
487 node.addChild(m_view, id);
488 if (node.isScrollable()) {
489 setCollectionInfo(node, ids.length, 1, false);
490 }
491
492 return node;
493 }
494
495 private final AccessibilityNodeProvider m_nodeProvider = new AccessibilityNodeProvider()
496 {
497 @Override
498 public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId)
499 {
500 if (virtualViewId == View.NO_ID || m_layout.getChildCount() == 0) {
501 return getNodeForView();
502 }
503 return getNodeForVirtualViewId(virtualViewId);
504 }
505
506 @Override
507 public boolean performAction(int virtualViewId, int action, Bundle arguments)
508 {
509 if (m_view == null) {
510 Log.e(TAG, "Unable to perform action with a null view");
511 return false;
512 }
513
514 boolean handled = false;
515 //Log.i(TAG, "PERFORM ACTION: " + action + " on " + virtualViewId);
516 switch (action) {
517 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS:
518 if (m_focusedVirtualViewId == virtualViewId) {
519 m_focusedVirtualViewId = INVALID_ID;
520 }
521 // Since we're managing focus at the parent level, we are
522 // likely to receive a FOCUS action before a CLEAR_FOCUS
523 // action. We'll give the benefit of the doubt to the
524 // framework and always handle FOCUS_CLEARED.
525 m_view.invalidate();
526 sendEventForVirtualViewId(virtualViewId,
527 AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
528 handled = true;
529 break;
530 default:
531 // Let the node provider handle focus for the view node.
532 if (virtualViewId == View.NO_ID) {
533 return m_view.performAccessibilityAction(action, arguments);
534 }
535 }
536 handled |= performActionForVirtualViewId(virtualViewId, action);
537
538 return handled;
539 }
540 };
541
542 protected boolean performActionForVirtualViewId(int virtualViewId, int action)
543 {
544 //noinspection CommentedOutCode
545 {
546 // Log.i(TAG, "ACTION " + action + " on " + virtualViewId);
547 // dumpNodes(virtualViewId);
548 }
549 boolean success = false;
550 switch (action) {
551 case AccessibilityNodeInfo.ACTION_CLICK:
552 success = QtNativeAccessibility.clickAction(virtualViewId);
553 if (success)
554 sendEventForVirtualViewId(virtualViewId, AccessibilityEvent.TYPE_VIEW_CLICKED);
555 break;
556 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS:
557 if (m_focusedVirtualViewId != virtualViewId) {
558 success = QtNativeAccessibility.focusAction(virtualViewId);
559 if (!success) {
560 notifyObjectFocus(virtualViewId);
561 success = true;
562 }
563 }
564 break;
565 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD:
566 success = QtNativeAccessibility.scrollForward(virtualViewId);
567 break;
568 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD:
569 success = QtNativeAccessibility.scrollBackward(virtualViewId);
570 break;
571 }
572 return success;
573 }
574
575 @SuppressWarnings("deprecation")
576 private AccessibilityEvent obtainAccessibilityEvent(int eventType) {
577 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
578 return new AccessibilityEvent(eventType);
579 } else {
580 return AccessibilityEvent.obtain(eventType);
581 }
582 }
583
584 @SuppressWarnings("deprecation")
585 private AccessibilityNodeInfo obtainAccessibilityNodeInfo() {
586 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
587 return new AccessibilityNodeInfo();
588 } else {
589 return AccessibilityNodeInfo.obtain();
590 }
591 }
592
593 @SuppressWarnings("deprecation")
594 private AccessibilityNodeInfo obtainAccessibilityNodeInfo(View source) {
595 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
596 return new AccessibilityNodeInfo(source);
597 } else {
598 return AccessibilityNodeInfo.obtain(source);
599 }
600 }
601
602 @SuppressWarnings("deprecation")
603 private void getBoundsInParent(AccessibilityNodeInfo node, Rect outBounds) {
604 node.getBoundsInParent(outBounds);
605 }
606
607 @SuppressWarnings("deprecation")
608 private void setBoundsInParent(AccessibilityNodeInfo node, Rect bounds) {
609 node.setBoundsInParent(bounds);
610 }
611
612 @SuppressWarnings("deprecation")
613 private void setCollectionInfo(AccessibilityNodeInfo node, int rowCount, int columnCount,
614 boolean hierarchical) {
615 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
616 node.setCollectionInfo(new CollectionInfo(rowCount, columnCount, hierarchical));
617 } else {
618 node.setCollectionInfo(CollectionInfo.obtain(rowCount, columnCount, hierarchical));
619 }
620 }
621}
QList< QVariant > arguments
QMap< Name, StatePointer > Bundle
Definition lalr.h:46
EGLOutputLayerEXT EGLint EGLAttrib value
[3]
GLboolean GLuint group
[16]
GLenum GLenum GLsizei const GLuint * ids
[qttrid_noop]
GLuint GLsizei const GLchar * message
GLsizei const GLfloat * v
GLsizei GLsizei GLchar * source
struct _cl_event * event
GLuint64EXT * result
[6]
static QEvent::Type eventType(int profilerEventType)
#define enabled
QGraphicsGridLayout * layout
QQuickView * view
[0]