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 {
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 {
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 CharSequence className = getNodeForVirtualViewId(viewId).getClassName();
245 final int eventType =
246 className != null && className.equals("android.widget.ProgressBar")
247 ? AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
248 : AccessibilityEvent.TYPE_ANNOUNCEMENT;
249 final AccessibilityEvent event = obtainAccessibilityEvent(eventType);
250
251 event.setEnabled(true);
252 event.setClassName(className);
253 event.setContentDescription(value);
254
255 if (event.getText().isEmpty() && TextUtils.isEmpty(event.getContentDescription())) {
256 Log.w(TAG, "No value to announce for " + event.getClassName());
257 return;
258 }
259
260 event.setPackageName(m_view.getContext().getPackageName());
261 event.setSource(m_view, viewId);
262
263 if (!group.requestSendAccessibilityEvent(m_view, event))
264 Log.w(TAG, "Failed to send value change announcement for " + event.getClassName());
265 });
266 }
267
268 void notifyDescriptionOrNameChanged(int viewId, String value)
269 {
270 if (viewId == m_focusedVirtualViewId)
271 notifyValueChanged(viewId, value);
272 }
273
274 void notifyAnnouncementEvent(int viewId, String message)
275 {
276 QtNative.runAction(() -> {
277 if (m_view == null)
278 return;
279
280 if (viewId == INVALID_ID) {
281 Log.w(TAG, "notifyAnnouncementEvent() for invalid view");
282 return;
283 }
284
285 if (!m_manager.isEnabled()) {
286 Log.w(TAG, "notifyAnnouncementEvent for disabled AccessibilityManager");
287 return;
288 }
289
290 final AccessibilityEvent event =
291 obtainAccessibilityEvent(AccessibilityEvent.TYPE_ANNOUNCEMENT);
292 event.getText().add(message);
293 event.setClassName(getNodeForVirtualViewId(viewId).getClassName());
294 event.setPackageName(m_view.getContext().getPackageName());
295 sendAccessibilityEvent(event);
296 });
297 }
298
299 void sendEventForVirtualViewId(int virtualViewId, int eventType)
300 {
301 final AccessibilityEvent event = getEventForVirtualViewId(virtualViewId, eventType);
302 sendAccessibilityEvent(event);
303 }
304
305 void sendAccessibilityEvent(AccessibilityEvent event)
306 {
307 if (m_view == null || event == null)
308 return;
309
310 final ViewGroup group = (ViewGroup) m_view.getParent();
311 if (group == null) {
312 Log.w(TAG, "Could not send AccessibilityEvent because group was null. " +
313 "This should really not happen.");
314 return;
315 }
316
317 group.requestSendAccessibilityEvent(m_view, event);
318 }
319
320 void invalidateVirtualViewId(int virtualViewId)
321 {
322 final AccessibilityEvent event = getEventForVirtualViewId(virtualViewId,
323 AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
324
325 if (event == null)
326 return;
327
328 event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
329 sendAccessibilityEvent(event);
330 }
331
332 private void setHoveredVirtualViewId(int virtualViewId)
333 {
334 if (m_hoveredVirtualViewId == virtualViewId) {
335 return;
336 }
337
338 final int previousVirtualViewId = m_hoveredVirtualViewId;
339 m_hoveredVirtualViewId = virtualViewId;
340 sendEventForVirtualViewId(virtualViewId, AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
341 sendEventForVirtualViewId(previousVirtualViewId, AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
342 }
343
344 private AccessibilityEvent getEventForVirtualViewId(int virtualViewId, int eventType)
345 {
346 final boolean isManagerEnabled = m_manager != null && m_manager.isEnabled();
347 if (m_view == null || !isManagerEnabled || (virtualViewId == INVALID_ID)) {
348 Log.w(TAG, "getEventForVirtualViewId for invalid view");
349 return null;
350 }
351
352 if (m_layout == null || m_layout.getChildCount() == 0)
353 return null;
354
355 final AccessibilityEvent event = obtainAccessibilityEvent(eventType);
356
357 event.setEnabled(true);
358 event.setClassName(getNodeForVirtualViewId(virtualViewId).getClassName());
359
360 event.setContentDescription(QtNativeAccessibility.descriptionForAccessibleObject(virtualViewId));
361 if (event.getText().isEmpty() && TextUtils.isEmpty(event.getContentDescription()))
362 Log.w(TAG, "AccessibilityEvent with empty description");
363
364 event.setPackageName(m_view.getContext().getPackageName());
365 event.setSource(m_view, virtualViewId);
366 return event;
367 }
368
369 // This can be used for debug by performActionForVirtualViewId()
371 private void dumpNodes(int parentId)
372 {
373 Log.i(TAG, "A11Y hierarchy: " + parentId + " parent: " + QtNativeAccessibility.parentId(parentId));
374 Log.i(TAG, " desc: " + QtNativeAccessibility.descriptionForAccessibleObject(parentId) + " rect: " + QtNativeAccessibility.screenRect(parentId));
375 Log.i(TAG, " NODE: " + getNodeForVirtualViewId(parentId));
376 int[] ids = QtNativeAccessibility.childIdListForAccessibleObject(parentId);
377 for (int id : ids) {
378 Log.i(TAG, parentId + " has child: " + id);
379 dumpNodes(id);
380 }
381 }
382
383 private AccessibilityNodeInfo getNodeForView()
384 {
385 if (m_view == null || m_layout == null)
386 return obtainAccessibilityNodeInfo();
387
388 // Since we don't want the parent to be focusable, but we can't remove
389 // actions from a node, copy over the necessary fields.
390 final AccessibilityNodeInfo result = obtainAccessibilityNodeInfo(m_view);
391 final AccessibilityNodeInfo source = obtainAccessibilityNodeInfo(m_view);
392 m_view.onInitializeAccessibilityNodeInfo(source);
393
394 // Get the actual position on screen, taking the status bar into account.
395 m_view.getLocationOnScreen(m_globalOffset);
396 final int offsetX = m_globalOffset[0];
397 final int offsetY = m_globalOffset[1];
398
399 // Copy over parent and screen bounds.
400 final Rect m_tempParentRect = new Rect();
401 getBoundsInParent(source, m_tempParentRect);
402 setBoundsInParent(result, m_tempParentRect);
403
404 final Rect m_tempScreenRect = new Rect();
405 source.getBoundsInScreen(m_tempScreenRect);
406 m_tempScreenRect.offset(offsetX, offsetY);
407 result.setBoundsInScreen(m_tempScreenRect);
408
409 // Set up the parent view, if applicable.
410 final ViewParent parent = m_view.getParent();
411 if (parent instanceof View) {
412 result.setParent((View) parent);
413 }
414
415 result.setVisibleToUser(source.isVisibleToUser());
416 result.setPackageName(source.getPackageName());
417 result.setClassName(source.getClassName());
418
419 // Spit out the entire hierarchy for debugging purposes
420 // dumpNodes(-1);
421
422 if (m_layout.getChildCount() != 0) {
423 int[] ids = QtNativeAccessibility.childIdListForAccessibleObject(-1);
424 for (int id : ids)
425 result.addChild(m_view, id);
426 }
427
428 // The offset values have changed, so we need to re-focus the
429 // currently focused item, otherwise it will have an incorrect
430 // focus frame
431 if ((m_oldOffsetX != offsetX) || (m_oldOffsetY != offsetY)) {
432 m_oldOffsetX = offsetX;
433 m_oldOffsetY = offsetY;
434 if (m_focusedVirtualViewId != INVALID_ID) {
435 m_nodeProvider.performAction(m_focusedVirtualViewId,
436 AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS,
437 new Bundle());
438 m_nodeProvider.performAction(m_focusedVirtualViewId,
439 AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS,
440 new Bundle());
441 }
442 }
443
444 return result;
445 }
446
447 private AccessibilityNodeInfo getNodeForVirtualViewId(int virtualViewId)
448 {
449 if (m_view == null || m_layout == null)
450 return obtainAccessibilityNodeInfo();
451
452 final AccessibilityNodeInfo node = obtainAccessibilityNodeInfo();
453
454 node.setPackageName(m_view.getContext().getPackageName());
455
456 if (m_layout.getChildCount() == 0 || !QtNativeAccessibility.populateNode(virtualViewId, node)) {
457 return node;
458 }
459
460 // set only if valid, otherwise we return a node that is invalid and will crash when accessed
461 node.setSource(m_view, virtualViewId);
462
463 if (TextUtils.isEmpty(node.getText()) && TextUtils.isEmpty(node.getContentDescription()))
464 Log.w(TAG, "AccessibilityNodeInfo with empty contentDescription: " + virtualViewId);
465
466 int parentId = QtNativeAccessibility.parentId(virtualViewId);
467 node.setParent(m_view, parentId);
468
469 Rect screenRect = QtNativeAccessibility.screenRect(virtualViewId);
470 final int offsetX = m_globalOffset[0];
471 final int offsetY = m_globalOffset[1];
472 screenRect.offset(offsetX, offsetY);
473 node.setBoundsInScreen(screenRect);
474
475 Rect parentScreenRect = QtNativeAccessibility.screenRect(parentId);
476 screenRect.offset(-parentScreenRect.left, -parentScreenRect.top);
477 setBoundsInParent(node, screenRect);
478
479 // Manage internal accessibility focus state.
480 if (m_focusedVirtualViewId == virtualViewId) {
481 node.setAccessibilityFocused(true);
482 node.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
483 } else {
484 node.setAccessibilityFocused(false);
485 node.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_ACCESSIBILITY_FOCUS);
486 }
487
488 int[] ids = QtNativeAccessibility.childIdListForAccessibleObject(virtualViewId);
489 for (int id : ids)
490 node.addChild(m_view, id);
491 if (node.isScrollable()) {
492 setCollectionInfo(node, ids.length, 1, false);
493 }
494
495 return node;
496 }
497
498 private final AccessibilityNodeProvider m_nodeProvider = new AccessibilityNodeProvider()
499 {
500 @Override
501 public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId)
502 {
503 if (virtualViewId == View.NO_ID || m_layout.getChildCount() == 0) {
504 return getNodeForView();
505 }
506 return getNodeForVirtualViewId(virtualViewId);
507 }
508
509 @Override
510 public boolean performAction(int virtualViewId, int action, Bundle arguments)
511 {
512 if (m_view == null) {
513 Log.e(TAG, "Unable to perform action with a null view");
514 return false;
515 }
516
517 boolean handled = false;
518 //Log.i(TAG, "PERFORM ACTION: " + action + " on " + virtualViewId);
519 switch (action) {
520 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS:
521 if (m_focusedVirtualViewId == virtualViewId) {
522 m_focusedVirtualViewId = INVALID_ID;
523 }
524 // Since we're managing focus at the parent level, we are
525 // likely to receive a FOCUS action before a CLEAR_FOCUS
526 // action. We'll give the benefit of the doubt to the
527 // framework and always handle FOCUS_CLEARED.
528 m_view.invalidate();
529 sendEventForVirtualViewId(virtualViewId,
530 AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
531 handled = true;
532 break;
533 default:
534 // Let the node provider handle focus for the view node.
535 if (virtualViewId == View.NO_ID) {
536 return m_view.performAccessibilityAction(action, arguments);
537 }
538 }
539 handled |= performActionForVirtualViewId(virtualViewId, action);
540
541 return handled;
542 }
543 };
544
545 protected boolean performActionForVirtualViewId(int virtualViewId, int action)
546 {
547 //noinspection CommentedOutCode
548 {
549 // Log.i(TAG, "ACTION " + action + " on " + virtualViewId);
550 // dumpNodes(virtualViewId);
551 }
552 boolean success = false;
553 switch (action) {
554 case AccessibilityNodeInfo.ACTION_CLICK:
555 success = QtNativeAccessibility.clickAction(virtualViewId);
556 if (success)
557 sendEventForVirtualViewId(virtualViewId, AccessibilityEvent.TYPE_VIEW_CLICKED);
558 break;
559 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS:
560 if (m_focusedVirtualViewId != virtualViewId) {
561 success = QtNativeAccessibility.focusAction(virtualViewId);
562 if (!success) {
563 notifyObjectFocus(virtualViewId);
564 success = true;
565 }
566 }
567 break;
568 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD:
569 success = QtNativeAccessibility.scrollForward(virtualViewId);
570 break;
571 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD:
572 success = QtNativeAccessibility.scrollBackward(virtualViewId);
573 break;
574 }
575 return success;
576 }
577
578 @SuppressWarnings("deprecation")
579 private AccessibilityEvent obtainAccessibilityEvent(int eventType) {
580 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
581 return new AccessibilityEvent(eventType);
582 } else {
583 return AccessibilityEvent.obtain(eventType);
584 }
585 }
586
587 @SuppressWarnings("deprecation")
588 private AccessibilityNodeInfo obtainAccessibilityNodeInfo() {
589 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
590 return new AccessibilityNodeInfo();
591 } else {
592 return AccessibilityNodeInfo.obtain();
593 }
594 }
595
596 @SuppressWarnings("deprecation")
597 private AccessibilityNodeInfo obtainAccessibilityNodeInfo(View source) {
598 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
599 return new AccessibilityNodeInfo(source);
600 } else {
601 return AccessibilityNodeInfo.obtain(source);
602 }
603 }
604
605 @SuppressWarnings("deprecation")
606 private void getBoundsInParent(AccessibilityNodeInfo node, Rect outBounds) {
607 node.getBoundsInParent(outBounds);
608 }
609
610 @SuppressWarnings("deprecation")
611 private void setBoundsInParent(AccessibilityNodeInfo node, Rect bounds) {
612 node.setBoundsInParent(bounds);
613 }
614
615 @SuppressWarnings("deprecation")
616 private void setCollectionInfo(AccessibilityNodeInfo node, int rowCount, int columnCount,
617 boolean hierarchical) {
618 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
619 node.setCollectionInfo(new CollectionInfo(rowCount, columnCount, hierarchical));
620 } else {
621 node.setCollectionInfo(CollectionInfo.obtain(rowCount, columnCount, hierarchical));
622 }
623 }
624}
QList< QVariant > arguments
QMap< Name, StatePointer > Bundle
Definition lalr.h:46
EGLOutputLayerEXT EGLint EGLAttrib value
[3]
GLboolean GLuint group
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
const char className[16]
Definition qwizard.cpp:100
QQuickView * view
[0]