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
androidjniaccessibility.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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// Qt-Security score:significant reason:default
4
10#include "qpa/qplatformaccessibility.h"
11#include <QtGui/private/qaccessiblebridgeutils_p.h>
13#include "qwindow.h"
14#include "qrect.h"
15#include "QtGui/qaccessible.h"
16#include <QtCore/qmath.h>
17#include <QtCore/private/qjnihelpers_p.h>
18#include <QtCore/QJniObject>
19#include <QtGui/private/qhighdpiscaling_p.h>
20
21#include <QtCore/QObject>
22#include <QtCore/qpointer.h>
23#include <QtCore/qscopeguard.h>
24#include <QtCore/qvarlengtharray.h>
25
26static const char m_qtTag[] = "Qt A11Y";
27
28QT_BEGIN_NAMESPACE
29
30using namespace Qt::StringLiterals;
31
33{
49
50 static int RANGE_TYPE_INT = 0;
51 static int RANGE_TYPE_FLOAT = 0;
52 static int RANGE_TYPE_PERCENT = 0;
54
55 static bool m_accessibilityActivated = false;
56
57 // This object is needed to schedule the execution of the code that
58 // deals with accessibility instances to the Qt main thread.
59 // Because of that almost every method here is split into two parts.
60 // The _helper part is executed in the context of m_accessibilityContext
61 // on the main thread. The other part is executed in Java thread.
63
64 // This method is called from the Qt main thread, and normally a
65 // QGuiApplication instance will be used as a parent.
67 {
68 if (m_accessibilityContext)
69 m_accessibilityContext->deleteLater();
70 m_accessibilityContext = new QObject(parent);
71 }
72
73 template <typename Func, typename Ret>
74 void runInObjectContext(QObject *context, Func &&func, Ret *retVal)
75 {
77 __android_log_print(ANDROID_LOG_WARN, m_qtTag,
78 "Could not run accessibility call in object context, no valid surface.");
79 return;
80 }
81
82 QtAndroidPrivate::AndroidDeadlockProtector protector(
83 u"QtAndroidAccessibility::runInObjectContext()"_s);
84 if (!protector.acquire()) {
85 __android_log_print(ANDROID_LOG_WARN, m_qtTag,
86 "Could not run accessibility call in object context, accessing "
87 "main thread could lead to deadlock");
88 return;
89 }
90
91 if (!QtAndroid::blockEventLoopsWhenSuspended()
92 || QGuiApplication::applicationState() != Qt::ApplicationSuspended) {
93 QMetaObject::invokeMethod(context, func, Qt::BlockingQueuedConnection, retVal);
94 } else {
95 __android_log_print(ANDROID_LOG_WARN, m_qtTag,
96 "Could not run accessibility call in object context, event loop suspended.");
97 }
98 }
99
100 bool isActive()
101 {
103 }
104
105 static void setActive(JNIEnv */*env*/, jobject /*thiz*/, jboolean active)
106 {
107 QMutexLocker lock(QtAndroid::platformInterfaceMutex());
110 if (platformIntegration) {
111 platformIntegration->accessibility()->setActive(active);
112 } else {
113 __android_log_print(ANDROID_LOG_DEBUG, m_qtTag,
114 "Android platform integration is not ready, accessibility activation deferred.");
115 }
116 }
117
118 QAccessibleInterface *interfaceFromId(jint objectId)
119 {
120 QAccessibleInterface *iface = nullptr;
121 if (objectId == -1) {
122 QWindow *win = qApp->focusWindow();
123 if (win)
124 iface = win->accessibleRoot();
125 } else {
126 iface = QAccessible::accessibleInterface(objectId);
127 }
128 return iface;
129 }
130
131 void notifyLocationChange(uint accessibilityObjectId)
132 {
133 QtAndroid::notifyAccessibilityLocationChange(accessibilityObjectId);
134 }
135
136 static int parentId_helper(int objectId); // forward declaration
137
138 void notifyObjectHide(uint accessibilityObjectId)
139 {
140 const auto parentObjectId = parentId_helper(accessibilityObjectId);
141 QtAndroid::notifyObjectHide(accessibilityObjectId, parentObjectId);
142 }
143
144 void notifyObjectShow(uint accessibilityObjectId)
145 {
146 const auto parentObjectId = parentId_helper(accessibilityObjectId);
147 QtAndroid::notifyObjectShow(parentObjectId);
148 }
149
150 void notifyObjectFocus(uint accessibilityObjectId)
151 {
152 QtAndroid::notifyObjectFocus(accessibilityObjectId);
153 }
154
155 static jstring jvalueForAccessibleObject(int objectId); // forward declaration
156
157 void notifyValueChanged(uint accessibilityObjectId)
158 {
159 jstring value = jvalueForAccessibleObject(accessibilityObjectId);
160 QtAndroid::notifyValueChanged(accessibilityObjectId, value);
161 }
162
163 // Forward declaration
164 static QString descriptionForInterface(QAccessibleInterface *iface);
165
166 void notifyDescriptionOrNameChanged(uint accessibilityObjectId)
167 {
168 QAccessibleInterface *iface = interfaceFromId(accessibilityObjectId);
169 if (iface && iface->isValid()) {
170 const QString value = descriptionForInterface(iface);
171 QtAndroid::notifyDescriptionOrNameChanged(accessibilityObjectId, value);
172 }
173 }
174
175 void notifyScrolledEvent(uint accessiblityObjectId)
176 {
177 QtAndroid::notifyScrolledEvent(accessiblityObjectId);
178 }
179
180 void notifyAnnouncementEvent(uint accessibilityObjectId, const QString &message)
181 {
182 QtAndroid::notifyAnnouncementEvent(accessibilityObjectId, message);
183 }
184
186 {
187 QAccessibleInterface *iface = interfaceFromId(objectId);
188 if (iface && iface->isValid()) {
189 const int childCount = iface->childCount();
190 QVarLengthArray<jint, 8> ifaceIdArray;
191 ifaceIdArray.reserve(childCount);
192 for (int i = 0; i < childCount; ++i) {
193 QAccessibleInterface *child = iface->child(i);
194 if (child && child->isValid())
195 ifaceIdArray.append(QAccessible::uniqueId(child));
196 }
197 return ifaceIdArray;
198 }
199 return {};
200 }
201
202 static jintArray childIdListForAccessibleObject(JNIEnv *env, jobject /*thiz*/, jint objectId)
203 {
204 if (m_accessibilityContext) {
205 QVarLengthArray<jint, 8> ifaceIdArray;
206 runInObjectContext(m_accessibilityContext, [objectId]() {
207 return childIdListForAccessibleObject_helper(objectId);
208 }, &ifaceIdArray);
209 jintArray jArray = env->NewIntArray(jsize(ifaceIdArray.count()));
210 env->SetIntArrayRegion(jArray, 0, ifaceIdArray.count(), ifaceIdArray.data());
211 return jArray;
212 }
213
214 return env->NewIntArray(jsize(0));
215 }
216
217 static int parentId_helper(int objectId)
218 {
219 QAccessibleInterface *iface = interfaceFromId(objectId);
220 if (iface && iface->isValid()) {
221 QAccessibleInterface *parent = iface->parent();
222 if (parent && parent->isValid()) {
223 if (parent->role() == QAccessible::Application)
224 return -1;
225 return QAccessible::uniqueId(parent);
226 }
227 }
228 return -1;
229 }
230
231 static jint parentId(JNIEnv */*env*/, jobject /*thiz*/, jint objectId)
232 {
233 jint result = -1;
234 if (m_accessibilityContext) {
235 runInObjectContext(m_accessibilityContext, [objectId]() {
236 return parentId_helper(objectId);
237 }, &result);
238 }
239 return result;
240 }
241
242 static QRect screenRect_helper(int objectId, bool clip = true)
243 {
244 QRect rect;
245 QAccessibleInterface *iface = interfaceFromId(objectId);
246 if (iface && iface->isValid()) {
247 rect = QHighDpi::toNativePixels(iface->rect(), iface->window());
248 }
249 // If the widget is not fully in-bound in its parent then we have to clip the rectangle to draw
250 if (clip && iface && iface->parent() && iface->parent()->isValid()) {
251 const auto parentRect = QHighDpi::toNativePixels(iface->parent()->rect(), iface->parent()->window());
252 rect = rect.intersected(parentRect);
253 }
254 return rect;
255 }
256
257 static jobject screenRect(JNIEnv *env, jobject /*thiz*/, jint objectId)
258 {
259 QRect rect;
260 if (m_accessibilityContext) {
261 runInObjectContext(m_accessibilityContext, [objectId]() {
262 return screenRect_helper(objectId);
263 }, &rect);
264 }
265 jclass rectClass = env->FindClass("android/graphics/Rect");
266 jmethodID ctor = env->GetMethodID(rectClass, "<init>", "(IIII)V");
267 jobject jrect = env->NewObject(rectClass, ctor, rect.left(), rect.top(), rect.right(), rect.bottom());
268 return jrect;
269 }
270
271 static int hitTest_helper(float x, float y)
272 {
273 QAccessibleInterface *root = interfaceFromId(-1);
274 if (root && root->isValid()) {
275 QPoint pos = QHighDpi::fromNativePixels(QPoint(int(x), int(y)), root->window());
276
277 QAccessibleInterface *child = root->childAt(pos.x(), pos.y());
278 QAccessibleInterface *lastChild = nullptr;
279 while (child && (child != lastChild)) {
280 lastChild = child;
281 child = child->childAt(pos.x(), pos.y());
282 }
283 if (lastChild)
284 return QAccessible::uniqueId(lastChild);
285 }
286 return -1;
287 }
288
289 static jint hitTest(JNIEnv */*env*/, jobject /*thiz*/, jfloat x, jfloat y)
290 {
291 jint result = -1;
292 if (m_accessibilityContext) {
293 runInObjectContext(m_accessibilityContext, [x, y]() {
294 return hitTest_helper(x, y);
295 }, &result);
296 }
297 return result;
298 }
299
300 static void invokeActionOnInterfaceInMainThread(QAccessibleActionInterface* actionInterface,
301 const QString& action)
302 {
303 // Queue the action and return back to Java thread, so that we do not
304 // block it for too long
305 QMetaObject::invokeMethod(qApp, [actionInterface, action]() {
306 actionInterface->doAction(action);
307 }, Qt::QueuedConnection);
308 }
309
310 static bool clickAction_helper(int objectId)
311 {
312 QAccessibleInterface *iface = interfaceFromId(objectId);
313 if (!iface || !iface->isValid() || !iface->actionInterface())
314 return false;
315
316 const auto& actionNames = iface->actionInterface()->actionNames();
317
318 if (actionNames.contains(QAccessibleActionInterface::pressAction())) {
319 invokeActionOnInterfaceInMainThread(iface->actionInterface(),
320 QAccessibleActionInterface::pressAction());
321 } else if (actionNames.contains(QAccessibleActionInterface::toggleAction())) {
322 invokeActionOnInterfaceInMainThread(iface->actionInterface(),
323 QAccessibleActionInterface::toggleAction());
324 } else {
325 return false;
326 }
327 return true;
328 }
329
330 static bool focusAction_helper(int objectId)
331 {
332 QAccessibleInterface *iface = interfaceFromId(objectId);
333 if (!iface || !iface->isValid() || !iface->actionInterface())
334 return false;
335
336 const auto& actionNames = iface->actionInterface()->actionNames();
337
338 if (actionNames.contains(QAccessibleActionInterface::setFocusAction())) {
339 QAccessibleActionInterface *actionInterface = iface->actionInterface();
340 // Suppress keyboard activation during accessibility focus navigation.
341 QMetaObject::invokeMethod(qApp, [actionInterface]() {
342 auto *inputContext = QAndroidInputContext::androidInputContext();
343 if (inputContext)
344 inputContext->setAccessibilityFocusInProgress(true);
345 const auto resetGuard = qScopeGuard([inputContext] {
346 if (inputContext)
347 inputContext->setAccessibilityFocusInProgress(false);
348 });
349 actionInterface->doAction(QAccessibleActionInterface::setFocusAction());
350 }, Qt::QueuedConnection);
351 return true;
352 }
353 return false;
354 }
355
356 static jboolean clickAction(JNIEnv */*env*/, jobject /*thiz*/, jint objectId)
357 {
358 bool result = false;
359 if (m_accessibilityContext) {
360 runInObjectContext(m_accessibilityContext, [objectId]() {
361 return clickAction_helper(objectId);
362 }, &result);
363 }
364 return result;
365 }
366
367 static jboolean focusAction(JNIEnv */*env*/, jobject /*thiz*/, jint objectId)
368 {
369 bool result = false;
370 if (m_accessibilityContext) {
371 runInObjectContext(m_accessibilityContext, [objectId]() {
372 return focusAction_helper(objectId);
373 }, &result);
374 }
375 return result;
376 }
377
378 static bool scroll_helper(int objectId, const QString &actionName)
379 {
380 QAccessibleInterface *iface = interfaceFromId(objectId);
381 if (iface && iface->isValid())
382 return QAccessibleBridgeUtils::performEffectiveAction(iface, actionName);
383 return false;
384 }
385
386 static jboolean scrollForward(JNIEnv */*env*/, jobject /*thiz*/, jint objectId)
387 {
388 bool result = false;
389
390 const auto& ids = childIdListForAccessibleObject_helper(objectId);
391 if (ids.isEmpty())
392 return false;
393
394 const int firstChildId = ids.first();
395 const QRect oldPosition = screenRect_helper(firstChildId, false);
396
397 if (m_accessibilityContext) {
398 runInObjectContext(m_accessibilityContext, [objectId]() {
399 return scroll_helper(objectId, QAccessibleActionInterface::increaseAction());
400 }, &result);
401 }
402
403 // Don't check for position change if the call was not successful
404 return result && oldPosition != screenRect_helper(firstChildId, false);
405 }
406
407 static jboolean scrollBackward(JNIEnv */*env*/, jobject /*thiz*/, jint objectId)
408 {
409 bool result = false;
410
411 const auto& ids = childIdListForAccessibleObject_helper(objectId);
412 if (ids.isEmpty())
413 return false;
414
415 const int firstChildId = ids.first();
416 const QRect oldPosition = screenRect_helper(firstChildId, false);
417
418 if (m_accessibilityContext) {
419 runInObjectContext(m_accessibilityContext, [objectId]() {
420 return scroll_helper(objectId, QAccessibleActionInterface::decreaseAction());
421 }, &result);
422 }
423
424 // Don't check for position change if the call was not successful
425 return result && oldPosition != screenRect_helper(firstChildId, false);
426 }
427
428 static bool showOnScreen_helper(int objectId)
429 {
430 QAccessibleInterface *iface = interfaceFromId(objectId);
431 if (!iface || !iface->isValid() || !iface->actionInterface())
432 return false;
433
434 const auto actionNames = iface->actionInterface()->actionNames();
435
436 if (actionNames.contains(QAccessibleActionInterface::showOnScreenAction())) {
437 invokeActionOnInterfaceInMainThread(iface->actionInterface(), QAccessibleActionInterface::showOnScreenAction());
438 return true;
439 }
440 return false;
441 }
442
443 static jboolean showOnScreen(JNIEnv */*env*/, jobject /*thiz*/, jint objectId)
444 {
445 bool result = false;
446 if (m_accessibilityContext) {
447 runInObjectContext(m_accessibilityContext, [objectId]() {
448 return showOnScreen_helper(objectId);
449 }, &result);
450 }
451 return result;
452 }
453
454 static QString textFromValue(QAccessibleInterface *iface)
455 {
456 QString valueStr;
457 QAccessibleValueInterface *valueIface = iface->valueInterface();
458 if (valueIface) {
459 const QVariant valueVar = valueIface->currentValue();
460 const auto type = valueVar.typeId();
461 if (type == QMetaType::Double || type == QMetaType::Float) {
462 // QVariant's toString() formats floating-point values with
463 // FloatingPointShortest, which is not an accessible
464 // representation; nor, in many cases, is it suitable to the UI
465 // element whose value we're looking at. So roll our own
466 // A11Y-friendly conversion to string.
467 const double val = valueVar.toDouble();
468 // Try to use minimumStepSize() to determine precision
469 bool stepIsValid = false;
470 const double step = qAbs(valueIface->minimumStepSize().toDouble(&stepIsValid));
471 if (!stepIsValid || qFuzzyIsNull(step)) {
472 // Ignore step, use default precision
473 valueStr = qFuzzyIsNull(val) ? u"0"_s : QString::number(val, 'f');
474 } else {
475 const int precision = [](double s) {
476 int count = 0;
477 while (s < 1. && !qFuzzyCompare(s, 1.)) {
478 ++count;
479 s *= 10;
480 }
481 // If s is now 1.25, we want to show some more digits,
482 // but don't want to get silly with a step like 1./7;
483 // so only include a few extra digits.
484 const int stop = count + 3;
485 const auto fractional = [](double v) {
486 double whole = 0.0;
487 std::modf(v + 0.5, &whole);
488 return qAbs(v - whole);
489 };
490 s = fractional(s);
491 while (count < stop && !qFuzzyIsNull(s)) {
492 ++count;
493 s = fractional(s * 10);
494 }
495 return count;
496 }(step);
497 valueStr = qFuzzyIsNull(val / step) ? u"0"_s
498 : QString::number(val, 'f', precision);
499 }
500 } else {
501 valueStr = valueVar.toString();
502 }
503 }
504 return valueStr;
505 }
506
508 {
509 QAccessibleInterface *iface = interfaceFromId(objectId);
510 const QString value = textFromValue(iface);
511 QJniEnvironment env;
512 jstring jstr = env->NewString((jchar*)value.constData(), (jsize)value.size());
513 if (env.checkAndClearExceptions())
514 __android_log_print(ANDROID_LOG_WARN, m_qtTag, "Failed to create jstring");
515 return jstr;
516 }
517
518 static QString classNameForRole(QAccessible::Role role, QAccessible::State state) {
519 switch (role) {
520 case QAccessible::Role::Button:
521 case QAccessible::Role::Link:
522 {
523 if (state.checkable)
524 return QStringLiteral("android.widget.ToggleButton");
525 return QStringLiteral("android.widget.Button");
526 }
527 case QAccessible::Role::CheckBox:
528 // As of android/accessibility/utils/Role.java::getRole a CheckBox
529 // is NOT android.widget.CheckBox
530 return QStringLiteral("android.widget.CompoundButton");
531 case QAccessible::Role::Switch:
532 return QStringLiteral("android.widget.Switch");
533 case QAccessible::Role::Clock:
534 return QStringLiteral("android.widget.TextClock");
535 case QAccessible::Role::ComboBox:
536 return QStringLiteral("android.widget.Spinner");
537 case QAccessible::Role::Graphic:
538 // QQuickImage does not provide this role it inherits Client from QQuickItem
539 return QStringLiteral("android.widget.ImageView");
540 case QAccessible::Role::Grouping:
541 return QStringLiteral("android.view.ViewGroup");
542 case QAccessible::Role::List:
543 // As of android/accessibility/utils/Role.java::getRole a List
544 // is NOT android.widget.ListView
545 return QStringLiteral("android.widget.AbsListView");
546 case QAccessible::Role::MenuItem:
547 return QStringLiteral("android.view.MenuItem");
548 case QAccessible::Role::PopupMenu:
549 return QStringLiteral("android.widget.PopupMenu");
550 case QAccessible::Role::Separator:
551 return QStringLiteral("android.widget.Space");
552 case QAccessible::Role::ToolBar:
553 return QStringLiteral("android.view.Toolbar");
554 case QAccessible::Role::Heading: [[fallthrough]];
555 case QAccessible::Role::StaticText:
556 // Heading vs. regular Text is finally determined by AccessibilityNodeInfo.isHeading()
557 return QStringLiteral("android.widget.TextView");
558 case QAccessible::Role::EditableText:
559 return QStringLiteral("android.widget.EditText");
560 case QAccessible::Role::RadioButton:
561 return QStringLiteral("android.widget.RadioButton");
562 case QAccessible::Role::ProgressBar:
563 return QStringLiteral("android.widget.ProgressBar");
564 case QAccessible::Role::SpinBox:
565 return QStringLiteral("android.widget.NumberPicker");
566 case QAccessible::Role::WebDocument:
567 return QStringLiteral("android.webkit.WebView");
568 case QAccessible::Role::Dialog:
569 return QStringLiteral("android.app.AlertDialog");
570 case QAccessible::Role::PageTab:
571 return QStringLiteral("android.app.ActionBar.Tab");
572 case QAccessible::Role::PageTabList:
573 return QStringLiteral("android.widget.TabWidget");
574 case QAccessible::Role::ScrollBar:
575 return QStringLiteral("android.widget.Scroller");
576 case QAccessible::Role::Slider:
577 return QStringLiteral("com.google.android.material.slider.Slider");
578 case QAccessible::Role::Table:
579 // #TODO Evaluate the usage of AccessibleNodeInfo.setCollectionItemInfo() to provide
580 // infos about colums, rows und items.
581 return QStringLiteral("android.widget.GridView");
582 case QAccessible::Role::Pane:
583 // #TODO QQuickScrollView, QQuickListView (see QTBUG-137806)
584 return QStringLiteral("android.view.ViewGroup");
585 case QAccessible::Role::AlertMessage:
586 case QAccessible::Role::Animation:
587 case QAccessible::Role::Application:
588 case QAccessible::Role::Assistant:
589 case QAccessible::Role::BlockQuote:
590 case QAccessible::Role::Border:
591 case QAccessible::Role::ButtonDropGrid:
592 case QAccessible::Role::ButtonDropDown:
593 case QAccessible::Role::ButtonMenu:
594 case QAccessible::Role::Canvas:
595 case QAccessible::Role::Caret:
596 case QAccessible::Role::Cell:
597 case QAccessible::Role::Chart:
598 case QAccessible::Role::Client:
599 case QAccessible::Role::ColorChooser:
600 case QAccessible::Role::Column:
601 case QAccessible::Role::ColumnHeader:
602 case QAccessible::Role::ComplementaryContent:
603 case QAccessible::Role::Cursor:
604 case QAccessible::Role::Desktop:
605 case QAccessible::Role::Dial:
606 case QAccessible::Role::Document:
607 case QAccessible::Role::Equation:
608 case QAccessible::Role::Footer:
609 case QAccessible::Role::Form:
610 case QAccessible::Role::Grip:
611 case QAccessible::Role::HelpBalloon:
612 case QAccessible::Role::HotkeyField:
613 case QAccessible::Role::Indicator:
614 case QAccessible::Role::LayeredPane:
615 case QAccessible::Role::ListItem:
616 case QAccessible::Role::MenuBar:
617 case QAccessible::Role::NoRole:
618 case QAccessible::Role::Note:
619 case QAccessible::Role::Notification:
620 case QAccessible::Role::Paragraph:
621 case QAccessible::Role::PropertyPage:
622 case QAccessible::Role::Row:
623 case QAccessible::Role::RowHeader:
624 case QAccessible::Role::Section:
625 case QAccessible::Role::Sound:
626 case QAccessible::Role::Splitter:
627 case QAccessible::Role::StatusBar:
628 case QAccessible::Role::Terminal:
629 case QAccessible::Role::TitleBar:
630 case QAccessible::Role::ToolTip:
631 case QAccessible::Role::Tree:
632 case QAccessible::Role::TreeItem:
633 case QAccessible::Role::UserRole:
634 case QAccessible::Role::Whitespace:
635 case QAccessible::Role::Window:
636 // If unsure, every visible or interactive element in Android
637 // inherits android.view.View and by many extends also TextView.
638 // Android itself does a similar thing e.g. in its Settings-App.
639 return QStringLiteral("android.view.TextView");
640 }
641 }
642
643 static QString descriptionForInterface(QAccessibleInterface *iface)
644 {
645 QString desc;
646 if (iface && iface->isValid()) {
647 bool hasValue = false;
648 desc = iface->text(QAccessible::Name);
649 const QString descStr = iface->text(QAccessible::Description);
650 if (!descStr.isEmpty()) {
651 if (!desc.isEmpty())
652 desc.append(QStringLiteral(", "));
653 desc.append(descStr);
654 }
655 if (desc.isEmpty()) {
656 desc = iface->text(QAccessible::Value);
657 hasValue = !desc.isEmpty();
658 }
659 if (!hasValue && iface->valueInterface()) {
660 const QString valueStr = textFromValue(iface);
661 if (!valueStr.isEmpty()) {
662 if (!desc.isEmpty())
663 desc.append(QChar(QChar::Space));
664 desc.append(valueStr);
665 }
666 }
667 }
668 return desc;
669 }
670
671 static QString descriptionForAccessibleObject_helper(int objectId)
672 {
673 QAccessibleInterface *iface = interfaceFromId(objectId);
674 return descriptionForInterface(iface);
675 }
676
677 static jstring descriptionForAccessibleObject(JNIEnv *env, jobject /*thiz*/, jint objectId)
678 {
679 QString desc;
680 if (m_accessibilityContext) {
681 runInObjectContext(m_accessibilityContext, [objectId]() {
682 return descriptionForAccessibleObject_helper(objectId);
683 }, &desc);
684 }
685 return env->NewString((jchar*) desc.constData(), (jsize) desc.size());
686 }
687
688 static QString languageTag_helper(int objectId)
689 {
690 QAccessibleInterface *iface = interfaceFromId(objectId);
691 if (!iface || !iface->isValid())
692 return QString();
693
694 QAccessibleAttributesInterface *attributesIface = iface->attributesInterface();
695 if (!attributesIface || !attributesIface->attributeKeys().contains(QAccessible::Attribute::Locale))
696 return QString();
697
698 return attributesIface->attributeValue(QAccessible::Attribute::Locale).toLocale().bcp47Name();
699 }
700
701 static jstring languageTag(JNIEnv *env, jobject /*thiz*/, jint objectId)
702 {
703 QString tag;
704 if (m_accessibilityContext) {
705 runInObjectContext(m_accessibilityContext, [objectId]() {
706 return languageTag_helper(objectId);
707 }, &tag);
708 }
709 return env->NewString((jchar*)tag.constData(), (jsize)tag.size());
710 }
711
712 struct NodeInfo
713 {
714 bool valid = false;
718 QString description;
719 QString identifier;
720 bool hasTextSelection = false;
723 bool hasValue = false;
724 QVariant minValue = 0;
725 QVariant maxValue = 0;
726 QVariant currentValue = 0;
727 QVariant valueStepSize = 0;
728 };
729
730 static NodeInfo populateNode_helper(int objectId)
731 {
732 NodeInfo info;
733 QAccessibleInterface *iface = interfaceFromId(objectId);
734 if (iface && iface->isValid()) {
735 info.valid = true;
736 info.state = iface->state();
737 info.role = iface->role();
738 info.actions = QAccessibleBridgeUtils::effectiveActionNames(iface);
739 info.description = descriptionForInterface(iface);
740 info.identifier = QAccessibleBridgeUtils::accessibleId(iface);
741 QAccessibleTextInterface *textIface = iface->textInterface();
742 if (textIface && (textIface->selectionCount() > 0)) {
743 info.hasTextSelection = true;
744 textIface->selection(0, &info.selectionStart, &info.selectionEnd);
745 }
746 QAccessibleValueInterface *valueInterface = iface->valueInterface();
747 if (valueInterface) {
748 info.hasValue = true;
749 info.minValue = valueInterface->minimumValue();
750 info.maxValue = valueInterface->maximumValue();
751 info.currentValue = valueInterface->currentValue();
752 info.valueStepSize = valueInterface->minimumStepSize();
753 }
754 }
755 return info;
756 }
757
758 static jboolean populateNode(JNIEnv *env, jobject /*thiz*/, jint objectId, jobject node)
759 {
760 NodeInfo info;
761 if (m_accessibilityContext) {
762 runInObjectContext(m_accessibilityContext, [objectId]() {
763 return populateNode_helper(objectId);
764 }, &info);
765 }
766 if (!info.valid) {
767 __android_log_print(ANDROID_LOG_WARN, m_qtTag, "Accessibility: populateNode for Invalid ID");
768 return false;
769 }
770
771 const QString role = classNameForRole(info.role, info.state);
772 jstring jrole = env->NewString((jchar*)role.constData(), (jsize)role.size());
773 env->CallVoidMethod(node, m_setClassNameMethodID, jrole);
774
775 const bool hasClickableAction =
776 (info.actions.contains(QAccessibleActionInterface::pressAction())
777 || info.actions.contains(QAccessibleActionInterface::toggleAction()))
778 && !(info.role == QAccessible::StaticText || info.role == QAccessible::Heading);
779 const bool hasIncreaseAction =
780 info.actions.contains(QAccessibleActionInterface::increaseAction());
781 const bool hasDecreaseAction =
782 info.actions.contains(QAccessibleActionInterface::decreaseAction());
783
784 if (info.hasTextSelection && m_setTextSelectionMethodID) {
785 env->CallVoidMethod(node, m_setTextSelectionMethodID, info.selectionStart,
786 info.selectionEnd);
787 }
788
789 if (info.hasValue && m_setRangeInfoMethodID) {
790 int valueType = info.currentValue.typeId();
791 jint rangeType = RANGE_TYPE_INDETERMINATE;
792 switch (valueType) {
793 case QMetaType::Float:
794 case QMetaType::Double:
795 rangeType = RANGE_TYPE_FLOAT;
796 break;
797 case QMetaType::Int:
798 rangeType = RANGE_TYPE_INT;
799 break;
800 }
801
802 float min = info.minValue.toFloat();
803 float max = info.maxValue.toFloat();
804 float current = info.currentValue.toFloat();
805 if (info.role == QAccessible::ProgressBar) {
806 rangeType = RANGE_TYPE_PERCENT;
807 current = 100 * (current - min) / (max - min);
808 min = 0.0f;
809 max = 100.0f;
810 }
811
812 QJniObject rangeInfo("android/view/accessibility/AccessibilityNodeInfo$RangeInfo",
813 "(IFFF)V", rangeType, min, max, current);
814
815 if (rangeInfo.isValid()) {
816 env->CallVoidMethod(node, m_setRangeInfoMethodID, rangeInfo.object());
817 }
818 }
819
820 env->CallVoidMethod(node, m_setCheckableMethodID, (bool)info.state.checkable);
821 env->CallVoidMethod(node, m_setCheckedMethodID, (bool)info.state.checked);
822 env->CallVoidMethod(node, m_setEditableMethodID, info.state.editable);
823 env->CallVoidMethod(node, m_setEnabledMethodID, !info.state.disabled);
824 env->CallVoidMethod(node, m_setFocusableMethodID, (bool)info.state.focusable);
825 env->CallVoidMethod(node, m_setFocusedMethodID, (bool)info.state.focused);
827 env->CallVoidMethod(node, m_setHeadingMethodID, info.role == QAccessible::Heading);
828 env->CallVoidMethod(node, m_setVisibleToUserMethodID, !info.state.invisible);
829 env->CallVoidMethod(node, m_setScrollableMethodID, hasIncreaseAction || hasDecreaseAction);
830 env->CallVoidMethod(node, m_setClickableMethodID, hasClickableAction || info.role == QAccessible::Link);
831
832 // Add ACTION_CLICK
833 if (hasClickableAction)
834 env->CallVoidMethod(node, m_addActionMethodID, (int)0x00000010); // ACTION_CLICK defined in AccessibilityNodeInfo
835
836 // Add ACTION_SCROLL_FORWARD
837 if (hasIncreaseAction)
838 env->CallVoidMethod(node, m_addActionMethodID, (int)0x00001000); // ACTION_SCROLL_FORWARD defined in AccessibilityNodeInfo
839
840 // Add ACTION_SCROLL_BACKWARD
841 if (hasDecreaseAction)
842 env->CallVoidMethod(node, m_addActionMethodID, (int)0x00002000); // ACTION_SCROLL_BACKWARD defined in AccessibilityNodeInfo
843
844 // try to fill in the text property, this is what the screen reader reads
845 jstring jdesc = env->NewString((jchar*)info.description.constData(),
846 (jsize)info.description.size());
847 //CALL_METHOD(node, "setText", "(Ljava/lang/CharSequence;)V", jdesc)
848 env->CallVoidMethod(node, m_setContentDescriptionMethodID, jdesc);
849
850 QJniObject(node).callMethod<void>("setViewIdResourceName", info.identifier);
851
852 return true;
853 }
854
855 static const JNINativeMethod methods[] = {
856 {"setActive","(Z)V",(void*)setActive},
857 {"childIdListForAccessibleObject", "(I)[I", (jintArray)childIdListForAccessibleObject},
858 {"parentId", "(I)I", (void*)parentId},
859 {"descriptionForAccessibleObject", "(I)Ljava/lang/String;", (jstring)descriptionForAccessibleObject},
860 {"languageTag", "(I)Ljava/lang/String;", (jstring)languageTag},
861 {"screenRect", "(I)Landroid/graphics/Rect;", (jobject)screenRect},
862 {"hitTest", "(FF)I", (void*)hitTest},
863 {"populateNode", "(ILandroid/view/accessibility/AccessibilityNodeInfo;)Z", (void*)populateNode},
864 {"clickAction", "(I)Z", (void*)clickAction},
865 {"focusAction", "(I)Z", (void*)focusAction},
866 {"scrollForward", "(I)Z", (void*)scrollForward},
867 {"scrollBackward", "(I)Z", (void*)scrollBackward},
868 {"showOnScreen", "(I)Z", (void *)showOnScreen}
869 };
870
871#define GET_AND_CHECK_STATIC_METHOD(VAR, CLASS, METHOD_NAME, METHOD_SIGNATURE)
872 VAR = env->GetMethodID(CLASS, METHOD_NAME, METHOD_SIGNATURE);
873 if (!VAR) {
874 __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::methodErrorMsgFmt(), METHOD_NAME, METHOD_SIGNATURE);
875 return false;
876 }
877
878#define CHECK_AND_INIT_STATIC_FIELD(TYPE, VAR, CLASS, FIELD_NAME)
879 if (env.findStaticField<TYPE>(CLASS, FIELD_NAME) == nullptr) {
880 __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(),
881 QtAndroid::staticFieldErrorMsgFmt(), FIELD_NAME);
882 return false;
883 }
884 VAR = QJniObject::getStaticField<TYPE>(CLASS, FIELD_NAME);
885
886 bool registerNatives(QJniEnvironment &env)
887 {
888 if (!env.registerNativeMethods("org/qtproject/qt/android/QtNativeAccessibility",
889 methods, sizeof(methods) / sizeof(methods[0]))) {
890 __android_log_print(ANDROID_LOG_FATAL,"Qt A11y", "RegisterNatives failed");
891 return false;
892 }
893
894 jclass nodeInfoClass = env->FindClass("android/view/accessibility/AccessibilityNodeInfo");
895 GET_AND_CHECK_STATIC_METHOD(m_setClassNameMethodID, nodeInfoClass, "setClassName", "(Ljava/lang/CharSequence;)V");
896 GET_AND_CHECK_STATIC_METHOD(m_addActionMethodID, nodeInfoClass, "addAction", "(I)V");
897 GET_AND_CHECK_STATIC_METHOD(m_setCheckableMethodID, nodeInfoClass, "setCheckable", "(Z)V");
898 GET_AND_CHECK_STATIC_METHOD(m_setCheckedMethodID, nodeInfoClass, "setChecked", "(Z)V");
899 GET_AND_CHECK_STATIC_METHOD(m_setClickableMethodID, nodeInfoClass, "setClickable", "(Z)V");
900 GET_AND_CHECK_STATIC_METHOD(m_setContentDescriptionMethodID, nodeInfoClass, "setContentDescription", "(Ljava/lang/CharSequence;)V");
901 GET_AND_CHECK_STATIC_METHOD(m_setEditableMethodID, nodeInfoClass, "setEditable", "(Z)V");
902 GET_AND_CHECK_STATIC_METHOD(m_setEnabledMethodID, nodeInfoClass, "setEnabled", "(Z)V");
903 GET_AND_CHECK_STATIC_METHOD(m_setFocusableMethodID, nodeInfoClass, "setFocusable", "(Z)V");
904 GET_AND_CHECK_STATIC_METHOD(m_setFocusedMethodID, nodeInfoClass, "setFocused", "(Z)V");
905 if (QtAndroidPrivate::androidSdkVersion() >= 28) {
906 GET_AND_CHECK_STATIC_METHOD(m_setHeadingMethodID, nodeInfoClass, "setHeading", "(Z)V");
907 }
908 GET_AND_CHECK_STATIC_METHOD(m_setScrollableMethodID, nodeInfoClass, "setScrollable", "(Z)V");
909 GET_AND_CHECK_STATIC_METHOD(m_setVisibleToUserMethodID, nodeInfoClass, "setVisibleToUser", "(Z)V");
910 GET_AND_CHECK_STATIC_METHOD(m_setTextSelectionMethodID, nodeInfoClass, "setTextSelection", "(II)V");
912 m_setRangeInfoMethodID, nodeInfoClass, "setRangeInfo",
913 "(Landroid/view/accessibility/AccessibilityNodeInfo$RangeInfo;)V");
914
915 jclass rangeInfoClass =
916 env->FindClass("android/view/accessibility/AccessibilityNodeInfo$RangeInfo");
917 CHECK_AND_INIT_STATIC_FIELD(int, RANGE_TYPE_INT, rangeInfoClass, "RANGE_TYPE_INT");
918 CHECK_AND_INIT_STATIC_FIELD(int, RANGE_TYPE_FLOAT, rangeInfoClass, "RANGE_TYPE_FLOAT");
919 CHECK_AND_INIT_STATIC_FIELD(int, RANGE_TYPE_PERCENT, rangeInfoClass, "RANGE_TYPE_PERCENT");
920 if (QtAndroidPrivate::androidSdkVersion() >= 36) {
921 CHECK_AND_INIT_STATIC_FIELD(int, RANGE_TYPE_INDETERMINATE, rangeInfoClass,
922 "RANGE_TYPE_INDETERMINATE");
923 } else {
925 }
926
927 return true;
928 }
929}
930
931QT_END_NAMESPACE
static const char m_qtTag[]
#define GET_AND_CHECK_STATIC_METHOD(VAR, CLASS, METHOD_NAME, METHOD_SIGNATURE)
#define CHECK_AND_INIT_STATIC_FIELD(TYPE, VAR, CLASS, FIELD_NAME)
\inmodule QtCore\reentrant
Definition qpoint.h:30
static jboolean showOnScreen(JNIEnv *, jobject, jint objectId)
void notifyDescriptionOrNameChanged(uint accessibilityObjectId)
void notifyObjectShow(uint accessibilityObjectId)
static bool clickAction_helper(int objectId)
static const JNINativeMethod methods[]
void notifyLocationChange(uint accessibilityObjectId)
void runInObjectContext(QObject *context, Func &&func, Ret *retVal)
static jboolean scrollForward(JNIEnv *, jobject, jint objectId)
void notifyObjectFocus(uint accessibilityObjectId)
static jboolean scrollBackward(JNIEnv *, jobject, jint objectId)
static QString descriptionForInterface(QAccessibleInterface *iface)
static bool showOnScreen_helper(int objectId)
static int hitTest_helper(float x, float y)
static jstring descriptionForAccessibleObject(JNIEnv *env, jobject, jint objectId)
static bool scroll_helper(int objectId, const QString &actionName)
static QString classNameForRole(QAccessible::Role role, QAccessible::State state)
static jmethodID m_setContentDescriptionMethodID
static QString textFromValue(QAccessibleInterface *iface)
void createAccessibilityContextObject(QObject *parent)
static QVarLengthArray< int, 8 > childIdListForAccessibleObject_helper(int objectId)
static NodeInfo populateNode_helper(int objectId)
void notifyObjectHide(uint accessibilityObjectId)
static jboolean focusAction(JNIEnv *, jobject, jint objectId)
static QString languageTag_helper(int objectId)
static jmethodID m_setVisibleToUserMethodID
static jint hitTest(JNIEnv *, jobject, jfloat x, jfloat y)
static jstring languageTag(JNIEnv *env, jobject, jint objectId)
static bool focusAction_helper(int objectId)
static jboolean clickAction(JNIEnv *, jobject, jint objectId)
static jstring jvalueForAccessibleObject(int objectId)
static void setActive(JNIEnv *, jobject, jboolean active)
static void invokeActionOnInterfaceInMainThread(QAccessibleActionInterface *actionInterface, const QString &action)
static jmethodID m_setTextSelectionMethodID
bool registerNatives(QJniEnvironment &env)
QAccessibleInterface * interfaceFromId(jint objectId)
void notifyValueChanged(uint accessibilityObjectId)
void notifyAnnouncementEvent(uint accessibilityObjectId, const QString &message)
static int parentId_helper(int objectId)
static QRect screenRect_helper(int objectId, bool clip=true)
static QString descriptionForAccessibleObject_helper(int objectId)
static jint parentId(JNIEnv *, jobject, jint objectId)
static jobject screenRect(JNIEnv *env, jobject, jint objectId)
static jintArray childIdListForAccessibleObject(JNIEnv *env, jobject, jint objectId)
static jboolean populateNode(JNIEnv *env, jobject, jint objectId, jobject node)
void notifyScrolledEvent(uint accessiblityObjectId)
QBasicMutex * platformInterfaceMutex()
QAndroidPlatformIntegration * androidPlatformIntegration()
#define qApp