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 base::string16 description
= GetString16Attribute(ui::AX_ATTR_DESCRIPTION
);
274 text
= base::UTF8ToUTF16(name());
275 else if (!description
.empty())
277 else if (!value().empty())
278 text
= base::UTF8ToUTF16(value());
280 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
282 if (text
.empty() && HasOnlyStaticTextChildren()) {
283 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
284 BrowserAccessibility
* child
= InternalGetChild(i
);
285 text
+= static_cast<BrowserAccessibilityAndroid
*>(child
)->GetText();
290 case ui::AX_ROLE_HEADING
:
291 // Only append "heading" if this node already has text.
293 text
+= base::ASCIIToUTF16(" Heading");
297 if (text
.empty() && IsLink()) {
298 base::string16 url
= GetString16Attribute(ui::AX_ATTR_URL
);
299 // Given a url like http://foo.com/bar/baz.png, just return the
300 // base name, e.g., "baz".
301 int trailing_slashes
= 0;
302 while (url
.size() - trailing_slashes
> 0 &&
303 url
[url
.size() - trailing_slashes
- 1] == '/') {
306 if (trailing_slashes
)
307 url
= url
.substr(0, url
.size() - trailing_slashes
);
308 size_t slash_index
= url
.rfind('/');
309 if (slash_index
!= std::string::npos
)
310 url
= url
.substr(slash_index
+ 1);
311 size_t dot_index
= url
.rfind('.');
312 if (dot_index
!= std::string::npos
)
313 url
= url
.substr(0, dot_index
);
320 int BrowserAccessibilityAndroid::GetItemIndex() const {
323 case ui::AX_ROLE_LIST_ITEM
:
324 case ui::AX_ROLE_LIST_BOX_OPTION
:
325 case ui::AX_ROLE_TREE_ITEM
:
326 index
= GetIndexInParent();
328 case ui::AX_ROLE_SLIDER
:
329 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
330 float value_for_range
;
331 if (GetFloatAttribute(
332 ui::AX_ATTR_VALUE_FOR_RANGE
, &value_for_range
)) {
333 index
= static_cast<int>(value_for_range
);
341 int BrowserAccessibilityAndroid::GetItemCount() const {
344 case ui::AX_ROLE_LIST
:
345 case ui::AX_ROLE_LIST_BOX
:
346 count
= PlatformChildCount();
348 case ui::AX_ROLE_SLIDER
:
349 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
350 float max_value_for_range
;
351 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
,
352 &max_value_for_range
)) {
353 count
= static_cast<int>(max_value_for_range
);
361 int BrowserAccessibilityAndroid::GetScrollX() const {
363 GetIntAttribute(ui::AX_ATTR_SCROLL_X
, &value
);
367 int BrowserAccessibilityAndroid::GetScrollY() const {
369 GetIntAttribute(ui::AX_ATTR_SCROLL_Y
, &value
);
373 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
375 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
, &value
);
379 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
381 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX
, &value
);
385 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
387 while (index
< old_value_
.length() &&
388 index
< new_value_
.length() &&
389 old_value_
[index
] == new_value_
[index
]) {
395 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
396 size_t old_len
= old_value_
.length();
397 size_t new_len
= new_value_
.length();
399 while (left
< old_len
&&
401 old_value_
[left
] == new_value_
[left
]) {
405 while (right
< old_len
&&
407 old_value_
[old_len
- right
- 1] == new_value_
[new_len
- right
- 1]) {
410 return (new_len
- left
- right
);
413 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
414 size_t old_len
= old_value_
.length();
415 size_t new_len
= new_value_
.length();
417 while (left
< old_len
&&
419 old_value_
[left
] == new_value_
[left
]) {
423 while (right
< old_len
&&
425 old_value_
[old_len
- right
- 1] == new_value_
[new_len
- right
- 1]) {
428 return (old_len
- left
- right
);
431 base::string16
BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
435 int BrowserAccessibilityAndroid::GetSelectionStart() const {
437 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START
, &sel_start
);
441 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
443 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END
, &sel_end
);
447 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
448 return value().length();
451 int BrowserAccessibilityAndroid::AndroidInputType() const {
452 std::string html_tag
= GetStringAttribute(
453 ui::AX_ATTR_HTML_TAG
);
454 if (html_tag
!= "input")
455 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
458 if (!GetHtmlAttribute("type", &type
))
459 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
461 if (type
== "" || type
== "text" || type
== "search")
462 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
463 else if (type
== "date")
464 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
465 else if (type
== "datetime" || type
== "datetime-local")
466 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
467 else if (type
== "email")
468 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL
;
469 else if (type
== "month")
470 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
471 else if (type
== "number")
472 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER
;
473 else if (type
== "password")
474 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD
;
475 else if (type
== "tel")
476 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE
;
477 else if (type
== "time")
478 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME
;
479 else if (type
== "url")
480 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI
;
481 else if (type
== "week")
482 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
484 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
487 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
488 std::string live
= GetStringAttribute(
489 ui::AX_ATTR_LIVE_STATUS
);
490 if (live
== "polite")
491 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE
;
492 else if (live
== "assertive")
493 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE
;
494 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE
;
497 int BrowserAccessibilityAndroid::AndroidRangeType() const {
498 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT
;
501 int BrowserAccessibilityAndroid::RowCount() const {
502 if (GetRole() == ui::AX_ROLE_GRID
||
503 GetRole() == ui::AX_ROLE_TABLE
) {
504 return CountChildrenWithRole(ui::AX_ROLE_ROW
);
507 if (GetRole() == ui::AX_ROLE_LIST
||
508 GetRole() == ui::AX_ROLE_LIST_BOX
||
509 GetRole() == ui::AX_ROLE_TREE
) {
510 return PlatformChildCount();
516 int BrowserAccessibilityAndroid::ColumnCount() const {
517 if (GetRole() == ui::AX_ROLE_GRID
||
518 GetRole() == ui::AX_ROLE_TABLE
) {
519 return CountChildrenWithRole(ui::AX_ROLE_COLUMN
);
524 int BrowserAccessibilityAndroid::RowIndex() const {
525 if (GetRole() == ui::AX_ROLE_LIST_ITEM
||
526 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION
||
527 GetRole() == ui::AX_ROLE_TREE_ITEM
) {
528 return GetIndexInParent();
531 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX
);
534 int BrowserAccessibilityAndroid::RowSpan() const {
535 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN
);
538 int BrowserAccessibilityAndroid::ColumnIndex() const {
539 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX
);
542 int BrowserAccessibilityAndroid::ColumnSpan() const {
543 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN
);
546 float BrowserAccessibilityAndroid::RangeMin() const {
547 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
550 float BrowserAccessibilityAndroid::RangeMax() const {
551 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
554 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
555 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
558 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
559 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
561 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
562 BrowserAccessibility
* child
= InternalGetChild(i
);
563 if (child
->HasState(ui::AX_STATE_FOCUSABLE
))
565 if (static_cast<BrowserAccessibilityAndroid
*>(child
)->HasFocusableChild())
571 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
572 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
574 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
575 BrowserAccessibility
* child
= InternalGetChild(i
);
576 if (child
->GetRole() != ui::AX_ROLE_STATIC_TEXT
)
582 bool BrowserAccessibilityAndroid::IsIframe() const {
583 base::string16 html_tag
= GetString16Attribute(
584 ui::AX_ATTR_HTML_TAG
);
585 return html_tag
== base::ASCIIToUTF16("iframe");
588 void BrowserAccessibilityAndroid::OnDataChanged() {
589 BrowserAccessibility::OnDataChanged();
591 if (IsEditableText()) {
592 if (base::UTF8ToUTF16(value()) != new_value_
) {
593 old_value_
= new_value_
;
594 new_value_
= base::UTF8ToUTF16(value());
598 if (GetRole() == ui::AX_ROLE_ALERT
&& first_time_
)
599 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT
, this);
602 if (GetString16Attribute(
603 ui::AX_ATTR_CONTAINER_LIVE_STATUS
, &live
)) {
604 NotifyLiveRegionUpdate(live
);
610 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
611 base::string16
& aria_live
) {
612 if (!EqualsASCII(aria_live
, aria_strings::kAriaLivePolite
) &&
613 !EqualsASCII(aria_live
, aria_strings::kAriaLiveAssertive
))
616 base::string16 text
= GetText();
617 if (cached_text_
!= text
) {
619 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW
,
626 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role
) const {
628 for (uint32 i
= 0; i
< PlatformChildCount(); i
++) {
629 if (PlatformGetChild(i
)->GetRole() == role
)
635 } // namespace content