Code example for SparseArray

Methods: indexOfKeyput

0
    static final int SELECTOR_CONTAINER = 20;
    static final int SELECTOR_PATTERN = 21;
    static final int SELECTOR_PARENT = 22;
    static final int SELECTOR_COUNT = 23;
 
    private SparseArray<Object> mSelectorAttributes = new SparseArray<Object>();
    public static final String LOG_TAG = "ByClass";
 
    public UiSelector() { 
    } 
 
    protected UiSelector(UiSelector by) {
        mSelectorAttributes = by.cloneSelectors().mSelectorAttributes;
    } 
 
    protected UiSelector cloneSelectors() { 
        UiSelector ret = new UiSelector();
        ret.mSelectorAttributes = mSelectorAttributes.clone();
        return ret;
    } 
 
    static UiSelector patternBuilder(UiSelector selector) {
        if(!selector.hasPatternSelector()) {
            return new UiSelector().patternSelector(selector);
        } 
        return selector;
    } 
 
    static UiSelector patternBuilder(UiSelector container, UiSelector pattern) {
        return new UiSelector( 
                new UiSelector().containerSelector(container).patternSelector(pattern));
    } 
 
    public UiSelector text(String text) {
        return buildSelector(SELECTOR_TEXT, text);
    } 
 
    public UiSelector textStartsWith(String text) {
        return buildSelector(SELECTOR_START_TEXT, text);
    } 
 
    public UiSelector textContains(String text) {
        return buildSelector(SELECTOR_CONTAINS_TEXT, text);
    } 
 
    public UiSelector className(String className) {
        return buildSelector(SELECTOR_CLASS, className);
    } 
 
    public UiSelector description(String desc) {
        return buildSelector(SELECTOR_DESCRIPTION, desc);
    } 
 
    public UiSelector descriptionStartsWith(String desc) {
        return buildSelector(SELECTOR_START_DESCRIPTION, desc);
    } 
 
    public UiSelector descriptionContains(String desc) {
        return buildSelector(SELECTOR_CONTAINS_DESCRIPTION, desc);
    } 
 
    public UiSelector index(final int index) {
        return buildSelector(SELECTOR_INDEX, index);
    } 
 
    public UiSelector instance(final int instance) {
        return buildSelector(SELECTOR_INSTANCE, instance);
    } 
 
    public UiSelector enabled(boolean val) {
        return buildSelector(SELECTOR_ENABLED, val);
    } 
 
    public UiSelector focused(boolean val) {
        return buildSelector(SELECTOR_FOCUSED, val);
    } 
 
    public UiSelector focusable(boolean val) {
        return buildSelector(SELECTOR_FOCUSABLE, val);
    } 
 
    public UiSelector scrollable(boolean val) {
        return buildSelector(SELECTOR_SCROLLABLE, val);
    } 
 
    public UiSelector selected(boolean val) {
        return buildSelector(SELECTOR_SELECTED, val);
    } 
 
    public UiSelector checked(boolean val) {
        return buildSelector(SELECTOR_CHECKED, val);
    } 
 
    public UiSelector clickable(boolean val) {
        return buildSelector(SELECTOR_CLICKABLE, val);
    } 
 
    public UiSelector childSelector(UiSelector by) {
        return buildSelector(SELECTOR_CHILD, by);
    } 
 
    private UiSelector patternSelector(UiSelector by) {
        return buildSelector(SELECTOR_PATTERN, by);
    } 
 
    private UiSelector containerSelector(UiSelector by) {
        return buildSelector(SELECTOR_CONTAINER, by);
    } 
 
    public UiSelector fromParent(UiSelector by) {
        return buildSelector(SELECTOR_PARENT, by);
    } 
 
    public UiSelector packageName(String name) {
        return buildSelector(SELECTOR_PACKAGE_NAME, name);
    } 
 
    /** 
     * Building a By selector always returns a new By selector and never modifies the 
     * existing By selector being used. For example a test library have predefined 
     * By selectors SA, SB, SC etc. The test may decide that it needs a By selector SX in the 
     * context of SB. It can use SX = SB.critereon(x) to generate SX without modifying the 
     * state of SB which is expected to be something else. For this we will return a new 
     * By selector every time. 
     */ 
    private UiSelector buildSelector(int selectorId, Object selectorValue) {
        UiSelector by = new UiSelector(this);
        if(selectorId == SELECTOR_CHILD || selectorId == SELECTOR_PARENT)
            by.getLastSubSelector().mSelectorAttributes.put(selectorId, selectorValue);
        else 
            by.mSelectorAttributes.put(selectorId, selectorValue);
        return by;
    } 
 
    /** 
     * Selectors may have a hierarchy defined by specifying child nodes to be matched. 
     * It is not necessary that every selector have more than one level. A selector 
     * can also be a single level referencing only one node. In such cases the return 
     * it null. 
     * @return a child selector if one exists. Else null if this selector does not 
     * reference child node. 
     */ 
 
    UiSelector getChildSelector() { 
        UiSelector by = (UiSelector)mSelectorAttributes.get(UiSelector.SELECTOR_CHILD, null);
        if(by != null)
            return new UiSelector(by);
        return null; 
    } 
 
    UiSelector getPatternSelector() { 
        UiSelector by = (UiSelector)mSelectorAttributes.get(UiSelector.SELECTOR_PATTERN, null);
        if(by != null)
            return new UiSelector(by);
        return null; 
    } 
 
    UiSelector getContainerSelector() { 
        UiSelector by = (UiSelector)mSelectorAttributes.get(UiSelector.SELECTOR_CONTAINER, null);
        if(by != null)
            return new UiSelector(by);
        return null; 
    } 
 
    UiSelector getParentSelector() { 
        UiSelector by = (UiSelector) mSelectorAttributes.get(UiSelector.SELECTOR_PARENT, null);
        if(by != null)
            return new UiSelector(by);
        return null; 
    } 
 
    int getInstance() { 
        return getInt(UiSelector.SELECTOR_INSTANCE);
    } 
 
    String getString(int criterion) {
        return (String) mSelectorAttributes.get(criterion, null);
    } 
 
    boolean getBoolean(int criterion) {
        return (Boolean) mSelectorAttributes.get(criterion, false);
    } 
 
    int getInt(int criterion) {
        return (Integer) mSelectorAttributes.get(criterion, 0);
    } 
 
    boolean isMatchFor(AccessibilityNodeInfo node, int index) {
        int size = mSelectorAttributes.size();
        for(int x = 0; x < size; x++) {
            CharSequence s = null;
            int criterion = mSelectorAttributes.keyAt(x);
            switch(criterion) {
            case UiSelector.SELECTOR_INDEX:
                if(index != this.getInt(criterion))
                    return false; 
                break; 
            case UiSelector.SELECTOR_CHECKED:
                if (node.isChecked() != getBoolean(criterion)) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_CLASS:
                s = node.getClassName();
                if (s == null || !s.toString().contentEquals(getString(criterion))) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_CLICKABLE:
                if (node.isClickable() != getBoolean(criterion)) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_CONTAINS_DESCRIPTION:
                s = node.getContentDescription();
                if(s == null || !s.toString().toLowerCase()
                        .contains(getString(criterion).toLowerCase())) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_START_DESCRIPTION:
                s = node.getContentDescription();
                if(s == null || !s.toString().toLowerCase()
                        .startsWith(getString(criterion).toLowerCase())) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_DESCRIPTION:
                s = node.getContentDescription();
                if(s == null || !s.toString().contentEquals(getString(criterion))) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_CONTAINS_TEXT:
                s = node.getText();
                if(s == null || !s.toString().toLowerCase()
                        .contains(getString(criterion).toLowerCase())) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_START_TEXT:
                s = node.getText();
                if(s == null || !s.toString().toLowerCase()
                        .startsWith(getString(criterion).toLowerCase())) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_TEXT:
                s = node.getText();
                if(s == null || !s.toString().contentEquals(getString(criterion))) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_ENABLED:
                if(node.isEnabled() != getBoolean(criterion)) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_FOCUSABLE:
                if(node.isFocusable() != getBoolean(criterion)) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_FOCUSED:
                if(node.isFocused() != getBoolean(criterion)) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_ID:
                break; //TODO: do we need this for AccessibilityNodeInfo.id? 
            case UiSelector.SELECTOR_PACKAGE_NAME:
                s = node.getPackageName();
                if(s == null || !s.toString().contentEquals(getString(criterion))) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_SCROLLABLE:
                if(node.isScrollable() != getBoolean(criterion)) {
                    return false; 
                } 
                break; 
            case UiSelector.SELECTOR_SELECTED:
                if(node.isSelected() != getBoolean(criterion)) {
                    return false; 
                } 
                break; 
            } 
        } 
        return matchOrUpdateInstance(); 
    } 
 
    private boolean matchOrUpdateInstance() { 
        int currentSelectorCounter = 0;
        int currentSelectorInstance = 0;
 
        // matched attributes - now check for matching instance number 
        if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_INSTANCE) > 0) {
            currentSelectorInstance =
                    (Integer)mSelectorAttributes.get(UiSelector.SELECTOR_INSTANCE);
        } 
 
        // instance is required. Add count if not already counting 
        if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_COUNT) > 0) {
            currentSelectorCounter = (Integer)mSelectorAttributes.get(UiSelector.SELECTOR_COUNT);
        } 
 
        // Verify 
        if (currentSelectorInstance == currentSelectorCounter) {
            return true; 
        } 
        // Update count 
        if (currentSelectorInstance > currentSelectorCounter) {
            mSelectorAttributes.put(UiSelector.SELECTOR_COUNT, ++currentSelectorCounter);
        } 
        return false; 
    } 
 
    /** 
     * Leaf selector indicates no more child or parent selectors 
     * are declared in the this selector. 
     * @return true if is leaf. 
     */ 
    boolean isLeaf() { 
        if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_CHILD) < 0 &&
                mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_PARENT) < 0) {
            return true; 
        } 
        return false; 
    } 
 
    boolean hasChildSelector() { 
        if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_CHILD) < 0) {
            return false; 
        } 
        return true; 
    } 
 
    boolean hasPatternSelector() { 
        if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_PATTERN) < 0) {
            return false; 
        } 
        return true; 
    } 
 
    boolean hasContainerSelector() { 
        if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_CONTAINER) < 0) {
            return false; 
        } 
        return true; 
    } 
 
    boolean hasParentSelector() { 
        if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_PARENT) < 0) {
            return false; 
        } 
        return true; 
    } 
 
    /** 
     * Returns the deepest selector in the chain of possible sub selectors. 
     * A chain of selector is created when either of {@link UiSelector#childSelector(UiSelector)} 
     * or {@link UiSelector#fromParent(UiSelector)} are used once or more in the construction of 
     * a selector. 
     * @return last By selector in chain 
     */ 
    private UiSelector getLastSubSelector() { 
        if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_CHILD) >= 0) {
            UiSelector child = (UiSelector)mSelectorAttributes.get(UiSelector.SELECTOR_CHILD);
            if(child.getLastSubSelector() == null) {
                return child;
            } 
            return child.getLastSubSelector();
        } else if(mSelectorAttributes.indexOfKey(UiSelector.SELECTOR_PARENT) >= 0) {
            UiSelector parent = (UiSelector)mSelectorAttributes.get(UiSelector.SELECTOR_PARENT);
            if(parent.getLastSubSelector() == null) {
                return parent;
            } 
            return parent.getLastSubSelector();
Experience pair programming with AI  Get Codota for Java