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 (child_count() == 0)
62 // Iframes are always allowed to contain children.
64 role() == ui::AX_ROLE_ROOT_WEB_AREA
||
65 role() == 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 (role() == 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 (role() == ui::AX_ROLE_CHECK_BOX
||
95 role() == 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 (role() == ui::AX_ROLE_GRID
||
114 role() == ui::AX_ROLE_LIST
||
115 role() == ui::AX_ROLE_LIST_BOX
||
116 role() == ui::AX_ROLE_TABLE
||
117 role() == ui::AX_ROLE_TREE
);
120 bool BrowserAccessibilityAndroid::IsCollectionItem() const {
121 return (role() == ui::AX_ROLE_CELL
||
122 role() == ui::AX_ROLE_COLUMN_HEADER
||
123 role() == ui::AX_ROLE_DESCRIPTION_LIST_TERM
||
124 role() == ui::AX_ROLE_LIST_BOX_OPTION
||
125 role() == ui::AX_ROLE_LIST_ITEM
||
126 role() == ui::AX_ROLE_ROW_HEADER
||
127 role() == 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 role() == ui::AX_ROLE_WEB_AREA
) {
152 bool BrowserAccessibilityAndroid::IsFocused() const {
153 return manager()->GetFocus(manager()->GetRoot()) == this;
156 bool BrowserAccessibilityAndroid::IsHeading() const {
157 return (role() == ui::AX_ROLE_COLUMN_HEADER
||
158 role() == ui::AX_ROLE_HEADING
||
159 role() == ui::AX_ROLE_ROW_HEADER
);
162 bool BrowserAccessibilityAndroid::IsHierarchical() const {
163 return (role() == ui::AX_ROLE_LIST
||
164 role() == ui::AX_ROLE_TREE
);
167 bool BrowserAccessibilityAndroid::IsMultiLine() const {
168 return role() == ui::AX_ROLE_TEXT_AREA
;
171 bool BrowserAccessibilityAndroid::IsPassword() const {
172 return HasState(ui::AX_STATE_PROTECTED
);
175 bool BrowserAccessibilityAndroid::IsRangeType() const {
176 return (role() == ui::AX_ROLE_PROGRESS_INDICATOR
||
177 role() == ui::AX_ROLE_SCROLL_BAR
||
178 role() == ui::AX_ROLE_SLIDER
);
181 bool BrowserAccessibilityAndroid::IsScrollable() const {
183 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
, &dummy
);
186 bool BrowserAccessibilityAndroid::IsSelected() const {
187 return HasState(ui::AX_STATE_SELECTED
);
190 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
191 return !HasState(ui::AX_STATE_INVISIBLE
);
194 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
195 return HasState(ui::AX_STATE_HASPOPUP
);
198 const char* BrowserAccessibilityAndroid::GetClassName() const {
199 const char* class_name
= NULL
;
202 case ui::AX_ROLE_EDITABLE_TEXT
:
203 case ui::AX_ROLE_SPIN_BUTTON
:
204 case ui::AX_ROLE_TEXT_AREA
:
205 case ui::AX_ROLE_TEXT_FIELD
:
206 class_name
= "android.widget.EditText";
208 case ui::AX_ROLE_SLIDER
:
209 class_name
= "android.widget.SeekBar";
211 case ui::AX_ROLE_COMBO_BOX
:
212 class_name
= "android.widget.Spinner";
214 case ui::AX_ROLE_BUTTON
:
215 case ui::AX_ROLE_MENU_BUTTON
:
216 case ui::AX_ROLE_POP_UP_BUTTON
:
217 class_name
= "android.widget.Button";
219 case ui::AX_ROLE_CHECK_BOX
:
220 class_name
= "android.widget.CheckBox";
222 case ui::AX_ROLE_RADIO_BUTTON
:
223 class_name
= "android.widget.RadioButton";
225 case ui::AX_ROLE_TOGGLE_BUTTON
:
226 class_name
= "android.widget.ToggleButton";
228 case ui::AX_ROLE_CANVAS
:
229 case ui::AX_ROLE_IMAGE
:
230 class_name
= "android.widget.Image";
232 case ui::AX_ROLE_PROGRESS_INDICATOR
:
233 class_name
= "android.widget.ProgressBar";
235 case ui::AX_ROLE_TAB_LIST
:
236 class_name
= "android.widget.TabWidget";
238 case ui::AX_ROLE_GRID
:
239 case ui::AX_ROLE_TABLE
:
240 class_name
= "android.widget.GridView";
242 case ui::AX_ROLE_LIST
:
243 case ui::AX_ROLE_LIST_BOX
:
244 class_name
= "android.widget.ListView";
246 case ui::AX_ROLE_DIALOG
:
247 class_name
= "android.app.Dialog";
250 class_name
= "android.view.View";
257 base::string16
BrowserAccessibilityAndroid::GetText() const {
259 role() == ui::AX_ROLE_WEB_AREA
) {
260 return base::string16();
263 base::string16 description
= GetString16Attribute(
264 ui::AX_ATTR_DESCRIPTION
);
267 text
= base::UTF8ToUTF16(name());
268 else if (!description
.empty())
270 else if (!value().empty())
271 text
= base::UTF8ToUTF16(value());
273 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
275 if (text
.empty() && HasOnlyStaticTextChildren()) {
276 for (uint32 i
= 0; i
< child_count(); i
++) {
277 BrowserAccessibility
* child
= children()[i
];
278 text
+= static_cast<BrowserAccessibilityAndroid
*>(child
)->GetText();
283 case ui::AX_ROLE_IMAGE_MAP_LINK
:
284 case ui::AX_ROLE_LINK
:
286 text
+= base::ASCIIToUTF16(" ");
287 text
+= base::ASCIIToUTF16("Link");
289 case ui::AX_ROLE_HEADING
:
290 // Only append "heading" if this node already has text.
292 text
+= base::ASCIIToUTF16(" Heading");
299 int BrowserAccessibilityAndroid::GetItemIndex() const {
302 case ui::AX_ROLE_LIST_ITEM
:
303 case ui::AX_ROLE_LIST_BOX_OPTION
:
304 case ui::AX_ROLE_TREE_ITEM
:
305 index
= index_in_parent();
307 case ui::AX_ROLE_SLIDER
:
308 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
309 float value_for_range
;
310 if (GetFloatAttribute(
311 ui::AX_ATTR_VALUE_FOR_RANGE
, &value_for_range
)) {
312 index
= static_cast<int>(value_for_range
);
320 int BrowserAccessibilityAndroid::GetItemCount() const {
323 case ui::AX_ROLE_LIST
:
324 case ui::AX_ROLE_LIST_BOX
:
325 count
= PlatformChildCount();
327 case ui::AX_ROLE_SLIDER
:
328 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
329 float max_value_for_range
;
330 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
,
331 &max_value_for_range
)) {
332 count
= static_cast<int>(max_value_for_range
);
340 int BrowserAccessibilityAndroid::GetScrollX() const {
342 GetIntAttribute(ui::AX_ATTR_SCROLL_X
, &value
);
346 int BrowserAccessibilityAndroid::GetScrollY() const {
348 GetIntAttribute(ui::AX_ATTR_SCROLL_Y
, &value
);
352 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
354 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
, &value
);
358 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
360 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX
, &value
);
364 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
366 while (index
< old_value_
.length() &&
367 index
< new_value_
.length() &&
368 old_value_
[index
] == new_value_
[index
]) {
374 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
375 size_t old_len
= old_value_
.length();
376 size_t new_len
= new_value_
.length();
378 while (left
< old_len
&&
380 old_value_
[left
] == new_value_
[left
]) {
384 while (right
< old_len
&&
386 old_value_
[old_len
- right
- 1] == new_value_
[new_len
- right
- 1]) {
389 return (new_len
- left
- right
);
392 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
393 size_t old_len
= old_value_
.length();
394 size_t new_len
= new_value_
.length();
396 while (left
< old_len
&&
398 old_value_
[left
] == new_value_
[left
]) {
402 while (right
< old_len
&&
404 old_value_
[old_len
- right
- 1] == new_value_
[new_len
- right
- 1]) {
407 return (old_len
- left
- right
);
410 base::string16
BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
414 int BrowserAccessibilityAndroid::GetSelectionStart() const {
416 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START
, &sel_start
);
420 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
422 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END
, &sel_end
);
426 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
427 return value().length();
430 int BrowserAccessibilityAndroid::AndroidInputType() const {
431 std::string html_tag
= GetStringAttribute(
432 ui::AX_ATTR_HTML_TAG
);
433 if (html_tag
!= "input")
434 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
437 if (!GetHtmlAttribute("type", &type
))
438 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
440 if (type
== "" || type
== "text" || type
== "search")
441 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
442 else if (type
== "date")
443 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
444 else if (type
== "datetime" || type
== "datetime-local")
445 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
446 else if (type
== "email")
447 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL
;
448 else if (type
== "month")
449 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
450 else if (type
== "number")
451 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER
;
452 else if (type
== "password")
453 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD
;
454 else if (type
== "tel")
455 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE
;
456 else if (type
== "time")
457 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME
;
458 else if (type
== "url")
459 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI
;
460 else if (type
== "week")
461 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
463 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
466 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
467 std::string live
= GetStringAttribute(
468 ui::AX_ATTR_LIVE_STATUS
);
469 if (live
== "polite")
470 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE
;
471 else if (live
== "assertive")
472 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE
;
473 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE
;
476 int BrowserAccessibilityAndroid::AndroidRangeType() const {
477 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT
;
480 int BrowserAccessibilityAndroid::RowCount() const {
481 if (role() == ui::AX_ROLE_GRID
||
482 role() == ui::AX_ROLE_TABLE
) {
483 return CountChildrenWithRole(ui::AX_ROLE_ROW
);
486 if (role() == ui::AX_ROLE_LIST
||
487 role() == ui::AX_ROLE_LIST_BOX
||
488 role() == ui::AX_ROLE_TREE
) {
489 return PlatformChildCount();
495 int BrowserAccessibilityAndroid::ColumnCount() const {
496 if (role() == ui::AX_ROLE_GRID
||
497 role() == ui::AX_ROLE_TABLE
) {
498 return CountChildrenWithRole(ui::AX_ROLE_COLUMN
);
503 int BrowserAccessibilityAndroid::RowIndex() const {
504 if (role() == ui::AX_ROLE_LIST_ITEM
||
505 role() == ui::AX_ROLE_LIST_BOX_OPTION
||
506 role() == ui::AX_ROLE_TREE_ITEM
) {
507 return index_in_parent();
510 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX
);
513 int BrowserAccessibilityAndroid::RowSpan() const {
514 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN
);
517 int BrowserAccessibilityAndroid::ColumnIndex() const {
518 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX
);
521 int BrowserAccessibilityAndroid::ColumnSpan() const {
522 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN
);
525 float BrowserAccessibilityAndroid::RangeMin() const {
526 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
529 float BrowserAccessibilityAndroid::RangeMax() const {
530 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
533 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
534 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
537 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
538 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
540 for (uint32 i
= 0; i
< child_count(); i
++) {
541 BrowserAccessibility
* child
= children()[i
];
542 if (child
->HasState(ui::AX_STATE_FOCUSABLE
))
544 if (static_cast<BrowserAccessibilityAndroid
*>(child
)->HasFocusableChild())
550 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
551 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
553 for (uint32 i
= 0; i
< child_count(); i
++) {
554 BrowserAccessibility
* child
= children()[i
];
555 if (child
->role() != ui::AX_ROLE_STATIC_TEXT
)
561 bool BrowserAccessibilityAndroid::IsIframe() const {
562 base::string16 html_tag
= GetString16Attribute(
563 ui::AX_ATTR_HTML_TAG
);
564 return html_tag
== base::ASCIIToUTF16("iframe");
567 void BrowserAccessibilityAndroid::PostInitialize() {
568 BrowserAccessibility::PostInitialize();
570 if (IsEditableText()) {
571 if (base::UTF8ToUTF16(value()) != new_value_
) {
572 old_value_
= new_value_
;
573 new_value_
= base::UTF8ToUTF16(value());
577 if (role() == ui::AX_ROLE_ALERT
&& first_time_
)
578 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT
, this);
581 if (GetString16Attribute(
582 ui::AX_ATTR_CONTAINER_LIVE_STATUS
, &live
)) {
583 NotifyLiveRegionUpdate(live
);
589 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
590 base::string16
& aria_live
) {
591 if (!EqualsASCII(aria_live
, aria_strings::kAriaLivePolite
) &&
592 !EqualsASCII(aria_live
, aria_strings::kAriaLiveAssertive
))
595 base::string16 text
= GetText();
596 if (cached_text_
!= text
) {
598 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW
,
605 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role
) const {
607 for (uint32 i
= 0; i
< PlatformChildCount(); i
++) {
608 if (PlatformGetChild(i
)->role() == role
)
614 } // namespace content