1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/accessibility/browser_accessibility_manager_android.h"
#include <cmath>
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/i18n/char_iterator.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/browser/accessibility/browser_accessibility_android.h"
#include "content/common/accessibility_messages.h"
#include "jni/BrowserAccessibilityManager_jni.h"
#include "ui/accessibility/ax_text_utils.h"
using base::android::AttachCurrentThread;
using base::android::ScopedJavaLocalRef;
namespace {
enum AndroidHtmlElementType {
HTML_ELEMENT_TYPE_SECTION,
HTML_ELEMENT_TYPE_LIST,
HTML_ELEMENT_TYPE_CONTROL,
HTML_ELEMENT_TYPE_ANY
};
// These are special unofficial strings sent from TalkBack/BrailleBack
// to jump to certain categories of web elements.
AndroidHtmlElementType HtmlElementTypeFromString(base::string16 element_type) {
if (element_type == base::ASCIIToUTF16("SECTION"))
return HTML_ELEMENT_TYPE_SECTION;
else if (element_type == base::ASCIIToUTF16("LIST"))
return HTML_ELEMENT_TYPE_LIST;
else if (element_type == base::ASCIIToUTF16("CONTROL"))
return HTML_ELEMENT_TYPE_CONTROL;
else
return HTML_ELEMENT_TYPE_ANY;
}
} // anonymous namespace
namespace content {
namespace aria_strings {
const char kAriaLivePolite[] = "polite";
const char kAriaLiveAssertive[] = "assertive";
}
// static
BrowserAccessibilityManager* BrowserAccessibilityManager::Create(
const ui::AXTreeUpdate& initial_tree,
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory) {
return new BrowserAccessibilityManagerAndroid(
ScopedJavaLocalRef<jobject>(), initial_tree, delegate, factory);
}
BrowserAccessibilityManagerAndroid*
BrowserAccessibilityManager::ToBrowserAccessibilityManagerAndroid() {
return static_cast<BrowserAccessibilityManagerAndroid*>(this);
}
BrowserAccessibilityManagerAndroid::BrowserAccessibilityManagerAndroid(
ScopedJavaLocalRef<jobject> content_view_core,
const ui::AXTreeUpdate& initial_tree,
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory)
: BrowserAccessibilityManager(delegate, factory) {
SetContentViewCore(content_view_core);
Initialize(initial_tree);
}
BrowserAccessibilityManagerAndroid::~BrowserAccessibilityManagerAndroid() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null())
return;
Java_BrowserAccessibilityManager_onNativeObjectDestroyed(env, obj.obj());
}
// static
ui::AXTreeUpdate BrowserAccessibilityManagerAndroid::GetEmptyDocument() {
ui::AXNodeData empty_document;
empty_document.id = 0;
empty_document.role = ui::AX_ROLE_ROOT_WEB_AREA;
empty_document.state = 1 << ui::AX_STATE_READ_ONLY;
ui::AXTreeUpdate update;
update.nodes.push_back(empty_document);
return update;
}
void BrowserAccessibilityManagerAndroid::SetContentViewCore(
ScopedJavaLocalRef<jobject> content_view_core) {
if (content_view_core.is_null())
return;
JNIEnv* env = AttachCurrentThread();
java_ref_ = JavaObjectWeakGlobalRef(
env, Java_BrowserAccessibilityManager_create(
env, reinterpret_cast<intptr_t>(this),
content_view_core.obj()).obj());
}
void BrowserAccessibilityManagerAndroid::NotifyAccessibilityEvent(
ui::AXEvent event_type,
BrowserAccessibility* node) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null())
return;
BrowserAccessibilityAndroid* android_node =
static_cast<BrowserAccessibilityAndroid*>(node);
if (event_type == ui::AX_EVENT_HIDE)
return;
if (event_type == ui::AX_EVENT_TREE_CHANGED)
return;
if (event_type == ui::AX_EVENT_HOVER) {
HandleHoverEvent(node);
return;
}
// Always send AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED to notify
// the Android system that the accessibility hierarchy rooted at this
// node has changed.
Java_BrowserAccessibilityManager_handleContentChanged(
env, obj.obj(), node->GetId());
switch (event_type) {
case ui::AX_EVENT_LOAD_COMPLETE:
Java_BrowserAccessibilityManager_handlePageLoaded(
env, obj.obj(), focus_->id());
break;
case ui::AX_EVENT_FOCUS:
Java_BrowserAccessibilityManager_handleFocusChanged(
env, obj.obj(), node->GetId());
break;
case ui::AX_EVENT_CHECKED_STATE_CHANGED:
Java_BrowserAccessibilityManager_handleCheckStateChanged(
env, obj.obj(), node->GetId());
break;
case ui::AX_EVENT_SCROLL_POSITION_CHANGED:
Java_BrowserAccessibilityManager_handleScrollPositionChanged(
env, obj.obj(), node->GetId());
break;
case ui::AX_EVENT_SCROLLED_TO_ANCHOR:
Java_BrowserAccessibilityManager_handleScrolledToAnchor(
env, obj.obj(), node->GetId());
break;
case ui::AX_EVENT_ALERT:
// An alert is a special case of live region. Fall through to the
// next case to handle it.
case ui::AX_EVENT_SHOW: {
// This event is fired when an object appears in a live region.
// Speak its text.
Java_BrowserAccessibilityManager_announceLiveRegionText(
env, obj.obj(),
base::android::ConvertUTF16ToJavaString(
env, android_node->GetText()).obj());
break;
}
case ui::AX_EVENT_TEXT_SELECTION_CHANGED:
Java_BrowserAccessibilityManager_handleTextSelectionChanged(
env, obj.obj(), node->GetId());
break;
case ui::AX_EVENT_TEXT_CHANGED:
case ui::AX_EVENT_VALUE_CHANGED:
if (node->IsEditableText() && GetFocus(GetRoot()) == node) {
Java_BrowserAccessibilityManager_handleEditableTextChanged(
env, obj.obj(), node->GetId());
} else if (android_node->IsSlider()) {
Java_BrowserAccessibilityManager_handleSliderChanged(
env, obj.obj(), node->GetId());
}
break;
default:
// There are some notifications that aren't meaningful on Android.
// It's okay to skip them.
break;
}
}
jint BrowserAccessibilityManagerAndroid::GetRootId(JNIEnv* env, jobject obj) {
return static_cast<jint>(GetRoot()->GetId());
}
jboolean BrowserAccessibilityManagerAndroid::IsNodeValid(
JNIEnv* env, jobject obj, jint id) {
return GetFromID(id) != NULL;
}
void BrowserAccessibilityManagerAndroid::HitTest(
JNIEnv* env, jobject obj, jint x, jint y) {
if (delegate())
delegate()->AccessibilityHitTest(gfx::Point(x, y));
}
jboolean BrowserAccessibilityManagerAndroid::IsEditableText(
JNIEnv* env, jobject obj, jint id) {
BrowserAccessibilityAndroid* node = static_cast<BrowserAccessibilityAndroid*>(
GetFromID(id));
if (!node)
return false;
return node->IsEditableText();
}
jint BrowserAccessibilityManagerAndroid::GetEditableTextSelectionStart(
JNIEnv* env, jobject obj, jint id) {
BrowserAccessibilityAndroid* node = static_cast<BrowserAccessibilityAndroid*>(
GetFromID(id));
if (!node)
return false;
return node->GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START);
}
jint BrowserAccessibilityManagerAndroid::GetEditableTextSelectionEnd(
JNIEnv* env, jobject obj, jint id) {
BrowserAccessibilityAndroid* node = static_cast<BrowserAccessibilityAndroid*>(
GetFromID(id));
if (!node)
return false;
return node->GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END);
}
jboolean BrowserAccessibilityManagerAndroid::PopulateAccessibilityNodeInfo(
JNIEnv* env, jobject obj, jobject info, jint id) {
BrowserAccessibilityAndroid* node = static_cast<BrowserAccessibilityAndroid*>(
GetFromID(id));
if (!node)
return false;
if (node->GetParent()) {
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoParent(
env, obj, info, node->GetParent()->GetId());
}
for (unsigned i = 0; i < node->PlatformChildCount(); ++i) {
Java_BrowserAccessibilityManager_addAccessibilityNodeInfoChild(
env, obj, info, node->InternalGetChild(i)->GetId());
}
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoBooleanAttributes(
env, obj, info,
id,
node->CanScrollForward(),
node->CanScrollBackward(),
node->IsCheckable(),
node->IsChecked(),
node->IsClickable(),
node->IsEditableText(),
node->IsEnabled(),
node->IsFocusable(),
node->IsFocused(),
node->IsPassword(),
node->IsScrollable(),
node->IsSelected(),
node->IsVisibleToUser());
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoClassName(
env, obj, info,
base::android::ConvertUTF8ToJavaString(env, node->GetClassName()).obj());
if (!node->IsPassword() ||
Java_BrowserAccessibilityManager_shouldExposePasswordText(env, obj)) {
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoContentDescription(
env, obj, info,
base::android::ConvertUTF16ToJavaString(env, node->GetText()).obj(),
node->IsLink());
}
gfx::Rect absolute_rect = node->GetLocalBoundsRect();
gfx::Rect parent_relative_rect = absolute_rect;
if (node->GetParent()) {
gfx::Rect parent_rect = node->GetParent()->GetLocalBoundsRect();
parent_relative_rect.Offset(-parent_rect.OffsetFromOrigin());
}
bool is_root = node->GetParent() == NULL;
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoLocation(
env, obj, info,
id,
absolute_rect.x(), absolute_rect.y(),
parent_relative_rect.x(), parent_relative_rect.y(),
absolute_rect.width(), absolute_rect.height(),
is_root);
// New KitKat APIs
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoKitKatAttributes(
env, obj, info,
node->CanOpenPopup(),
node->IsContentInvalid(),
node->IsDismissable(),
node->IsMultiLine(),
node->AndroidInputType(),
node->AndroidLiveRegionType());
if (node->IsCollection()) {
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionInfo(
env, obj, info,
node->RowCount(),
node->ColumnCount(),
node->IsHierarchical());
}
if (node->IsCollectionItem() || node->IsHeading()) {
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionItemInfo(
env, obj, info,
node->RowIndex(),
node->RowSpan(),
node->ColumnIndex(),
node->ColumnSpan(),
node->IsHeading());
}
if (node->IsRangeType()) {
Java_BrowserAccessibilityManager_setAccessibilityNodeInfoRangeInfo(
env, obj, info,
node->AndroidRangeType(),
node->RangeMin(),
node->RangeMax(),
node->RangeCurrentValue());
}
return true;
}
jboolean BrowserAccessibilityManagerAndroid::PopulateAccessibilityEvent(
JNIEnv* env, jobject obj, jobject event, jint id, jint event_type) {
BrowserAccessibilityAndroid* node = static_cast<BrowserAccessibilityAndroid*>(
GetFromID(id));
if (!node)
return false;
Java_BrowserAccessibilityManager_setAccessibilityEventBooleanAttributes(
env, obj, event,
node->IsChecked(),
node->IsEnabled(),
node->IsPassword(),
node->IsScrollable());
Java_BrowserAccessibilityManager_setAccessibilityEventClassName(
env, obj, event,
base::android::ConvertUTF8ToJavaString(env, node->GetClassName()).obj());
Java_BrowserAccessibilityManager_setAccessibilityEventListAttributes(
env, obj, event,
node->GetItemIndex(),
node->GetItemCount());
Java_BrowserAccessibilityManager_setAccessibilityEventScrollAttributes(
env, obj, event,
node->GetScrollX(),
node->GetScrollY(),
node->GetMaxScrollX(),
node->GetMaxScrollY());
switch (event_type) {
case ANDROID_ACCESSIBILITY_EVENT_TEXT_CHANGED: {
base::string16 before_text, text;
if (!node->IsPassword() ||
Java_BrowserAccessibilityManager_shouldExposePasswordText(env, obj)) {
before_text = node->GetTextChangeBeforeText();
text = node->GetText();
}
Java_BrowserAccessibilityManager_setAccessibilityEventTextChangedAttrs(
env, obj, event,
node->GetTextChangeFromIndex(),
node->GetTextChangeAddedCount(),
node->GetTextChangeRemovedCount(),
base::android::ConvertUTF16ToJavaString(
env, before_text).obj(),
base::android::ConvertUTF16ToJavaString(env, text).obj());
break;
}
case ANDROID_ACCESSIBILITY_EVENT_TEXT_SELECTION_CHANGED: {
base::string16 text;
if (!node->IsPassword() ||
Java_BrowserAccessibilityManager_shouldExposePasswordText(env, obj)) {
text = node->GetText();
}
Java_BrowserAccessibilityManager_setAccessibilityEventSelectionAttrs(
env, obj, event,
node->GetSelectionStart(),
node->GetSelectionEnd(),
node->GetEditableTextLength(),
base::android::ConvertUTF16ToJavaString(env, text).obj());
break;
}
default:
break;
}
// Backwards-compatible fallback for new KitKat APIs.
Java_BrowserAccessibilityManager_setAccessibilityEventKitKatAttributes(
env, obj, event,
node->CanOpenPopup(),
node->IsContentInvalid(),
node->IsDismissable(),
node->IsMultiLine(),
node->AndroidInputType(),
node->AndroidLiveRegionType());
if (node->IsCollection()) {
Java_BrowserAccessibilityManager_setAccessibilityEventCollectionInfo(
env, obj, event,
node->RowCount(),
node->ColumnCount(),
node->IsHierarchical());
}
if (node->IsHeading()) {
Java_BrowserAccessibilityManager_setAccessibilityEventHeadingFlag(
env, obj, event, true);
}
if (node->IsCollectionItem()) {
Java_BrowserAccessibilityManager_setAccessibilityEventCollectionItemInfo(
env, obj, event,
node->RowIndex(),
node->RowSpan(),
node->ColumnIndex(),
node->ColumnSpan());
}
if (node->IsRangeType()) {
Java_BrowserAccessibilityManager_setAccessibilityEventRangeInfo(
env, obj, event,
node->AndroidRangeType(),
node->RangeMin(),
node->RangeMax(),
node->RangeCurrentValue());
}
return true;
}
void BrowserAccessibilityManagerAndroid::Click(
JNIEnv* env, jobject obj, jint id) {
BrowserAccessibility* node = GetFromID(id);
if (node)
DoDefaultAction(*node);
}
void BrowserAccessibilityManagerAndroid::Focus(
JNIEnv* env, jobject obj, jint id) {
BrowserAccessibility* node = GetFromID(id);
if (node)
SetFocus(node, true);
}
void BrowserAccessibilityManagerAndroid::Blur(JNIEnv* env, jobject obj) {
SetFocus(GetRoot(), true);
}
void BrowserAccessibilityManagerAndroid::ScrollToMakeNodeVisible(
JNIEnv* env, jobject obj, jint id) {
BrowserAccessibility* node = GetFromID(id);
if (node)
ScrollToMakeVisible(*node, gfx::Rect(node->GetLocation().size()));
}
void BrowserAccessibilityManagerAndroid::SetTextFieldValue(
JNIEnv* env, jobject obj, jint id, jstring value) {
BrowserAccessibility* node = GetFromID(id);
if (node) {
BrowserAccessibilityManager::SetValue(
*node, base::android::ConvertJavaStringToUTF16(env, value));
}
}
void BrowserAccessibilityManagerAndroid::SetSelection(
JNIEnv* env, jobject obj, jint id, jint start, jint end) {
BrowserAccessibility* node = GetFromID(id);
if (node)
SetTextSelection(*node, start, end);
}
jboolean BrowserAccessibilityManagerAndroid::AdjustSlider(
JNIEnv* env, jobject obj, jint id, jboolean increment) {
BrowserAccessibility* node = GetFromID(id);
if (!node)
return false;
BrowserAccessibilityAndroid* android_node =
static_cast<BrowserAccessibilityAndroid*>(node);
if (!android_node->IsSlider() || !android_node->IsEnabled())
return false;
float value = node->GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
float min = node->GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
float max = node->GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
if (max <= min)
return false;
// To behave similarly to an Android SeekBar, move by an increment of
// approximately 20%.
float original_value = value;
float delta = (max - min) / 5.0f;
value += (increment ? delta : -delta);
value = std::max(std::min(value, max), min);
if (value != original_value) {
BrowserAccessibilityManager::SetValue(
*node, base::UTF8ToUTF16(base::DoubleToString(value)));
return true;
}
return false;
}
void BrowserAccessibilityManagerAndroid::HandleHoverEvent(
BrowserAccessibility* node) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null())
return;
BrowserAccessibilityAndroid* ancestor =
static_cast<BrowserAccessibilityAndroid*>(node->GetParent());
while (ancestor && ancestor != GetRoot()) {
if (ancestor->PlatformIsLeaf() ||
(ancestor->IsFocusable() && !ancestor->HasFocusableChild())) {
node = ancestor;
// Don't break - we want the highest ancestor that's focusable or a
// leaf node.
}
ancestor = static_cast<BrowserAccessibilityAndroid*>(ancestor->GetParent());
}
Java_BrowserAccessibilityManager_handleHover(
env, obj.obj(), node->GetId());
}
jint BrowserAccessibilityManagerAndroid::FindElementType(
JNIEnv* env, jobject obj, jint start_id, jstring element_type_str,
jboolean forwards) {
BrowserAccessibility* node = GetFromID(start_id);
if (!node)
return 0;
AndroidHtmlElementType element_type = HtmlElementTypeFromString(
base::android::ConvertJavaStringToUTF16(env, element_type_str));
node = forwards ? NextInTreeOrder(node) : PreviousInTreeOrder(node);
while (node) {
switch(element_type) {
case HTML_ELEMENT_TYPE_SECTION:
if (node->GetRole() == ui::AX_ROLE_ARTICLE ||
node->GetRole() == ui::AX_ROLE_APPLICATION ||
node->GetRole() == ui::AX_ROLE_BANNER ||
node->GetRole() == ui::AX_ROLE_COMPLEMENTARY ||
node->GetRole() == ui::AX_ROLE_CONTENT_INFO ||
node->GetRole() == ui::AX_ROLE_HEADING ||
node->GetRole() == ui::AX_ROLE_MAIN ||
node->GetRole() == ui::AX_ROLE_NAVIGATION ||
node->GetRole() == ui::AX_ROLE_SEARCH ||
node->GetRole() == ui::AX_ROLE_REGION) {
return node->GetId();
}
break;
case HTML_ELEMENT_TYPE_LIST:
if (node->GetRole() == ui::AX_ROLE_LIST ||
node->GetRole() == ui::AX_ROLE_GRID ||
node->GetRole() == ui::AX_ROLE_TABLE ||
node->GetRole() == ui::AX_ROLE_TREE) {
return node->GetId();
}
break;
case HTML_ELEMENT_TYPE_CONTROL:
if (static_cast<BrowserAccessibilityAndroid*>(node)->IsFocusable())
return node->GetId();
break;
case HTML_ELEMENT_TYPE_ANY:
// In theory, the API says that an accessibility service could
// jump to an element by element name, like 'H1' or 'P'. This isn't
// currently used by any accessibility service, and we think it's
// better to keep them high-level like 'SECTION' or 'CONTROL', so we
// just fall back on linear navigation when we don't recognize the
// element type.
if (static_cast<BrowserAccessibilityAndroid*>(node)->IsClickable())
return node->GetId();
break;
}
node = forwards ? NextInTreeOrder(node) : PreviousInTreeOrder(node);
}
return 0;
}
jboolean BrowserAccessibilityManagerAndroid::NextAtGranularity(
JNIEnv* env, jobject obj, jint granularity, jboolean extend_selection,
jint id, jint cursor_index) {
BrowserAccessibilityAndroid* node = static_cast<BrowserAccessibilityAndroid*>(
GetFromID(id));
if (!node)
return false;
jint start_index = -1;
int end_index = -1;
if (NextAtGranularity(granularity, cursor_index, node,
&start_index, &end_index)) {
base::string16 text;
if (!node->IsPassword() ||
Java_BrowserAccessibilityManager_shouldExposePasswordText(env, obj)) {
text = node->GetText();
}
Java_BrowserAccessibilityManager_finishGranularityMove(
env, obj, base::android::ConvertUTF16ToJavaString(
env, text).obj(),
extend_selection, start_index, end_index, true);
return true;
}
return false;
}
jboolean BrowserAccessibilityManagerAndroid::PreviousAtGranularity(
JNIEnv* env, jobject obj, jint granularity, jboolean extend_selection,
jint id, jint cursor_index) {
BrowserAccessibilityAndroid* node = static_cast<BrowserAccessibilityAndroid*>(
GetFromID(id));
if (!node)
return false;
jint start_index = -1;
int end_index = -1;
if (PreviousAtGranularity(granularity, cursor_index, node,
&start_index, &end_index)) {
Java_BrowserAccessibilityManager_finishGranularityMove(
env, obj, base::android::ConvertUTF16ToJavaString(
env, node->GetText()).obj(),
extend_selection, start_index, end_index, false);
return true;
}
return false;
}
bool BrowserAccessibilityManagerAndroid::NextAtGranularity(
int32 granularity, int32 cursor_index,
BrowserAccessibilityAndroid* node, int32* start_index, int32* end_index) {
switch (granularity) {
case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_CHARACTER: {
base::string16 text = node->GetText();
if (cursor_index >= static_cast<int32>(text.length()))
return false;
base::i18n::UTF16CharIterator iter(text.data(), text.size());
while (!iter.end() && iter.array_pos() <= cursor_index)
iter.Advance();
*start_index = iter.array_pos();
*end_index = iter.array_pos();
break;
}
case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD:
case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE: {
std::vector<int32> starts;
std::vector<int32> ends;
node->GetGranularityBoundaries(granularity, &starts, &ends, 0);
if (starts.size() == 0)
return false;
size_t index = 0;
while (index < starts.size() - 1 && starts[index] < cursor_index)
index++;
if (starts[index] < cursor_index)
return false;
*start_index = starts[index];
*end_index = ends[index];
break;
}
default:
NOTREACHED();
}
return true;
}
bool BrowserAccessibilityManagerAndroid::PreviousAtGranularity(
int32 granularity, int32 cursor_index,
BrowserAccessibilityAndroid* node, int32* start_index, int32* end_index) {
switch (granularity) {
case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_CHARACTER: {
if (cursor_index <= 0)
return false;
base::string16 text = node->GetText();
base::i18n::UTF16CharIterator iter(text.data(), text.size());
int previous_index = 0;
while (!iter.end() && iter.array_pos() < cursor_index) {
previous_index = iter.array_pos();
iter.Advance();
}
*start_index = previous_index;
*end_index = previous_index;
break;
}
case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD:
case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE: {
std::vector<int32> starts;
std::vector<int32> ends;
node->GetGranularityBoundaries(granularity, &starts, &ends, 0);
if (starts.size() == 0)
return false;
size_t index = starts.size() - 1;
while (index > 0 && starts[index] >= cursor_index)
index--;
if (starts[index] >= cursor_index)
return false;
*start_index = starts[index];
*end_index = ends[index];
break;
}
default:
NOTREACHED();
}
return true;
}
void BrowserAccessibilityManagerAndroid::SetAccessibilityFocus(
JNIEnv* env, jobject obj, jint id) {
if (delegate_)
delegate_->AccessibilitySetAccessibilityFocus(id);
}
void BrowserAccessibilityManagerAndroid::OnRootChanged(ui::AXNode* new_root) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null())
return;
Java_BrowserAccessibilityManager_handleNavigate(env, obj.obj());
}
bool
BrowserAccessibilityManagerAndroid::UseRootScrollOffsetsWhenComputingBounds() {
// The Java layer handles the root scroll offset.
return false;
}
bool RegisterBrowserAccessibilityManager(JNIEnv* env) {
return RegisterNativesImpl(env);
}
} // namespace content
|