1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/accessibility/browser_accessibility_manager_android.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/i18n/char_iterator.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "content/browser/accessibility/browser_accessibility_android.h"
16 #include "content/common/accessibility_messages.h"
17 #include "jni/BrowserAccessibilityManager_jni.h"
18 #include "ui/accessibility/ax_text_utils.h"
20 using base::android::AttachCurrentThread
;
21 using base::android::ScopedJavaLocalRef
;
25 enum AndroidHtmlElementType
{
26 HTML_ELEMENT_TYPE_SECTION
,
27 HTML_ELEMENT_TYPE_LIST
,
28 HTML_ELEMENT_TYPE_CONTROL
,
32 // These are special unofficial strings sent from TalkBack/BrailleBack
33 // to jump to certain categories of web elements.
34 AndroidHtmlElementType
HtmlElementTypeFromString(base::string16 element_type
) {
35 if (element_type
== base::ASCIIToUTF16("SECTION"))
36 return HTML_ELEMENT_TYPE_SECTION
;
37 else if (element_type
== base::ASCIIToUTF16("LIST"))
38 return HTML_ELEMENT_TYPE_LIST
;
39 else if (element_type
== base::ASCIIToUTF16("CONTROL"))
40 return HTML_ELEMENT_TYPE_CONTROL
;
42 return HTML_ELEMENT_TYPE_ANY
;
45 } // anonymous namespace
49 namespace aria_strings
{
50 const char kAriaLivePolite
[] = "polite";
51 const char kAriaLiveAssertive
[] = "assertive";
55 BrowserAccessibilityManager
* BrowserAccessibilityManager::Create(
56 const SimpleAXTreeUpdate
& initial_tree
,
57 BrowserAccessibilityDelegate
* delegate
,
58 BrowserAccessibilityFactory
* factory
) {
59 return new BrowserAccessibilityManagerAndroid(
60 ScopedJavaLocalRef
<jobject
>(), initial_tree
, delegate
, factory
);
63 BrowserAccessibilityManagerAndroid
*
64 BrowserAccessibilityManager::ToBrowserAccessibilityManagerAndroid() {
65 return static_cast<BrowserAccessibilityManagerAndroid
*>(this);
68 BrowserAccessibilityManagerAndroid::BrowserAccessibilityManagerAndroid(
69 ScopedJavaLocalRef
<jobject
> content_view_core
,
70 const SimpleAXTreeUpdate
& initial_tree
,
71 BrowserAccessibilityDelegate
* delegate
,
72 BrowserAccessibilityFactory
* factory
)
73 : BrowserAccessibilityManager(delegate
, factory
),
74 prune_tree_for_screen_reader_(true) {
75 Initialize(initial_tree
);
76 SetContentViewCore(content_view_core
);
79 BrowserAccessibilityManagerAndroid::~BrowserAccessibilityManagerAndroid() {
80 JNIEnv
* env
= AttachCurrentThread();
81 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
85 Java_BrowserAccessibilityManager_onNativeObjectDestroyed(env
, obj
.obj());
90 BrowserAccessibilityManagerAndroid::GetEmptyDocument() {
91 ui::AXNodeData empty_document
;
92 empty_document
.id
= 0;
93 empty_document
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
94 empty_document
.state
= 1 << ui::AX_STATE_READ_ONLY
;
96 SimpleAXTreeUpdate update
;
97 update
.nodes
.push_back(empty_document
);
101 void BrowserAccessibilityManagerAndroid::SetContentViewCore(
102 ScopedJavaLocalRef
<jobject
> content_view_core
) {
103 if (content_view_core
.is_null())
106 JNIEnv
* env
= AttachCurrentThread();
107 java_ref_
= JavaObjectWeakGlobalRef(
108 env
, Java_BrowserAccessibilityManager_create(
109 env
, reinterpret_cast<intptr_t>(this),
110 content_view_core
.obj()).obj());
113 void BrowserAccessibilityManagerAndroid::NotifyAccessibilityEvent(
114 ui::AXEvent event_type
,
115 BrowserAccessibility
* node
) {
116 JNIEnv
* env
= AttachCurrentThread();
117 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
121 BrowserAccessibilityAndroid
* android_node
=
122 static_cast<BrowserAccessibilityAndroid
*>(node
);
124 if (event_type
== ui::AX_EVENT_HIDE
)
127 if (event_type
== ui::AX_EVENT_TREE_CHANGED
)
130 if (event_type
== ui::AX_EVENT_HOVER
) {
131 HandleHoverEvent(node
);
135 // Always send AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED to notify
136 // the Android system that the accessibility hierarchy rooted at this
138 Java_BrowserAccessibilityManager_handleContentChanged(
139 env
, obj
.obj(), node
->GetId());
141 switch (event_type
) {
142 case ui::AX_EVENT_LOAD_COMPLETE
:
143 Java_BrowserAccessibilityManager_handlePageLoaded(
144 env
, obj
.obj(), focus_
->id());
146 case ui::AX_EVENT_FOCUS
:
147 Java_BrowserAccessibilityManager_handleFocusChanged(
148 env
, obj
.obj(), node
->GetId());
150 case ui::AX_EVENT_CHECKED_STATE_CHANGED
:
151 Java_BrowserAccessibilityManager_handleCheckStateChanged(
152 env
, obj
.obj(), node
->GetId());
154 case ui::AX_EVENT_SCROLL_POSITION_CHANGED
:
155 Java_BrowserAccessibilityManager_handleScrollPositionChanged(
156 env
, obj
.obj(), node
->GetId());
158 case ui::AX_EVENT_SCROLLED_TO_ANCHOR
:
159 Java_BrowserAccessibilityManager_handleScrolledToAnchor(
160 env
, obj
.obj(), node
->GetId());
162 case ui::AX_EVENT_ALERT
:
163 // An alert is a special case of live region. Fall through to the
164 // next case to handle it.
165 case ui::AX_EVENT_SHOW
: {
166 // This event is fired when an object appears in a live region.
168 Java_BrowserAccessibilityManager_announceLiveRegionText(
170 base::android::ConvertUTF16ToJavaString(
171 env
, android_node
->GetText()).obj());
174 case ui::AX_EVENT_TEXT_SELECTION_CHANGED
:
175 Java_BrowserAccessibilityManager_handleTextSelectionChanged(
176 env
, obj
.obj(), node
->GetId());
178 case ui::AX_EVENT_TEXT_CHANGED
:
179 case ui::AX_EVENT_VALUE_CHANGED
:
180 if (node
->IsEditableText() && GetFocus(GetRoot()) == node
) {
181 Java_BrowserAccessibilityManager_handleEditableTextChanged(
182 env
, obj
.obj(), node
->GetId());
183 } else if (android_node
->IsSlider()) {
184 Java_BrowserAccessibilityManager_handleSliderChanged(
185 env
, obj
.obj(), node
->GetId());
189 // There are some notifications that aren't meaningful on Android.
190 // It's okay to skip them.
195 jint
BrowserAccessibilityManagerAndroid::GetRootId(JNIEnv
* env
, jobject obj
) {
197 return static_cast<jint
>(GetRoot()->GetId());
202 jboolean
BrowserAccessibilityManagerAndroid::IsNodeValid(
203 JNIEnv
* env
, jobject obj
, jint id
) {
204 return GetFromID(id
) != NULL
;
207 void BrowserAccessibilityManagerAndroid::HitTest(
208 JNIEnv
* env
, jobject obj
, jint x
, jint y
) {
210 delegate()->AccessibilityHitTest(gfx::Point(x
, y
));
213 jboolean
BrowserAccessibilityManagerAndroid::IsEditableText(
214 JNIEnv
* env
, jobject obj
, jint id
) {
215 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
220 return node
->IsEditableText();
223 jint
BrowserAccessibilityManagerAndroid::GetEditableTextSelectionStart(
224 JNIEnv
* env
, jobject obj
, jint id
) {
225 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
230 return node
->GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START
);
233 jint
BrowserAccessibilityManagerAndroid::GetEditableTextSelectionEnd(
234 JNIEnv
* env
, jobject obj
, jint id
) {
235 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
240 return node
->GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END
);
243 jboolean
BrowserAccessibilityManagerAndroid::PopulateAccessibilityNodeInfo(
244 JNIEnv
* env
, jobject obj
, jobject info
, jint id
) {
245 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
250 if (node
->GetParent()) {
251 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoParent(
252 env
, obj
, info
, node
->GetParent()->GetId());
254 for (unsigned i
= 0; i
< node
->PlatformChildCount(); ++i
) {
255 Java_BrowserAccessibilityManager_addAccessibilityNodeInfoChild(
256 env
, obj
, info
, node
->InternalGetChild(i
)->GetId());
258 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoBooleanAttributes(
268 node
->IsScrollable(),
270 node
->IsVisibleToUser());
271 Java_BrowserAccessibilityManager_addAccessibilityNodeInfoActions(
274 node
->CanScrollForward(),
275 node
->CanScrollBackward(),
277 node
->CanScrollDown(),
278 node
->CanScrollLeft(),
279 node
->CanScrollRight(),
281 node
->IsEditableText(),
285 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoClassName(
287 base::android::ConvertUTF8ToJavaString(env
, node
->GetClassName()).obj());
288 if (!node
->IsPassword() ||
289 Java_BrowserAccessibilityManager_shouldExposePasswordText(env
, obj
)) {
290 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoContentDescription(
292 base::android::ConvertUTF16ToJavaString(env
, node
->GetText()).obj(),
295 base::string16 element_id
;
296 if (node
->GetHtmlAttribute("id", &element_id
)) {
297 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoViewIdResourceName(
299 base::android::ConvertUTF16ToJavaString(env
, element_id
).obj());
302 gfx::Rect absolute_rect
= node
->GetLocalBoundsRect();
303 gfx::Rect parent_relative_rect
= absolute_rect
;
304 if (node
->GetParent()) {
305 gfx::Rect parent_rect
= node
->GetParent()->GetLocalBoundsRect();
306 parent_relative_rect
.Offset(-parent_rect
.OffsetFromOrigin());
308 bool is_root
= node
->GetParent() == NULL
;
309 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoLocation(
312 absolute_rect
.x(), absolute_rect
.y(),
313 parent_relative_rect
.x(), parent_relative_rect
.y(),
314 absolute_rect
.width(), absolute_rect
.height(),
317 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoLollipopAttributes(
319 node
->CanOpenPopup(),
320 node
->IsContentInvalid(),
321 node
->IsDismissable(),
323 node
->AndroidInputType(),
324 node
->AndroidLiveRegionType());
325 if (node
->IsCollection()) {
326 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionInfo(
330 node
->IsHierarchical());
332 if (node
->IsCollectionItem() || node
->IsHeading()) {
333 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionItemInfo(
341 if (node
->IsRangeType()) {
342 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoRangeInfo(
344 node
->AndroidRangeType(),
347 node
->RangeCurrentValue());
353 jboolean
BrowserAccessibilityManagerAndroid::PopulateAccessibilityEvent(
354 JNIEnv
* env
, jobject obj
, jobject event
, jint id
, jint event_type
) {
355 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
360 Java_BrowserAccessibilityManager_setAccessibilityEventBooleanAttributes(
365 node
->IsScrollable());
366 Java_BrowserAccessibilityManager_setAccessibilityEventClassName(
368 base::android::ConvertUTF8ToJavaString(env
, node
->GetClassName()).obj());
369 Java_BrowserAccessibilityManager_setAccessibilityEventListAttributes(
371 node
->GetItemIndex(),
372 node
->GetItemCount());
373 Java_BrowserAccessibilityManager_setAccessibilityEventScrollAttributes(
377 node
->GetMaxScrollX(),
378 node
->GetMaxScrollY());
380 switch (event_type
) {
381 case ANDROID_ACCESSIBILITY_EVENT_TEXT_CHANGED
: {
382 base::string16 before_text
, text
;
383 if (!node
->IsPassword() ||
384 Java_BrowserAccessibilityManager_shouldExposePasswordText(env
, obj
)) {
385 before_text
= node
->GetTextChangeBeforeText();
386 text
= node
->GetText();
388 Java_BrowserAccessibilityManager_setAccessibilityEventTextChangedAttrs(
390 node
->GetTextChangeFromIndex(),
391 node
->GetTextChangeAddedCount(),
392 node
->GetTextChangeRemovedCount(),
393 base::android::ConvertUTF16ToJavaString(
394 env
, before_text
).obj(),
395 base::android::ConvertUTF16ToJavaString(env
, text
).obj());
398 case ANDROID_ACCESSIBILITY_EVENT_TEXT_SELECTION_CHANGED
: {
400 if (!node
->IsPassword() ||
401 Java_BrowserAccessibilityManager_shouldExposePasswordText(env
, obj
)) {
402 text
= node
->GetText();
404 Java_BrowserAccessibilityManager_setAccessibilityEventSelectionAttrs(
406 node
->GetSelectionStart(),
407 node
->GetSelectionEnd(),
408 node
->GetEditableTextLength(),
409 base::android::ConvertUTF16ToJavaString(env
, text
).obj());
416 Java_BrowserAccessibilityManager_setAccessibilityEventLollipopAttributes(
418 node
->CanOpenPopup(),
419 node
->IsContentInvalid(),
420 node
->IsDismissable(),
422 node
->AndroidInputType(),
423 node
->AndroidLiveRegionType());
424 if (node
->IsCollection()) {
425 Java_BrowserAccessibilityManager_setAccessibilityEventCollectionInfo(
429 node
->IsHierarchical());
431 if (node
->IsHeading()) {
432 Java_BrowserAccessibilityManager_setAccessibilityEventHeadingFlag(
433 env
, obj
, event
, true);
435 if (node
->IsCollectionItem()) {
436 Java_BrowserAccessibilityManager_setAccessibilityEventCollectionItemInfo(
443 if (node
->IsRangeType()) {
444 Java_BrowserAccessibilityManager_setAccessibilityEventRangeInfo(
446 node
->AndroidRangeType(),
449 node
->RangeCurrentValue());
455 void BrowserAccessibilityManagerAndroid::Click(
456 JNIEnv
* env
, jobject obj
, jint id
) {
457 BrowserAccessibility
* node
= GetFromID(id
);
459 DoDefaultAction(*node
);
462 void BrowserAccessibilityManagerAndroid::Focus(
463 JNIEnv
* env
, jobject obj
, jint id
) {
464 BrowserAccessibility
* node
= GetFromID(id
);
466 SetFocus(node
, true);
469 void BrowserAccessibilityManagerAndroid::Blur(JNIEnv
* env
, jobject obj
) {
470 SetFocus(GetRoot(), true);
473 void BrowserAccessibilityManagerAndroid::ScrollToMakeNodeVisible(
474 JNIEnv
* env
, jobject obj
, jint id
) {
475 BrowserAccessibility
* node
= GetFromID(id
);
477 ScrollToMakeVisible(*node
, gfx::Rect(node
->GetLocation().size()));
480 void BrowserAccessibilityManagerAndroid::SetTextFieldValue(
481 JNIEnv
* env
, jobject obj
, jint id
, jstring value
) {
482 BrowserAccessibility
* node
= GetFromID(id
);
484 BrowserAccessibilityManager::SetValue(
485 *node
, base::android::ConvertJavaStringToUTF16(env
, value
));
489 void BrowserAccessibilityManagerAndroid::SetSelection(
490 JNIEnv
* env
, jobject obj
, jint id
, jint start
, jint end
) {
491 BrowserAccessibility
* node
= GetFromID(id
);
493 SetTextSelection(*node
, start
, end
);
496 jboolean
BrowserAccessibilityManagerAndroid::AdjustSlider(
497 JNIEnv
* env
, jobject obj
, jint id
, jboolean increment
) {
498 BrowserAccessibility
* node
= GetFromID(id
);
502 BrowserAccessibilityAndroid
* android_node
=
503 static_cast<BrowserAccessibilityAndroid
*>(node
);
505 if (!android_node
->IsSlider() || !android_node
->IsEnabled())
508 float value
= node
->GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
509 float min
= node
->GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
510 float max
= node
->GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
514 // To behave similarly to an Android SeekBar, move by an increment of
515 // approximately 20%.
516 float original_value
= value
;
517 float delta
= (max
- min
) / 5.0f
;
518 value
+= (increment
? delta
: -delta
);
519 value
= std::max(std::min(value
, max
), min
);
520 if (value
!= original_value
) {
521 BrowserAccessibilityManager::SetValue(
522 *node
, base::UTF8ToUTF16(base::DoubleToString(value
)));
528 void BrowserAccessibilityManagerAndroid::HandleHoverEvent(
529 BrowserAccessibility
* node
) {
530 JNIEnv
* env
= AttachCurrentThread();
531 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
535 BrowserAccessibilityAndroid
* ancestor
=
536 static_cast<BrowserAccessibilityAndroid
*>(node
->GetParent());
537 while (ancestor
&& ancestor
!= GetRoot()) {
538 if (ancestor
->PlatformIsLeaf() ||
539 (ancestor
->IsFocusable() && !ancestor
->HasFocusableChild())) {
541 // Don't break - we want the highest ancestor that's focusable or a
544 ancestor
= static_cast<BrowserAccessibilityAndroid
*>(ancestor
->GetParent());
547 Java_BrowserAccessibilityManager_handleHover(
548 env
, obj
.obj(), node
->GetId());
551 jint
BrowserAccessibilityManagerAndroid::FindElementType(
552 JNIEnv
* env
, jobject obj
, jint start_id
, jstring element_type_str
,
554 BrowserAccessibility
* node
= GetFromID(start_id
);
558 AndroidHtmlElementType element_type
= HtmlElementTypeFromString(
559 base::android::ConvertJavaStringToUTF16(env
, element_type_str
));
561 node
= forwards
? NextInTreeOrder(node
) : PreviousInTreeOrder(node
);
563 switch(element_type
) {
564 case HTML_ELEMENT_TYPE_SECTION
:
565 if (node
->GetRole() == ui::AX_ROLE_ARTICLE
||
566 node
->GetRole() == ui::AX_ROLE_APPLICATION
||
567 node
->GetRole() == ui::AX_ROLE_BANNER
||
568 node
->GetRole() == ui::AX_ROLE_COMPLEMENTARY
||
569 node
->GetRole() == ui::AX_ROLE_CONTENT_INFO
||
570 node
->GetRole() == ui::AX_ROLE_HEADING
||
571 node
->GetRole() == ui::AX_ROLE_MAIN
||
572 node
->GetRole() == ui::AX_ROLE_NAVIGATION
||
573 node
->GetRole() == ui::AX_ROLE_SEARCH
||
574 node
->GetRole() == ui::AX_ROLE_REGION
) {
575 return node
->GetId();
578 case HTML_ELEMENT_TYPE_LIST
:
579 if (node
->GetRole() == ui::AX_ROLE_LIST
||
580 node
->GetRole() == ui::AX_ROLE_GRID
||
581 node
->GetRole() == ui::AX_ROLE_TABLE
||
582 node
->GetRole() == ui::AX_ROLE_TREE
) {
583 return node
->GetId();
586 case HTML_ELEMENT_TYPE_CONTROL
:
587 if (static_cast<BrowserAccessibilityAndroid
*>(node
)->IsFocusable())
588 return node
->GetId();
590 case HTML_ELEMENT_TYPE_ANY
:
591 // In theory, the API says that an accessibility service could
592 // jump to an element by element name, like 'H1' or 'P'. This isn't
593 // currently used by any accessibility service, and we think it's
594 // better to keep them high-level like 'SECTION' or 'CONTROL', so we
595 // just fall back on linear navigation when we don't recognize the
597 if (static_cast<BrowserAccessibilityAndroid
*>(node
)->IsClickable())
598 return node
->GetId();
602 node
= forwards
? NextInTreeOrder(node
) : PreviousInTreeOrder(node
);
608 jboolean
BrowserAccessibilityManagerAndroid::NextAtGranularity(
609 JNIEnv
* env
, jobject obj
, jint granularity
, jboolean extend_selection
,
610 jint id
, jint cursor_index
) {
611 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
616 jint start_index
= -1;
618 if (NextAtGranularity(granularity
, cursor_index
, node
,
619 &start_index
, &end_index
)) {
621 if (!node
->IsPassword() ||
622 Java_BrowserAccessibilityManager_shouldExposePasswordText(env
, obj
)) {
623 text
= node
->GetText();
625 Java_BrowserAccessibilityManager_finishGranularityMove(
626 env
, obj
, base::android::ConvertUTF16ToJavaString(
628 extend_selection
, start_index
, end_index
, true);
634 jboolean
BrowserAccessibilityManagerAndroid::PreviousAtGranularity(
635 JNIEnv
* env
, jobject obj
, jint granularity
, jboolean extend_selection
,
636 jint id
, jint cursor_index
) {
637 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
642 jint start_index
= -1;
644 if (PreviousAtGranularity(granularity
, cursor_index
, node
,
645 &start_index
, &end_index
)) {
646 Java_BrowserAccessibilityManager_finishGranularityMove(
647 env
, obj
, base::android::ConvertUTF16ToJavaString(
648 env
, node
->GetText()).obj(),
649 extend_selection
, start_index
, end_index
, false);
655 bool BrowserAccessibilityManagerAndroid::NextAtGranularity(
656 int32 granularity
, int32 cursor_index
,
657 BrowserAccessibilityAndroid
* node
, int32
* start_index
, int32
* end_index
) {
658 switch (granularity
) {
659 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_CHARACTER
: {
660 base::string16 text
= node
->GetText();
661 if (cursor_index
>= static_cast<int32
>(text
.length()))
663 base::i18n::UTF16CharIterator
iter(text
.data(), text
.size());
664 while (!iter
.end() && iter
.array_pos() <= cursor_index
)
666 *start_index
= iter
.array_pos();
667 *end_index
= iter
.array_pos();
670 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD
:
671 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE
: {
672 std::vector
<int32
> starts
;
673 std::vector
<int32
> ends
;
674 node
->GetGranularityBoundaries(granularity
, &starts
, &ends
, 0);
675 if (starts
.size() == 0)
679 while (index
< starts
.size() - 1 && starts
[index
] < cursor_index
)
682 if (starts
[index
] < cursor_index
)
685 *start_index
= starts
[index
];
686 *end_index
= ends
[index
];
696 bool BrowserAccessibilityManagerAndroid::PreviousAtGranularity(
697 int32 granularity
, int32 cursor_index
,
698 BrowserAccessibilityAndroid
* node
, int32
* start_index
, int32
* end_index
) {
699 switch (granularity
) {
700 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_CHARACTER
: {
701 if (cursor_index
<= 0)
703 base::string16 text
= node
->GetText();
704 base::i18n::UTF16CharIterator
iter(text
.data(), text
.size());
705 int previous_index
= 0;
706 while (!iter
.end() && iter
.array_pos() < cursor_index
) {
707 previous_index
= iter
.array_pos();
710 *start_index
= previous_index
;
711 *end_index
= previous_index
;
714 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD
:
715 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE
: {
716 std::vector
<int32
> starts
;
717 std::vector
<int32
> ends
;
718 node
->GetGranularityBoundaries(granularity
, &starts
, &ends
, 0);
719 if (starts
.size() == 0)
722 size_t index
= starts
.size() - 1;
723 while (index
> 0 && starts
[index
] >= cursor_index
)
726 if (starts
[index
] >= cursor_index
)
729 *start_index
= starts
[index
];
730 *end_index
= ends
[index
];
740 void BrowserAccessibilityManagerAndroid::SetAccessibilityFocus(
741 JNIEnv
* env
, jobject obj
, jint id
) {
743 delegate_
->AccessibilitySetAccessibilityFocus(id
);
746 bool BrowserAccessibilityManagerAndroid::IsSlider(
747 JNIEnv
* env
, jobject obj
, jint id
) {
748 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
753 return node
->GetRole() == ui::AX_ROLE_SLIDER
;
756 bool BrowserAccessibilityManagerAndroid::Scroll(
757 JNIEnv
* env
, jobject obj
, jint id
, int direction
) {
758 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
763 return node
->Scroll(direction
);
766 void BrowserAccessibilityManagerAndroid::OnAtomicUpdateFinished(
769 const std::vector
<ui::AXTreeDelegate::Change
>& changes
) {
771 JNIEnv
* env
= AttachCurrentThread();
772 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
776 Java_BrowserAccessibilityManager_handleNavigate(env
, obj
.obj());
781 BrowserAccessibilityManagerAndroid::UseRootScrollOffsetsWhenComputingBounds() {
782 // The Java layer handles the root scroll offset.
786 bool RegisterBrowserAccessibilityManager(JNIEnv
* env
) {
787 return RegisterNativesImpl(env
);
790 } // namespace content