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 bool BrowserAccessibilityAndroid::PlatformIsLeaf() const {
59 if (InternalChildCount() == 0)
62 // Iframes are always allowed to contain children.
64 GetRole() == ui::AX_ROLE_ROOT_WEB_AREA
||
65 GetRole() == ui::AX_ROLE_WEB_AREA
) {
69 // If it has a focusable child, we definitely can't leave out children.
70 if (HasFocusableChild())
73 // Headings with text can drop their children.
74 base::string16 name
= GetText();
75 if (GetRole() == ui::AX_ROLE_HEADING
&& !name
.empty())
78 // Focusable nodes with text can drop their children.
79 if (HasState(ui::AX_STATE_FOCUSABLE
) && !name
.empty())
82 // Nodes with only static text as children can drop their children.
83 if (HasOnlyStaticTextChildren())
86 return BrowserAccessibility::PlatformIsLeaf();
89 bool BrowserAccessibilityAndroid::IsCheckable() const {
90 bool checkable
= false;
91 bool is_aria_pressed_defined
;
93 GetAriaTristate("aria-pressed", &is_aria_pressed_defined
, &is_mixed
);
94 if (GetRole() == ui::AX_ROLE_CHECK_BOX
||
95 GetRole() == ui::AX_ROLE_RADIO_BUTTON
||
96 is_aria_pressed_defined
) {
99 if (HasState(ui::AX_STATE_CHECKED
))
104 bool BrowserAccessibilityAndroid::IsChecked() const {
105 return HasState(ui::AX_STATE_CHECKED
);
108 bool BrowserAccessibilityAndroid::IsClickable() const {
109 return (PlatformIsLeaf() && !GetText().empty());
112 bool BrowserAccessibilityAndroid::IsCollection() const {
113 return (GetRole() == ui::AX_ROLE_GRID
||
114 GetRole() == ui::AX_ROLE_LIST
||
115 GetRole() == ui::AX_ROLE_LIST_BOX
||
116 GetRole() == ui::AX_ROLE_TABLE
||
117 GetRole() == ui::AX_ROLE_TREE
);
120 bool BrowserAccessibilityAndroid::IsCollectionItem() const {
121 return (GetRole() == ui::AX_ROLE_CELL
||
122 GetRole() == ui::AX_ROLE_COLUMN_HEADER
||
123 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST_TERM
||
124 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION
||
125 GetRole() == ui::AX_ROLE_LIST_ITEM
||
126 GetRole() == ui::AX_ROLE_ROW_HEADER
||
127 GetRole() == ui::AX_ROLE_TREE_ITEM
);
130 bool BrowserAccessibilityAndroid::IsContentInvalid() const {
132 return GetHtmlAttribute("aria-invalid", &invalid
);
135 bool BrowserAccessibilityAndroid::IsDismissable() const {
136 return false; // No concept of "dismissable" on the web currently.
139 bool BrowserAccessibilityAndroid::IsEnabled() const {
140 return HasState(ui::AX_STATE_ENABLED
);
143 bool BrowserAccessibilityAndroid::IsFocusable() const {
144 bool focusable
= HasState(ui::AX_STATE_FOCUSABLE
);
146 GetRole() == ui::AX_ROLE_WEB_AREA
) {
152 bool BrowserAccessibilityAndroid::IsFocused() const {
153 return manager()->GetFocus(manager()->GetRoot()) == this;
156 bool BrowserAccessibilityAndroid::IsHeading() const {
157 return (GetRole() == ui::AX_ROLE_COLUMN_HEADER
||
158 GetRole() == ui::AX_ROLE_HEADING
||
159 GetRole() == ui::AX_ROLE_ROW_HEADER
);
162 bool BrowserAccessibilityAndroid::IsHierarchical() const {
163 return (GetRole() == ui::AX_ROLE_LIST
||
164 GetRole() == ui::AX_ROLE_TREE
);
167 bool BrowserAccessibilityAndroid::IsLink() const {
168 return GetRole() == ui::AX_ROLE_LINK
||
169 GetRole() == ui::AX_ROLE_IMAGE_MAP_LINK
;
172 bool BrowserAccessibilityAndroid::IsMultiLine() const {
173 return GetRole() == ui::AX_ROLE_TEXT_AREA
;
176 bool BrowserAccessibilityAndroid::IsPassword() const {
177 return HasState(ui::AX_STATE_PROTECTED
);
180 bool BrowserAccessibilityAndroid::IsRangeType() const {
181 return (GetRole() == ui::AX_ROLE_PROGRESS_INDICATOR
||
182 GetRole() == ui::AX_ROLE_SCROLL_BAR
||
183 GetRole() == ui::AX_ROLE_SLIDER
);
186 bool BrowserAccessibilityAndroid::IsScrollable() const {
188 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
, &dummy
);
191 bool BrowserAccessibilityAndroid::IsSelected() const {
192 return HasState(ui::AX_STATE_SELECTED
);
195 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
196 return !HasState(ui::AX_STATE_INVISIBLE
);
199 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
200 return HasState(ui::AX_STATE_HASPOPUP
);
203 const char* BrowserAccessibilityAndroid::GetClassName() const {
204 const char* class_name
= NULL
;
207 case ui::AX_ROLE_EDITABLE_TEXT
:
208 case ui::AX_ROLE_SPIN_BUTTON
:
209 case ui::AX_ROLE_TEXT_AREA
:
210 case ui::AX_ROLE_TEXT_FIELD
:
211 class_name
= "android.widget.EditText";
213 case ui::AX_ROLE_SLIDER
:
214 class_name
= "android.widget.SeekBar";
216 case ui::AX_ROLE_COMBO_BOX
:
217 class_name
= "android.widget.Spinner";
219 case ui::AX_ROLE_BUTTON
:
220 case ui::AX_ROLE_MENU_BUTTON
:
221 case ui::AX_ROLE_POP_UP_BUTTON
:
222 class_name
= "android.widget.Button";
224 case ui::AX_ROLE_CHECK_BOX
:
225 class_name
= "android.widget.CheckBox";
227 case ui::AX_ROLE_RADIO_BUTTON
:
228 class_name
= "android.widget.RadioButton";
230 case ui::AX_ROLE_TOGGLE_BUTTON
:
231 class_name
= "android.widget.ToggleButton";
233 case ui::AX_ROLE_CANVAS
:
234 case ui::AX_ROLE_IMAGE
:
235 class_name
= "android.widget.Image";
237 case ui::AX_ROLE_PROGRESS_INDICATOR
:
238 class_name
= "android.widget.ProgressBar";
240 case ui::AX_ROLE_TAB_LIST
:
241 class_name
= "android.widget.TabWidget";
243 case ui::AX_ROLE_GRID
:
244 case ui::AX_ROLE_TABLE
:
245 class_name
= "android.widget.GridView";
247 case ui::AX_ROLE_LIST
:
248 case ui::AX_ROLE_LIST_BOX
:
249 class_name
= "android.widget.ListView";
251 case ui::AX_ROLE_DIALOG
:
252 class_name
= "android.app.Dialog";
254 case ui::AX_ROLE_ROOT_WEB_AREA
:
255 class_name
= "android.webkit.WebView";
258 class_name
= "android.view.View";
265 base::string16
BrowserAccessibilityAndroid::GetText() const {
267 GetRole() == ui::AX_ROLE_WEB_AREA
) {
268 return base::string16();
271 // See comment in browser_accessibility_win.cc for details.
272 // The difference here is that we can only expose one accessible
273 // name on Android, not 2 or 3 like on Windows or Mac.
275 // The basic rule is: prefer description (aria-labelledby or aria-label),
276 // then help (title), then name (inner text), then value (control value).
277 // However, if title_elem_id is set, that means there's a label element
278 // supplying the name and then name takes precedence over help.
279 // TODO(dmazzoni): clean this up by providing more granular labels in
280 // Blink, making the platform-specific mapping to accessible text simpler.
281 base::string16 description
= GetString16Attribute(ui::AX_ATTR_DESCRIPTION
);
282 base::string16 help
= GetString16Attribute(ui::AX_ATTR_HELP
);
283 int title_elem_id
= GetIntAttribute(
284 ui::AX_ATTR_TITLE_UI_ELEMENT
);
286 if (!description
.empty())
288 else if (title_elem_id
&& !name().empty())
289 text
= base::UTF8ToUTF16(name());
290 else if (!help
.empty())
292 else if (!name().empty())
293 text
= base::UTF8ToUTF16(name());
294 else if (!value().empty())
295 text
= base::UTF8ToUTF16(value());
297 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
299 if (text
.empty() && HasOnlyStaticTextChildren()) {
300 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
301 BrowserAccessibility
* child
= InternalGetChild(i
);
302 text
+= static_cast<BrowserAccessibilityAndroid
*>(child
)->GetText();
307 case ui::AX_ROLE_HEADING
:
308 // Only append "heading" if this node already has text.
310 text
+= base::ASCIIToUTF16(" Heading");
314 if (text
.empty() && IsLink()) {
315 base::string16 url
= GetString16Attribute(ui::AX_ATTR_URL
);
316 // Given a url like http://foo.com/bar/baz.png, just return the
317 // base name, e.g., "baz".
318 int trailing_slashes
= 0;
319 while (url
.size() - trailing_slashes
> 0 &&
320 url
[url
.size() - trailing_slashes
- 1] == '/') {
323 if (trailing_slashes
)
324 url
= url
.substr(0, url
.size() - trailing_slashes
);
325 size_t slash_index
= url
.rfind('/');
326 if (slash_index
!= std::string::npos
)
327 url
= url
.substr(slash_index
+ 1);
328 size_t dot_index
= url
.rfind('.');
329 if (dot_index
!= std::string::npos
)
330 url
= url
.substr(0, dot_index
);
337 int BrowserAccessibilityAndroid::GetItemIndex() const {
340 case ui::AX_ROLE_LIST_ITEM
:
341 case ui::AX_ROLE_LIST_BOX_OPTION
:
342 case ui::AX_ROLE_TREE_ITEM
:
343 index
= GetIndexInParent();
345 case ui::AX_ROLE_SLIDER
:
346 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
347 float value_for_range
;
348 if (GetFloatAttribute(
349 ui::AX_ATTR_VALUE_FOR_RANGE
, &value_for_range
)) {
350 index
= static_cast<int>(value_for_range
);
358 int BrowserAccessibilityAndroid::GetItemCount() const {
361 case ui::AX_ROLE_LIST
:
362 case ui::AX_ROLE_LIST_BOX
:
363 count
= PlatformChildCount();
365 case ui::AX_ROLE_SLIDER
:
366 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
367 float max_value_for_range
;
368 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
,
369 &max_value_for_range
)) {
370 count
= static_cast<int>(max_value_for_range
);
378 int BrowserAccessibilityAndroid::GetScrollX() const {
380 GetIntAttribute(ui::AX_ATTR_SCROLL_X
, &value
);
384 int BrowserAccessibilityAndroid::GetScrollY() const {
386 GetIntAttribute(ui::AX_ATTR_SCROLL_Y
, &value
);
390 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
392 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
, &value
);
396 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
398 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX
, &value
);
402 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
404 while (index
< old_value_
.length() &&
405 index
< new_value_
.length() &&
406 old_value_
[index
] == new_value_
[index
]) {
412 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
413 size_t old_len
= old_value_
.length();
414 size_t new_len
= new_value_
.length();
416 while (left
< old_len
&&
418 old_value_
[left
] == new_value_
[left
]) {
422 while (right
< old_len
&&
424 old_value_
[old_len
- right
- 1] == new_value_
[new_len
- right
- 1]) {
427 return (new_len
- left
- right
);
430 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
431 size_t old_len
= old_value_
.length();
432 size_t new_len
= new_value_
.length();
434 while (left
< old_len
&&
436 old_value_
[left
] == new_value_
[left
]) {
440 while (right
< old_len
&&
442 old_value_
[old_len
- right
- 1] == new_value_
[new_len
- right
- 1]) {
445 return (old_len
- left
- right
);
448 base::string16
BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
452 int BrowserAccessibilityAndroid::GetSelectionStart() const {
454 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START
, &sel_start
);
458 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
460 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END
, &sel_end
);
464 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
465 return value().length();
468 int BrowserAccessibilityAndroid::AndroidInputType() const {
469 std::string html_tag
= GetStringAttribute(
470 ui::AX_ATTR_HTML_TAG
);
471 if (html_tag
!= "input")
472 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
475 if (!GetHtmlAttribute("type", &type
))
476 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
478 if (type
== "" || type
== "text" || type
== "search")
479 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
480 else if (type
== "date")
481 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
482 else if (type
== "datetime" || type
== "datetime-local")
483 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
484 else if (type
== "email")
485 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL
;
486 else if (type
== "month")
487 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
488 else if (type
== "number")
489 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER
;
490 else if (type
== "password")
491 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD
;
492 else if (type
== "tel")
493 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE
;
494 else if (type
== "time")
495 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME
;
496 else if (type
== "url")
497 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI
;
498 else if (type
== "week")
499 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
501 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
504 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
505 std::string live
= GetStringAttribute(
506 ui::AX_ATTR_LIVE_STATUS
);
507 if (live
== "polite")
508 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE
;
509 else if (live
== "assertive")
510 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE
;
511 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE
;
514 int BrowserAccessibilityAndroid::AndroidRangeType() const {
515 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT
;
518 int BrowserAccessibilityAndroid::RowCount() const {
519 if (GetRole() == ui::AX_ROLE_GRID
||
520 GetRole() == ui::AX_ROLE_TABLE
) {
521 return CountChildrenWithRole(ui::AX_ROLE_ROW
);
524 if (GetRole() == ui::AX_ROLE_LIST
||
525 GetRole() == ui::AX_ROLE_LIST_BOX
||
526 GetRole() == ui::AX_ROLE_TREE
) {
527 return PlatformChildCount();
533 int BrowserAccessibilityAndroid::ColumnCount() const {
534 if (GetRole() == ui::AX_ROLE_GRID
||
535 GetRole() == ui::AX_ROLE_TABLE
) {
536 return CountChildrenWithRole(ui::AX_ROLE_COLUMN
);
541 int BrowserAccessibilityAndroid::RowIndex() const {
542 if (GetRole() == ui::AX_ROLE_LIST_ITEM
||
543 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION
||
544 GetRole() == ui::AX_ROLE_TREE_ITEM
) {
545 return GetIndexInParent();
548 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX
);
551 int BrowserAccessibilityAndroid::RowSpan() const {
552 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN
);
555 int BrowserAccessibilityAndroid::ColumnIndex() const {
556 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX
);
559 int BrowserAccessibilityAndroid::ColumnSpan() const {
560 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN
);
563 float BrowserAccessibilityAndroid::RangeMin() const {
564 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
567 float BrowserAccessibilityAndroid::RangeMax() const {
568 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
571 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
572 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
575 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
576 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
578 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
579 BrowserAccessibility
* child
= InternalGetChild(i
);
580 if (child
->HasState(ui::AX_STATE_FOCUSABLE
))
582 if (static_cast<BrowserAccessibilityAndroid
*>(child
)->HasFocusableChild())
588 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
589 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
591 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
592 BrowserAccessibility
* child
= InternalGetChild(i
);
593 if (child
->GetRole() != ui::AX_ROLE_STATIC_TEXT
)
599 bool BrowserAccessibilityAndroid::IsIframe() const {
600 base::string16 html_tag
= GetString16Attribute(
601 ui::AX_ATTR_HTML_TAG
);
602 return html_tag
== base::ASCIIToUTF16("iframe");
605 void BrowserAccessibilityAndroid::OnDataChanged() {
606 BrowserAccessibility::OnDataChanged();
608 if (IsEditableText()) {
609 if (base::UTF8ToUTF16(value()) != new_value_
) {
610 old_value_
= new_value_
;
611 new_value_
= base::UTF8ToUTF16(value());
615 if (GetRole() == ui::AX_ROLE_ALERT
&& first_time_
)
616 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT
, this);
619 if (GetString16Attribute(
620 ui::AX_ATTR_CONTAINER_LIVE_STATUS
, &live
)) {
621 NotifyLiveRegionUpdate(live
);
627 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
628 base::string16
& aria_live
) {
629 if (!EqualsASCII(aria_live
, aria_strings::kAriaLivePolite
) &&
630 !EqualsASCII(aria_live
, aria_strings::kAriaLiveAssertive
))
633 base::string16 text
= GetText();
634 if (cached_text_
!= text
) {
636 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW
,
643 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role
) const {
645 for (uint32 i
= 0; i
< PlatformChildCount(); i
++) {
646 if (PlatformGetChild(i
)->GetRole() == role
)
652 } // namespace content