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_android.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/browser/accessibility/browser_accessibility_manager_android.h"
9 #include "content/common/accessibility_messages.h"
13 // These are enums from android.text.InputType in Java:
15 ANDROID_TEXT_INPUTTYPE_TYPE_NULL
= 0,
16 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
= 0x4,
17 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
= 0x14,
18 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME
= 0x24,
19 ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER
= 0x2,
20 ANDROID_TEXT_INPUTTYPE_TYPE_PHONE
= 0x3,
21 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
= 0x1,
22 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI
= 0x11,
23 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EDIT_TEXT
= 0xa1,
24 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL
= 0xd1,
25 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD
= 0xe1
28 // These are enums from android.view.View in Java:
30 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE
= 0,
31 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE
= 1,
32 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE
= 2
35 // These are enums from
36 // android.view.accessibility.AccessibilityNodeInfo.RangeInfo in Java:
38 ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT
= 1
46 BrowserAccessibility
* BrowserAccessibility::Create() {
47 return new BrowserAccessibilityAndroid();
50 BrowserAccessibilityAndroid::BrowserAccessibilityAndroid() {
54 bool BrowserAccessibilityAndroid::IsNative() const {
58 void BrowserAccessibilityAndroid::OnLocationChanged() {
59 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_LOCATION_CHANGED
, this);
62 bool BrowserAccessibilityAndroid::PlatformIsLeaf() const {
63 if (InternalChildCount() == 0)
66 // Iframes are always allowed to contain children.
68 GetRole() == ui::AX_ROLE_ROOT_WEB_AREA
||
69 GetRole() == ui::AX_ROLE_WEB_AREA
) {
73 // If it has a focusable child, we definitely can't leave out children.
74 if (HasFocusableChild())
77 // Headings with text can drop their children.
78 base::string16 name
= GetText();
79 if (GetRole() == ui::AX_ROLE_HEADING
&& !name
.empty())
82 // Focusable nodes with text can drop their children.
83 if (HasState(ui::AX_STATE_FOCUSABLE
) && !name
.empty())
86 // Nodes with only static text as children can drop their children.
87 if (HasOnlyStaticTextChildren())
90 return BrowserAccessibility::PlatformIsLeaf();
93 bool BrowserAccessibilityAndroid::IsCheckable() const {
94 bool checkable
= false;
95 bool is_aria_pressed_defined
;
97 GetAriaTristate("aria-pressed", &is_aria_pressed_defined
, &is_mixed
);
98 if (GetRole() == ui::AX_ROLE_CHECK_BOX
||
99 GetRole() == ui::AX_ROLE_RADIO_BUTTON
||
100 is_aria_pressed_defined
) {
103 if (HasState(ui::AX_STATE_CHECKED
))
108 bool BrowserAccessibilityAndroid::IsChecked() const {
109 return HasState(ui::AX_STATE_CHECKED
);
112 bool BrowserAccessibilityAndroid::IsClickable() const {
113 return (PlatformIsLeaf() && !GetText().empty());
116 bool BrowserAccessibilityAndroid::IsCollection() const {
117 return (GetRole() == ui::AX_ROLE_GRID
||
118 GetRole() == ui::AX_ROLE_LIST
||
119 GetRole() == ui::AX_ROLE_LIST_BOX
||
120 GetRole() == ui::AX_ROLE_TABLE
||
121 GetRole() == ui::AX_ROLE_TREE
);
124 bool BrowserAccessibilityAndroid::IsCollectionItem() const {
125 return (GetRole() == ui::AX_ROLE_CELL
||
126 GetRole() == ui::AX_ROLE_COLUMN_HEADER
||
127 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST_TERM
||
128 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION
||
129 GetRole() == ui::AX_ROLE_LIST_ITEM
||
130 GetRole() == ui::AX_ROLE_ROW_HEADER
||
131 GetRole() == ui::AX_ROLE_TREE_ITEM
);
134 bool BrowserAccessibilityAndroid::IsContentInvalid() const {
136 return GetHtmlAttribute("aria-invalid", &invalid
);
139 bool BrowserAccessibilityAndroid::IsDismissable() const {
140 return false; // No concept of "dismissable" on the web currently.
143 bool BrowserAccessibilityAndroid::IsEnabled() const {
144 return HasState(ui::AX_STATE_ENABLED
);
147 bool BrowserAccessibilityAndroid::IsFocusable() const {
148 bool focusable
= HasState(ui::AX_STATE_FOCUSABLE
);
150 GetRole() == ui::AX_ROLE_WEB_AREA
) {
156 bool BrowserAccessibilityAndroid::IsFocused() const {
157 return manager()->GetFocus(manager()->GetRoot()) == this;
160 bool BrowserAccessibilityAndroid::IsHeading() const {
161 return (GetRole() == ui::AX_ROLE_COLUMN_HEADER
||
162 GetRole() == ui::AX_ROLE_HEADING
||
163 GetRole() == ui::AX_ROLE_ROW_HEADER
);
166 bool BrowserAccessibilityAndroid::IsHierarchical() const {
167 return (GetRole() == ui::AX_ROLE_LIST
||
168 GetRole() == ui::AX_ROLE_TREE
);
171 bool BrowserAccessibilityAndroid::IsLink() const {
172 return GetRole() == ui::AX_ROLE_LINK
||
173 GetRole() == ui::AX_ROLE_IMAGE_MAP_LINK
;
176 bool BrowserAccessibilityAndroid::IsMultiLine() const {
177 return GetRole() == ui::AX_ROLE_TEXT_AREA
;
180 bool BrowserAccessibilityAndroid::IsPassword() const {
181 return HasState(ui::AX_STATE_PROTECTED
);
184 bool BrowserAccessibilityAndroid::IsRangeType() const {
185 return (GetRole() == ui::AX_ROLE_PROGRESS_INDICATOR
||
186 GetRole() == ui::AX_ROLE_SCROLL_BAR
||
187 GetRole() == ui::AX_ROLE_SLIDER
);
190 bool BrowserAccessibilityAndroid::IsScrollable() const {
192 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
, &dummy
);
195 bool BrowserAccessibilityAndroid::IsSelected() const {
196 return HasState(ui::AX_STATE_SELECTED
);
199 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
200 return !HasState(ui::AX_STATE_INVISIBLE
);
203 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
204 return HasState(ui::AX_STATE_HASPOPUP
);
207 const char* BrowserAccessibilityAndroid::GetClassName() const {
208 const char* class_name
= NULL
;
211 case ui::AX_ROLE_EDITABLE_TEXT
:
212 case ui::AX_ROLE_SPIN_BUTTON
:
213 case ui::AX_ROLE_TEXT_AREA
:
214 case ui::AX_ROLE_TEXT_FIELD
:
215 class_name
= "android.widget.EditText";
217 case ui::AX_ROLE_SLIDER
:
218 class_name
= "android.widget.SeekBar";
220 case ui::AX_ROLE_COMBO_BOX
:
221 class_name
= "android.widget.Spinner";
223 case ui::AX_ROLE_BUTTON
:
224 case ui::AX_ROLE_MENU_BUTTON
:
225 case ui::AX_ROLE_POP_UP_BUTTON
:
226 class_name
= "android.widget.Button";
228 case ui::AX_ROLE_CHECK_BOX
:
229 class_name
= "android.widget.CheckBox";
231 case ui::AX_ROLE_RADIO_BUTTON
:
232 class_name
= "android.widget.RadioButton";
234 case ui::AX_ROLE_TOGGLE_BUTTON
:
235 class_name
= "android.widget.ToggleButton";
237 case ui::AX_ROLE_CANVAS
:
238 case ui::AX_ROLE_IMAGE
:
239 class_name
= "android.widget.Image";
241 case ui::AX_ROLE_PROGRESS_INDICATOR
:
242 class_name
= "android.widget.ProgressBar";
244 case ui::AX_ROLE_TAB_LIST
:
245 class_name
= "android.widget.TabWidget";
247 case ui::AX_ROLE_GRID
:
248 case ui::AX_ROLE_TABLE
:
249 class_name
= "android.widget.GridView";
251 case ui::AX_ROLE_LIST
:
252 case ui::AX_ROLE_LIST_BOX
:
253 class_name
= "android.widget.ListView";
255 case ui::AX_ROLE_DIALOG
:
256 class_name
= "android.app.Dialog";
258 case ui::AX_ROLE_ROOT_WEB_AREA
:
259 class_name
= "android.webkit.WebView";
262 class_name
= "android.view.View";
269 base::string16
BrowserAccessibilityAndroid::GetText() const {
271 GetRole() == ui::AX_ROLE_WEB_AREA
) {
272 return base::string16();
275 // See comment in browser_accessibility_win.cc for details.
276 // The difference here is that we can only expose one accessible
277 // name on Android, not 2 or 3 like on Windows or Mac.
279 // The basic rule is: prefer description (aria-labelledby or aria-label),
280 // then help (title), then name (inner text), then value (control value).
281 // However, if title_elem_id is set, that means there's a label element
282 // supplying the name and then name takes precedence over help.
283 // TODO(dmazzoni): clean this up by providing more granular labels in
284 // Blink, making the platform-specific mapping to accessible text simpler.
285 base::string16 description
= GetString16Attribute(ui::AX_ATTR_DESCRIPTION
);
286 base::string16 help
= GetString16Attribute(ui::AX_ATTR_HELP
);
287 int title_elem_id
= GetIntAttribute(
288 ui::AX_ATTR_TITLE_UI_ELEMENT
);
290 if (!description
.empty())
292 else if (title_elem_id
&& !name().empty())
293 text
= base::UTF8ToUTF16(name());
294 else if (!help
.empty())
296 else if (!name().empty())
297 text
= base::UTF8ToUTF16(name());
298 else if (!value().empty())
299 text
= base::UTF8ToUTF16(value());
301 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
303 if (text
.empty() && HasOnlyStaticTextChildren()) {
304 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
305 BrowserAccessibility
* child
= InternalGetChild(i
);
306 text
+= static_cast<BrowserAccessibilityAndroid
*>(child
)->GetText();
311 case ui::AX_ROLE_HEADING
:
312 // Only append "heading" if this node already has text.
314 text
+= base::ASCIIToUTF16(" Heading");
318 if (text
.empty() && IsLink()) {
319 base::string16 url
= GetString16Attribute(ui::AX_ATTR_URL
);
320 // Given a url like http://foo.com/bar/baz.png, just return the
321 // base name, e.g., "baz".
322 int trailing_slashes
= 0;
323 while (url
.size() - trailing_slashes
> 0 &&
324 url
[url
.size() - trailing_slashes
- 1] == '/') {
327 if (trailing_slashes
)
328 url
= url
.substr(0, url
.size() - trailing_slashes
);
329 size_t slash_index
= url
.rfind('/');
330 if (slash_index
!= std::string::npos
)
331 url
= url
.substr(slash_index
+ 1);
332 size_t dot_index
= url
.rfind('.');
333 if (dot_index
!= std::string::npos
)
334 url
= url
.substr(0, dot_index
);
341 int BrowserAccessibilityAndroid::GetItemIndex() const {
344 case ui::AX_ROLE_LIST_ITEM
:
345 case ui::AX_ROLE_LIST_BOX_OPTION
:
346 case ui::AX_ROLE_TREE_ITEM
:
347 index
= GetIndexInParent();
349 case ui::AX_ROLE_SLIDER
:
350 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
351 float value_for_range
;
352 if (GetFloatAttribute(
353 ui::AX_ATTR_VALUE_FOR_RANGE
, &value_for_range
)) {
354 index
= static_cast<int>(value_for_range
);
362 int BrowserAccessibilityAndroid::GetItemCount() const {
365 case ui::AX_ROLE_LIST
:
366 case ui::AX_ROLE_LIST_BOX
:
367 count
= PlatformChildCount();
369 case ui::AX_ROLE_SLIDER
:
370 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
371 float max_value_for_range
;
372 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
,
373 &max_value_for_range
)) {
374 count
= static_cast<int>(max_value_for_range
);
382 int BrowserAccessibilityAndroid::GetScrollX() const {
384 GetIntAttribute(ui::AX_ATTR_SCROLL_X
, &value
);
388 int BrowserAccessibilityAndroid::GetScrollY() const {
390 GetIntAttribute(ui::AX_ATTR_SCROLL_Y
, &value
);
394 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
396 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
, &value
);
400 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
402 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX
, &value
);
406 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
408 while (index
< old_value_
.length() &&
409 index
< new_value_
.length() &&
410 old_value_
[index
] == new_value_
[index
]) {
416 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
417 size_t old_len
= old_value_
.length();
418 size_t new_len
= new_value_
.length();
420 while (left
< old_len
&&
422 old_value_
[left
] == new_value_
[left
]) {
426 while (right
< old_len
&&
428 old_value_
[old_len
- right
- 1] == new_value_
[new_len
- right
- 1]) {
431 return (new_len
- left
- right
);
434 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
435 size_t old_len
= old_value_
.length();
436 size_t new_len
= new_value_
.length();
438 while (left
< old_len
&&
440 old_value_
[left
] == new_value_
[left
]) {
444 while (right
< old_len
&&
446 old_value_
[old_len
- right
- 1] == new_value_
[new_len
- right
- 1]) {
449 return (old_len
- left
- right
);
452 base::string16
BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
456 int BrowserAccessibilityAndroid::GetSelectionStart() const {
458 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START
, &sel_start
);
462 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
464 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END
, &sel_end
);
468 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
469 return value().length();
472 int BrowserAccessibilityAndroid::AndroidInputType() const {
473 std::string html_tag
= GetStringAttribute(
474 ui::AX_ATTR_HTML_TAG
);
475 if (html_tag
!= "input")
476 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
479 if (!GetHtmlAttribute("type", &type
))
480 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
482 if (type
== "" || type
== "text" || type
== "search")
483 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
484 else if (type
== "date")
485 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
486 else if (type
== "datetime" || type
== "datetime-local")
487 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
488 else if (type
== "email")
489 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL
;
490 else if (type
== "month")
491 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
492 else if (type
== "number")
493 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER
;
494 else if (type
== "password")
495 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD
;
496 else if (type
== "tel")
497 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE
;
498 else if (type
== "time")
499 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME
;
500 else if (type
== "url")
501 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI
;
502 else if (type
== "week")
503 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
505 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
508 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
509 std::string live
= GetStringAttribute(
510 ui::AX_ATTR_LIVE_STATUS
);
511 if (live
== "polite")
512 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE
;
513 else if (live
== "assertive")
514 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE
;
515 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE
;
518 int BrowserAccessibilityAndroid::AndroidRangeType() const {
519 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT
;
522 int BrowserAccessibilityAndroid::RowCount() const {
523 if (GetRole() == ui::AX_ROLE_GRID
||
524 GetRole() == ui::AX_ROLE_TABLE
) {
525 return CountChildrenWithRole(ui::AX_ROLE_ROW
);
528 if (GetRole() == ui::AX_ROLE_LIST
||
529 GetRole() == ui::AX_ROLE_LIST_BOX
||
530 GetRole() == ui::AX_ROLE_TREE
) {
531 return PlatformChildCount();
537 int BrowserAccessibilityAndroid::ColumnCount() const {
538 if (GetRole() == ui::AX_ROLE_GRID
||
539 GetRole() == ui::AX_ROLE_TABLE
) {
540 return CountChildrenWithRole(ui::AX_ROLE_COLUMN
);
545 int BrowserAccessibilityAndroid::RowIndex() const {
546 if (GetRole() == ui::AX_ROLE_LIST_ITEM
||
547 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION
||
548 GetRole() == ui::AX_ROLE_TREE_ITEM
) {
549 return GetIndexInParent();
552 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX
);
555 int BrowserAccessibilityAndroid::RowSpan() const {
556 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN
);
559 int BrowserAccessibilityAndroid::ColumnIndex() const {
560 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX
);
563 int BrowserAccessibilityAndroid::ColumnSpan() const {
564 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN
);
567 float BrowserAccessibilityAndroid::RangeMin() const {
568 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
571 float BrowserAccessibilityAndroid::RangeMax() const {
572 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
575 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
576 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
579 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
580 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
582 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
583 BrowserAccessibility
* child
= InternalGetChild(i
);
584 if (child
->HasState(ui::AX_STATE_FOCUSABLE
))
586 if (static_cast<BrowserAccessibilityAndroid
*>(child
)->HasFocusableChild())
592 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
593 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
595 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
596 BrowserAccessibility
* child
= InternalGetChild(i
);
597 if (child
->GetRole() != ui::AX_ROLE_STATIC_TEXT
)
603 bool BrowserAccessibilityAndroid::IsIframe() const {
604 base::string16 html_tag
= GetString16Attribute(
605 ui::AX_ATTR_HTML_TAG
);
606 return html_tag
== base::ASCIIToUTF16("iframe");
609 void BrowserAccessibilityAndroid::OnDataChanged() {
610 BrowserAccessibility::OnDataChanged();
612 if (IsEditableText()) {
613 if (base::UTF8ToUTF16(value()) != new_value_
) {
614 old_value_
= new_value_
;
615 new_value_
= base::UTF8ToUTF16(value());
619 if (GetRole() == ui::AX_ROLE_ALERT
&& first_time_
)
620 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT
, this);
623 if (GetString16Attribute(
624 ui::AX_ATTR_CONTAINER_LIVE_STATUS
, &live
)) {
625 NotifyLiveRegionUpdate(live
);
631 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
632 base::string16
& aria_live
) {
633 if (!EqualsASCII(aria_live
, aria_strings::kAriaLivePolite
) &&
634 !EqualsASCII(aria_live
, aria_strings::kAriaLiveAssertive
))
637 base::string16 text
= GetText();
638 if (cached_text_
!= text
) {
640 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW
,
647 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role
) const {
649 for (uint32 i
= 0; i
< PlatformChildCount(); i
++) {
650 if (PlatformGetChild(i
)->GetRole() == role
)
656 } // namespace content