下面列出了android.view.accessibility.AccessibilityNodeInfo#isChecked ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Check if the UI element's <code>checked</code> property is currently true
*
* @return true if it is else false
* @since API Level 16
*/
public boolean isChecked() throws UiObjectNotFoundException {
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(mUiSelector.toString());
}
return node.isChecked();
}
/**
* Check if the UI element's <code>checked</code> property is currently true
*
* @return true if it is else false
* @since API Level 16
*/
public boolean isChecked() throws UiObjectNotFoundException {
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(mUiSelector.toString());
}
return node.isChecked();
}
public static void dumpNode(AccessibilityNodeInfo info, Node root,
int index, int width, int height) {
root.sourceId = info.getSourceNodeId();
root.index = index;
root.text = safeCharSeqToString(info.getText());
root.res = safeCharSeqToString(info.getViewIdResourceName());
root.clazz = safeCharSeqToString(info.getClassName());
root.pkg = safeCharSeqToString(info.getPackageName());
root.desc = safeCharSeqToString(info.getContentDescription());
root.checkable = info.isCheckable();
root.checked = info.isChecked();
root.clickable = info.isClickable();
root.enabled = info.isEnabled();
root.focusable = info.isFocusable();
root.focused = info.isFocused();
root.scrollable = info.isScrollable();
root.longClickable = info.isLongClickable();
root.password = info.isPassword();
root.selected = info.isSelected();
android.graphics.Rect r = AccessibilityNodeInfoHelper
.getVisibleBoundsInScreen(info, width, height);
root.rect = new Rect(r.left, r.top, r.right, r.bottom);
root.children = new ArrayList<Node>();
int count = info.getChildCount();
for (int i = 0; i < count; i++) {
AccessibilityNodeInfo child = info.getChild(i);
if (child != null) {
if (child.isVisibleToUser()) {
Node childNode = new Node();
dumpNode(child, childNode, i, width, height);
root.children.add(childNode);
child.recycle();
}
}
}
}
Builder(int id, @Nullable ViewHierarchyElementAndroid parent, AccessibilityNodeInfo fromInfo) {
// Bookkeeping
this.id = id;
this.parentId = (parent != null) ? parent.getId() : null;
// API 18+ properties
this.resourceName = AT_18 ? fromInfo.getViewIdResourceName() : null;
this.editable = AT_18 ? fromInfo.isEditable() : null;
// API 16+ properties
this.visibleToUser = AT_16 ? fromInfo.isVisibleToUser() : null;
// API 21+ properties
if (AT_21) {
ImmutableList.Builder<ViewHierarchyActionAndroid> actionBuilder =
new ImmutableList.Builder<>();
actionBuilder.addAll(
Lists.transform(
fromInfo.getActionList(),
action -> ViewHierarchyActionAndroid.newBuilder(action).build()));
this.actionList = actionBuilder.build();
}
// API 24+ properties
this.drawingOrder = AT_24 ? fromInfo.getDrawingOrder() : null;
// API 29+ properties
this.hasTouchDelegate = AT_29 ? (fromInfo.getTouchDelegateInfo() != null) : null;
// Base properties
this.className = fromInfo.getClassName();
this.packageName = fromInfo.getPackageName();
this.accessibilityClassName = fromInfo.getClassName();
this.contentDescription = SpannableStringAndroid.valueOf(fromInfo.getContentDescription());
this.text = SpannableStringAndroid.valueOf(fromInfo.getText());
this.importantForAccessibility = true;
this.clickable = fromInfo.isClickable();
this.longClickable = fromInfo.isLongClickable();
this.focusable = fromInfo.isFocusable();
this.scrollable = fromInfo.isScrollable();
this.canScrollForward =
((fromInfo.getActions() & AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) != 0);
this.canScrollBackward =
((fromInfo.getActions() & AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) != 0);
this.checkable = fromInfo.isCheckable();
this.checked = fromInfo.isChecked();
this.touchDelegateBounds = new ArrayList<>(); // Populated after construction
android.graphics.Rect tempRect = new android.graphics.Rect();
fromInfo.getBoundsInScreen(tempRect);
this.boundsInScreen = new Rect(tempRect.left, tempRect.top, tempRect.right, tempRect.bottom);
this.nonclippedHeight = null;
this.nonclippedWidth = null;
this.textSize = null;
this.textColor = null;
this.backgroundDrawableColor = null;
this.typefaceStyle = null;
this.enabled = fromInfo.isEnabled();
}
/**
* Processes an AccessibilityEvent, by traversing the View's tree and
* putting together a message to speak to the user.
*/
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (!mTextToSpeechInitialized) {
Log.e(LOG_TAG, "Text-To-Speech engine not ready. Bailing out.");
return;
}
// This AccessibilityNodeInfo represents the view that fired the
// AccessibilityEvent. The following code will use it to traverse the
// view hierarchy, using this node as a starting point.
//
// NOTE: Every method that returns an AccessibilityNodeInfo may return null,
// because the explored window is in another process and the
// corresponding View might be gone by the time your request reaches the
// view hierarchy.
AccessibilityNodeInfo source = event.getSource();
if (source == null) {
return;
}
// Grab the parent of the view that fired the event.
AccessibilityNodeInfo rowNode = getListItemNodeInfo(source);
if (rowNode == null) {
return;
}
// Using this parent, get references to both child nodes, the label and the checkbox.
AccessibilityNodeInfo labelNode = rowNode.getChild(0);
if (labelNode == null) {
rowNode.recycle();
return;
}
AccessibilityNodeInfo completeNode = rowNode.getChild(1);
if (completeNode == null) {
rowNode.recycle();
return;
}
// Determine what the task is and whether or not it's complete, based on
// the text inside the label, and the state of the check-box.
if (rowNode.getChildCount() < 2 || !rowNode.getChild(1).isCheckable()) {
rowNode.recycle();
return;
}
CharSequence taskLabel = labelNode.getText();
final boolean isComplete = completeNode.isChecked();
String completeStr = null;
if (isComplete) {
completeStr = getString(R.string.task_complete);
} else {
completeStr = getString(R.string.task_not_complete);
}
String taskStr = getString(R.string.task_complete_template, taskLabel, completeStr);
StringBuilder utterance = new StringBuilder(taskStr);
// The custom ListView added extra context to the event by adding an
// AccessibilityRecord to it. Extract that from the event and read it.
final int records = event.getRecordCount();
for (int i = 0; i < records; i++) {
AccessibilityRecord record = event.getRecord(i);
CharSequence contentDescription = record.getContentDescription();
if (!TextUtils.isEmpty(contentDescription )) {
utterance.append(SEPARATOR);
utterance.append(contentDescription);
}
}
// Announce the utterance.
mTts.speak(utterance.toString(), TextToSpeech.QUEUE_FLUSH, null);
Log.d(LOG_TAG, utterance.toString());
}
public void testAirplaneModeToOn() {
UiAutomation uiAutomation = getInstrumentation().getUiAutomation();
// Activityの起動を監視するリスナーをセット
mMainLaunched = false;
uiAutomation
.setOnAccessibilityEventListener(new OnAccessibilityEventListener() {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
// ウィンドウのコンテンツが変わった
if (TARGET_PKG.equals(event.getPackageName())) {
// MainActivityが起動した
mMainLaunched = true;
}
}
}
});
// MainActivity起動
Activity target = launchActivity(TARGET_PKG, MainActivity.class, null);
try {
// MainActivity起動待ち
do {
Thread.sleep(1000);
} while (!mMainLaunched);
// 機内モードをOnにする
// Settingsの起動
Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getInstrumentation().getContext().startActivity(intent);
// Settingsの起動待ち
AccessibilityNodeInfo root;
while (true) {
root = uiAutomation.getRootInActiveWindow();
if (root != null && SETTINGS_PKG.equals(root.getPackageName())) {
break;
} else {
Thread.sleep(1000);
}
}
// ボタンを押す
List<AccessibilityNodeInfo> list = root
.findAccessibilityNodeInfosByViewId("android:id/list");
AccessibilityNodeInfo listViewInfo = list.get(0);
AccessibilityNodeInfo airplaneModeView = listViewInfo.getChild(0);
List<AccessibilityNodeInfo> checkList = airplaneModeView
.findAccessibilityNodeInfosByViewId("android:id/checkbox");
AccessibilityNodeInfo airplaneModeCheck = checkList.get(0);
if (!airplaneModeCheck.isChecked()) {
airplaneModeView.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
// Backキーを押してSettingsの終了
uiAutomation.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
// 機内モード反映待ち
Thread.sleep(10000);
// TextViewの文字列検証
String expected = target
.getString(org.techbooster.uiautomationsample.R.string.airplane_mode_off);
TextView textView = (TextView) target
.findViewById(org.techbooster.uiautomationsample.R.id.text_view);
assertEquals(expected, textView.getText().toString());
} catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
} finally {
if (target != null) {
target.finish();
}
}
}