docs: Modernized AccessibilityNodeProvider example
Uses a delegate and more realistic method contents. Also added
language switcher for Kotlin-based and Java-based code.
Test: make ds-docs -j32
Bug: 138849874
Change-Id: Id31cf944f4adab2f09cf5004d68434b445da6eeb
diff --git a/core/java/android/view/accessibility/AccessibilityNodeProvider.java b/core/java/android/view/accessibility/AccessibilityNodeProvider.java
index 4b25378..f4c7b96 100644
--- a/core/java/android/view/accessibility/AccessibilityNodeProvider.java
+++ b/core/java/android/view/accessibility/AccessibilityNodeProvider.java
@@ -44,28 +44,126 @@
* View itself. Similarly the returned instance is responsible for performing accessibility
* actions on any virtual view or the root view itself. For example:
* </p>
- * <pre>
- * getAccessibilityNodeProvider(
- * if (mAccessibilityNodeProvider == null) {
- * mAccessibilityNodeProvider = new AccessibilityNodeProvider() {
- * public boolean performAction(int action, int virtualDescendantId) {
- * // Implementation.
+ * <div>
+ * <div class="ds-selector-tabs"><section><h3 id="kotlin">Kotlin</h3>
+ * <pre class="prettyprint lang-kotlin">
+ * // "view" is the View instance on which this class performs accessibility functions.
+ * class MyCalendarViewAccessibilityDelegate(
+ * private var view: MyCalendarView) : AccessibilityDelegate() {
+ * override fun getAccessibilityNodeProvider(host: View): AccessibilityNodeProvider {
+ * return object : AccessibilityNodeProvider() {
+ * override fun createAccessibilityNodeInfo(virtualViewId: Int):
+ * AccessibilityNodeInfo? {
+ * when (virtualViewId) {
+ * <var>host-view-id</var> -> {
+ * val node = AccessibilityNodeInfo.obtain(view)
+ * node.addChild(view, <var>child-view-id</var>)
+ * // Set other attributes like screenReaderFocusable
+ * // and contentDescription.
+ * return node
+ * }
+ * <var>child-view-id</var> -> {
+ * val node = AccessibilityNodeInfo
+ * .obtain(view, virtualViewId)
+ * node.setParent(view)
+ * node.addAction(ACTION_SCROLL_UP)
+ * node.addAction(ACTION_SCROLL_DOWN)
+ * // Set other attributes like focusable and visibleToUser.
+ * node.setBoundsInScreen(
+ * Rect(<var>coords-of-edges-relative-to-screen</var>))
+ * return node
+ * }
+ * else -> return null
+ * }
+ * }
+ *
+ * override fun performAction(
+ * virtualViewId: Int,
+ * action: Int,
+ * arguments: Bundle
+ * ): Boolean {
+ * if (virtualViewId == <var>host-view-id</var>) {
+ * return view.performAccessibilityAction(action, arguments)
+ * }
+ * when (action) {
+ * ACTION_SCROLL_UP.id -> {
+ * // Implement logic in a separate method.
+ * navigateToPreviousMonth()
+ *
+ * return true
+ * }
+ * ACTION_SCROLL_DOWN.id ->
+ * // Implement logic in a separate method.
+ * navigateToNextMonth()
+ *
+ * return true
+ * else -> return false
+ * }
+ * }
+ * }
+ * }
+ * }
+ * </pre>
+ * </section><section><h3 id="java">Java</h3>
+ * <pre class="prettyprint lang-java">
+ * final class MyCalendarViewAccessibilityDelegate extends AccessibilityDelegate {
+ * // The View instance on which this class performs accessibility functions.
+ * private final MyCalendarView view;
+ *
+ * MyCalendarViewAccessibilityDelegate(MyCalendarView view) {
+ * this.view = view;
+ * }
+ *
+ * @Override
+ * public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
+ * return new AccessibilityNodeProvider() {
+ * @Override
+ * @Nullable
+ * public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) {
+ * if (virtualViewId == <var>host-view-id</var>) {
+ * AccessibilityNodeInfo node = AccessibilityNodeInfo.obtain(view);
+ * node.addChild(view, <var>child-view-id</var>);
+ * // Set other attributes like screenReaderFocusable and contentDescription.
+ * return node;
+ * } else if (virtualViewId == <var>child-view-id</var>) {
+ * AccessibilityNodeInfo node =
+ * AccessibilityNodeInfo.obtain(view, virtualViewId);
+ * node.setParent(view);
+ * node.addAction(ACTION_SCROLL_UP);
+ * node.addAction(ACTION_SCROLL_DOWN);
+ * // Set other attributes like focusable and visibleToUser.
+ * node.setBoundsInScreen(
+ * new Rect(<var>coordinates-of-edges-relative-to-screen</var>));
+ * return node;
+ * } else {
+ * return null;
+ * }
+ * }
+ *
+ * @Override
+ * public boolean performAction(int virtualViewId, int action, Bundle arguments) {
+ * if (virtualViewId == <var>host-view-id</var>) {
+ * return view.performAccessibilityAction(action, arguments);
+ * }
+ *
+ * if (action == ACTION_SCROLL_UP.getId()) {
+ * // Implement logic in a separate method.
+ * navigateToPreviousMonth();
+ *
+ * return true;
+ * } else if (action == ACTION_SCROLL_DOWN.getId()) {
+ * // Implement logic in a separate method.
+ * navigateToNextMonth();
+ *
+ * return true;
+ * } else {
* return false;
* }
- *
- * public List<AccessibilityNodeInfo> findAccessibilityNodeInfosByText(String text,
- * int virtualDescendantId) {
- * // Implementation.
- * return null;
- * }
- *
- * public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualDescendantId) {
- * // Implementation.
- * return null;
- * }
- * });
- * return mAccessibilityNodeProvider;
- * </pre>
+ * }
+ * };
+ * }
+ * }
+ * </pre></section></div></div>
*/
public abstract class AccessibilityNodeProvider {