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