aboutsummaryrefslogtreecommitdiff
path: root/TestBack/src/foo/bar/testback/AccessibilityFocusManager.java
blob: c6b91ae64e2b5fcb7c16b7281c3126e5fc8b3907 (plain)
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
/*
 * Copyright 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package foo.bar.testback;

import static foo.bar.testback.AccessibilityNodeInfoUtils.findParent;

import android.text.TextUtils;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * Helper class to manage accessibility focus
 */
public class AccessibilityFocusManager {
    private static final AccessibilityAction[] IGNORED_ACTIONS_FOR_A11Y_FOCUS = {
            AccessibilityAction.ACTION_ACCESSIBILITY_FOCUS,
            AccessibilityAction.ACTION_CLEAR_ACCESSIBILITY_FOCUS,
            AccessibilityAction.ACTION_SELECT,
            AccessibilityAction.ACTION_CLEAR_SELECTION,
            AccessibilityAction.ACTION_SHOW_ON_SCREEN
    };

    public static final boolean canTakeAccessibilityFocus(AccessibilityNodeInfo nodeInfo) {
        if (nodeInfo.isFocusable() || nodeInfo.isScreenReaderFocusable()) {
            return true;
        }

        List<AccessibilityAction> actions = new ArrayList<>(nodeInfo.getActionList());
        actions.removeAll(Arrays.asList(IGNORED_ACTIONS_FOR_A11Y_FOCUS));

        // Nodes with relevant actions are always focusable
        if (!actions.isEmpty()) {
            return true;
        }

        // If a parent is specifically marked focusable, then this node should not be.
        if (findParent(nodeInfo,
                (parent) -> parent.isFocusable() || parent.isScreenReaderFocusable()) != null) {
            return false;
        }

        return !TextUtils.isEmpty(nodeInfo.getText())
                || !TextUtils.isEmpty(nodeInfo.getContentDescription());
    };
}