f286c85d43fb469eca92236fb15e51ab7b37a542
[platform/framework/web/crosswalk.git] / src / content / browser / accessibility / browser_accessibility_android.cc
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.
4
5 #include "content/browser/accessibility/browser_accessibility_android.h"
6
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"
10
11 namespace {
12
13 // These are enums from android.text.InputType in Java:
14 enum {
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
26 };
27
28 // These are enums from android.view.View in Java:
29 enum {
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
33 };
34
35 // These are enums from
36 // android.view.accessibility.AccessibilityNodeInfo.RangeInfo in Java:
37 enum {
38   ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT = 1
39 };
40
41 }  // namespace
42
43 namespace content {
44
45 // static
46 BrowserAccessibility* BrowserAccessibility::Create() {
47   return new BrowserAccessibilityAndroid();
48 }
49
50 BrowserAccessibilityAndroid::BrowserAccessibilityAndroid() {
51   first_time_ = true;
52 }
53
54 bool BrowserAccessibilityAndroid::IsNative() const {
55   return true;
56 }
57
58 bool BrowserAccessibilityAndroid::PlatformIsLeaf() const {
59   if (child_count() == 0)
60     return true;
61
62   // Iframes are always allowed to contain children.
63   if (IsIframe() ||
64       role() == ui::AX_ROLE_ROOT_WEB_AREA ||
65       role() == ui::AX_ROLE_WEB_AREA) {
66     return false;
67   }
68
69   // If it has a focusable child, we definitely can't leave out children.
70   if (HasFocusableChild())
71     return false;
72
73   // Headings with text can drop their children.
74   base::string16 name = GetText();
75   if (role() == ui::AX_ROLE_HEADING && !name.empty())
76     return true;
77
78   // Focusable nodes with text can drop their children.
79   if (HasState(ui::AX_STATE_FOCUSABLE) && !name.empty())
80     return true;
81
82   // Nodes with only static text as children can drop their children.
83   if (HasOnlyStaticTextChildren())
84     return true;
85
86   return BrowserAccessibility::PlatformIsLeaf();
87 }
88
89 bool BrowserAccessibilityAndroid::IsCheckable() const {
90   bool checkable = false;
91   bool is_aria_pressed_defined;
92   bool is_mixed;
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) {
97     checkable = true;
98   }
99   if (HasState(ui::AX_STATE_CHECKED))
100     checkable = true;
101   return checkable;
102 }
103
104 bool BrowserAccessibilityAndroid::IsChecked() const {
105   return HasState(ui::AX_STATE_CHECKED);
106 }
107
108 bool BrowserAccessibilityAndroid::IsClickable() const {
109   return (PlatformIsLeaf() && !GetText().empty());
110 }
111
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);
118 }
119
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);
128 }
129
130 bool BrowserAccessibilityAndroid::IsContentInvalid() const {
131   std::string invalid;
132   return GetHtmlAttribute("aria-invalid", &invalid);
133 }
134
135 bool BrowserAccessibilityAndroid::IsDismissable() const {
136   return false;  // No concept of "dismissable" on the web currently.
137 }
138
139 bool BrowserAccessibilityAndroid::IsEnabled() const {
140   return HasState(ui::AX_STATE_ENABLED);
141 }
142
143 bool BrowserAccessibilityAndroid::IsFocusable() const {
144   bool focusable = HasState(ui::AX_STATE_FOCUSABLE);
145   if (IsIframe() ||
146       role() == ui::AX_ROLE_WEB_AREA) {
147     focusable = false;
148   }
149   return focusable;
150 }
151
152 bool BrowserAccessibilityAndroid::IsFocused() const {
153   return manager()->GetFocus(manager()->GetRoot()) == this;
154 }
155
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);
160 }
161
162 bool BrowserAccessibilityAndroid::IsHierarchical() const {
163   return (role() == ui::AX_ROLE_LIST ||
164           role() == ui::AX_ROLE_TREE);
165 }
166
167 bool BrowserAccessibilityAndroid::IsLink() const {
168   return role() == ui::AX_ROLE_LINK || role() == ui::AX_ROLE_IMAGE_MAP_LINK;
169 }
170
171 bool BrowserAccessibilityAndroid::IsMultiLine() const {
172   return role() == ui::AX_ROLE_TEXT_AREA;
173 }
174
175 bool BrowserAccessibilityAndroid::IsPassword() const {
176   return HasState(ui::AX_STATE_PROTECTED);
177 }
178
179 bool BrowserAccessibilityAndroid::IsRangeType() const {
180   return (role() == ui::AX_ROLE_PROGRESS_INDICATOR ||
181           role() == ui::AX_ROLE_SCROLL_BAR ||
182           role() == ui::AX_ROLE_SLIDER);
183 }
184
185 bool BrowserAccessibilityAndroid::IsScrollable() const {
186   int dummy;
187   return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &dummy);
188 }
189
190 bool BrowserAccessibilityAndroid::IsSelected() const {
191   return HasState(ui::AX_STATE_SELECTED);
192 }
193
194 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
195   return !HasState(ui::AX_STATE_INVISIBLE);
196 }
197
198 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
199   return HasState(ui::AX_STATE_HASPOPUP);
200 }
201
202 const char* BrowserAccessibilityAndroid::GetClassName() const {
203   const char* class_name = NULL;
204
205   switch(role()) {
206     case ui::AX_ROLE_EDITABLE_TEXT:
207     case ui::AX_ROLE_SPIN_BUTTON:
208     case ui::AX_ROLE_TEXT_AREA:
209     case ui::AX_ROLE_TEXT_FIELD:
210       class_name = "android.widget.EditText";
211       break;
212     case ui::AX_ROLE_SLIDER:
213       class_name = "android.widget.SeekBar";
214       break;
215     case ui::AX_ROLE_COMBO_BOX:
216       class_name = "android.widget.Spinner";
217       break;
218     case ui::AX_ROLE_BUTTON:
219     case ui::AX_ROLE_MENU_BUTTON:
220     case ui::AX_ROLE_POP_UP_BUTTON:
221       class_name = "android.widget.Button";
222       break;
223     case ui::AX_ROLE_CHECK_BOX:
224       class_name = "android.widget.CheckBox";
225       break;
226     case ui::AX_ROLE_RADIO_BUTTON:
227       class_name = "android.widget.RadioButton";
228       break;
229     case ui::AX_ROLE_TOGGLE_BUTTON:
230       class_name = "android.widget.ToggleButton";
231       break;
232     case ui::AX_ROLE_CANVAS:
233     case ui::AX_ROLE_IMAGE:
234       class_name = "android.widget.Image";
235       break;
236     case ui::AX_ROLE_PROGRESS_INDICATOR:
237       class_name = "android.widget.ProgressBar";
238       break;
239     case ui::AX_ROLE_TAB_LIST:
240       class_name = "android.widget.TabWidget";
241       break;
242     case ui::AX_ROLE_GRID:
243     case ui::AX_ROLE_TABLE:
244       class_name = "android.widget.GridView";
245       break;
246     case ui::AX_ROLE_LIST:
247     case ui::AX_ROLE_LIST_BOX:
248       class_name = "android.widget.ListView";
249       break;
250     case ui::AX_ROLE_DIALOG:
251       class_name = "android.app.Dialog";
252       break;
253     case ui::AX_ROLE_ROOT_WEB_AREA:
254       class_name = "android.webkit.WebView";
255       break;
256     default:
257       class_name = "android.view.View";
258       break;
259   }
260
261   return class_name;
262 }
263
264 base::string16 BrowserAccessibilityAndroid::GetText() const {
265   if (IsIframe() ||
266       role() == ui::AX_ROLE_WEB_AREA) {
267     return base::string16();
268   }
269
270   base::string16 description = GetString16Attribute(
271       ui::AX_ATTR_DESCRIPTION);
272   base::string16 text;
273   if (!name().empty())
274     text = base::UTF8ToUTF16(name());
275   else if (!description.empty())
276     text = description;
277   else if (!value().empty())
278     text = base::UTF8ToUTF16(value());
279
280   // This is called from PlatformIsLeaf, so don't call PlatformChildCount
281   // from within this!
282   if (text.empty() && HasOnlyStaticTextChildren()) {
283     for (uint32 i = 0; i < child_count(); i++) {
284       BrowserAccessibility* child = children()[i];
285       text += static_cast<BrowserAccessibilityAndroid*>(child)->GetText();
286     }
287   }
288
289   switch(role()) {
290     case ui::AX_ROLE_HEADING:
291       // Only append "heading" if this node already has text.
292       if (!text.empty())
293         text += base::ASCIIToUTF16(" Heading");
294       break;
295   }
296
297   return text;
298 }
299
300 int BrowserAccessibilityAndroid::GetItemIndex() const {
301   int index = 0;
302   switch(role()) {
303     case ui::AX_ROLE_LIST_ITEM:
304     case ui::AX_ROLE_LIST_BOX_OPTION:
305     case ui::AX_ROLE_TREE_ITEM:
306       index = index_in_parent();
307       break;
308     case ui::AX_ROLE_SLIDER:
309     case ui::AX_ROLE_PROGRESS_INDICATOR: {
310       float value_for_range;
311       if (GetFloatAttribute(
312               ui::AX_ATTR_VALUE_FOR_RANGE, &value_for_range)) {
313         index = static_cast<int>(value_for_range);
314       }
315       break;
316     }
317   }
318   return index;
319 }
320
321 int BrowserAccessibilityAndroid::GetItemCount() const {
322   int count = 0;
323   switch(role()) {
324     case ui::AX_ROLE_LIST:
325     case ui::AX_ROLE_LIST_BOX:
326       count = PlatformChildCount();
327       break;
328     case ui::AX_ROLE_SLIDER:
329     case ui::AX_ROLE_PROGRESS_INDICATOR: {
330       float max_value_for_range;
331       if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE,
332                             &max_value_for_range)) {
333         count = static_cast<int>(max_value_for_range);
334       }
335       break;
336     }
337   }
338   return count;
339 }
340
341 int BrowserAccessibilityAndroid::GetScrollX() const {
342   int value = 0;
343   GetIntAttribute(ui::AX_ATTR_SCROLL_X, &value);
344   return value;
345 }
346
347 int BrowserAccessibilityAndroid::GetScrollY() const {
348   int value = 0;
349   GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &value);
350   return value;
351 }
352
353 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
354   int value = 0;
355   GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &value);
356   return value;
357 }
358
359 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
360   int value = 0;
361   GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX, &value);
362   return value;
363 }
364
365 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
366   size_t index = 0;
367   while (index < old_value_.length() &&
368          index < new_value_.length() &&
369          old_value_[index] == new_value_[index]) {
370     index++;
371   }
372   return index;
373 }
374
375 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
376   size_t old_len = old_value_.length();
377   size_t new_len = new_value_.length();
378   size_t left = 0;
379   while (left < old_len &&
380          left < new_len &&
381          old_value_[left] == new_value_[left]) {
382     left++;
383   }
384   size_t right = 0;
385   while (right < old_len &&
386          right < new_len &&
387          old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
388     right++;
389   }
390   return (new_len - left - right);
391 }
392
393 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
394   size_t old_len = old_value_.length();
395   size_t new_len = new_value_.length();
396   size_t left = 0;
397   while (left < old_len &&
398          left < new_len &&
399          old_value_[left] == new_value_[left]) {
400     left++;
401   }
402   size_t right = 0;
403   while (right < old_len &&
404          right < new_len &&
405          old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
406     right++;
407   }
408   return (old_len - left - right);
409 }
410
411 base::string16 BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
412   return old_value_;
413 }
414
415 int BrowserAccessibilityAndroid::GetSelectionStart() const {
416   int sel_start = 0;
417   GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START, &sel_start);
418   return sel_start;
419 }
420
421 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
422   int sel_end = 0;
423   GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end);
424   return sel_end;
425 }
426
427 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
428   return value().length();
429 }
430
431 int BrowserAccessibilityAndroid::AndroidInputType() const {
432   std::string html_tag = GetStringAttribute(
433       ui::AX_ATTR_HTML_TAG);
434   if (html_tag != "input")
435     return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
436
437   std::string type;
438   if (!GetHtmlAttribute("type", &type))
439     return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
440
441   if (type == "" || type == "text" || type == "search")
442     return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
443   else if (type == "date")
444     return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
445   else if (type == "datetime" || type == "datetime-local")
446     return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
447   else if (type == "email")
448     return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL;
449   else if (type == "month")
450     return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
451   else if (type == "number")
452     return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER;
453   else if (type == "password")
454     return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD;
455   else if (type == "tel")
456     return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE;
457   else if (type == "time")
458     return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME;
459   else if (type == "url")
460     return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI;
461   else if (type == "week")
462     return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
463
464   return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
465 }
466
467 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
468   std::string live = GetStringAttribute(
469       ui::AX_ATTR_LIVE_STATUS);
470   if (live == "polite")
471     return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE;
472   else if (live == "assertive")
473     return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE;
474   return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE;
475 }
476
477 int BrowserAccessibilityAndroid::AndroidRangeType() const {
478   return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT;
479 }
480
481 int BrowserAccessibilityAndroid::RowCount() const {
482   if (role() == ui::AX_ROLE_GRID ||
483       role() == ui::AX_ROLE_TABLE) {
484     return CountChildrenWithRole(ui::AX_ROLE_ROW);
485   }
486
487   if (role() == ui::AX_ROLE_LIST ||
488       role() == ui::AX_ROLE_LIST_BOX ||
489       role() == ui::AX_ROLE_TREE) {
490     return PlatformChildCount();
491   }
492
493   return 0;
494 }
495
496 int BrowserAccessibilityAndroid::ColumnCount() const {
497   if (role() == ui::AX_ROLE_GRID ||
498       role() == ui::AX_ROLE_TABLE) {
499     return CountChildrenWithRole(ui::AX_ROLE_COLUMN);
500   }
501   return 0;
502 }
503
504 int BrowserAccessibilityAndroid::RowIndex() const {
505   if (role() == ui::AX_ROLE_LIST_ITEM ||
506       role() == ui::AX_ROLE_LIST_BOX_OPTION ||
507       role() == ui::AX_ROLE_TREE_ITEM) {
508     return index_in_parent();
509   }
510
511   return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX);
512 }
513
514 int BrowserAccessibilityAndroid::RowSpan() const {
515   return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN);
516 }
517
518 int BrowserAccessibilityAndroid::ColumnIndex() const {
519   return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX);
520 }
521
522 int BrowserAccessibilityAndroid::ColumnSpan() const {
523   return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN);
524 }
525
526 float BrowserAccessibilityAndroid::RangeMin() const {
527   return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
528 }
529
530 float BrowserAccessibilityAndroid::RangeMax() const {
531   return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
532 }
533
534 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
535   return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
536 }
537
538 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
539   // This is called from PlatformIsLeaf, so don't call PlatformChildCount
540   // from within this!
541   for (uint32 i = 0; i < child_count(); i++) {
542     BrowserAccessibility* child = children()[i];
543     if (child->HasState(ui::AX_STATE_FOCUSABLE))
544       return true;
545     if (static_cast<BrowserAccessibilityAndroid*>(child)->HasFocusableChild())
546       return true;
547   }
548   return false;
549 }
550
551 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
552   // This is called from PlatformIsLeaf, so don't call PlatformChildCount
553   // from within this!
554   for (uint32 i = 0; i < child_count(); i++) {
555     BrowserAccessibility* child = children()[i];
556     if (child->role() != ui::AX_ROLE_STATIC_TEXT)
557       return false;
558   }
559   return true;
560 }
561
562 bool BrowserAccessibilityAndroid::IsIframe() const {
563   base::string16 html_tag = GetString16Attribute(
564       ui::AX_ATTR_HTML_TAG);
565   return html_tag == base::ASCIIToUTF16("iframe");
566 }
567
568 void BrowserAccessibilityAndroid::PostInitialize() {
569   BrowserAccessibility::PostInitialize();
570
571   if (IsEditableText()) {
572     if (base::UTF8ToUTF16(value()) != new_value_) {
573       old_value_ = new_value_;
574       new_value_ = base::UTF8ToUTF16(value());
575     }
576   }
577
578   if (role() == ui::AX_ROLE_ALERT && first_time_)
579     manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
580
581   base::string16 live;
582   if (GetString16Attribute(
583       ui::AX_ATTR_CONTAINER_LIVE_STATUS, &live)) {
584     NotifyLiveRegionUpdate(live);
585   }
586
587   first_time_ = false;
588 }
589
590 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
591     base::string16& aria_live) {
592   if (!EqualsASCII(aria_live, aria_strings::kAriaLivePolite) &&
593       !EqualsASCII(aria_live, aria_strings::kAriaLiveAssertive))
594     return;
595
596   base::string16 text = GetText();
597   if (cached_text_ != text) {
598     if (!text.empty()) {
599       manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW,
600                                          this);
601     }
602     cached_text_ = text;
603   }
604 }
605
606 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role) const {
607   int count = 0;
608   for (uint32 i = 0; i < PlatformChildCount(); i++) {
609     if (PlatformGetChild(i)->role() == role)
610       count++;
611   }
612   return count;
613 }
614
615 }  // namespace content