Use accessiblityNodeInfo to get information even in the case of when phone is returning it will fetch the ussd response also it will dismiss dialog when there is option to enter multiple choices.
First of all in case of [pohne] event class name is returned as ussdalertactivty so i used only "alert" for identification of alert dialog of ussd response.
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getPackageName().toString().equals("com.android.phone")
&& event.getClassName().toString().toLowerCase()
.contains("alert")) {
AccessibilityNodeInfo source = event.getSource();
if (source != null) {
String pcnResponse = fetchResponse(source);
}
}
Now i have made a function called fetchResponse which will return the response from pcn as string and will also dismiss the dialog so need to do performGlobalAction(GLOBAL_ACTION_BACK).
private String fetchResponse(AccessibilityNodeInfo accessibilityNodeInfo) {
String fetchedResponse = "";
if (accessibilityNodeInfo != null) {
for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
AccessibilityNodeInfo child = accessibilityNodeInfo.getChild(i);
if (child != null) {
CharSequence text = child.getText();
if (text != null
&& child.getClassName().equals(
Button.class.getName())) {
// dismiss dialog by performing action click in normal
// cases
if((text.toString().toLowerCase().equals("ok") || text
.toString().toLowerCase()
.equals("cancel"))) {
child.performAction(AccessibilityNodeInfo.ACTION_CLICK);
return fetchedResponse;
}
} else if (text != null
&& child.getClassName().equals(
TextView.class.getName())) {
// response of normal cases
if (text.toString().length() > 10) {
fetchedResponse = text.toString();
}
} else if (child.getClassName().equals(
ScrollView.class.getName())) {
// when response comes as phone then response can be
// retrived from subchild
for (int j = 0; j < child.getChildCount(); j++) {
AccessibilityNodeInfo subChild = child.getChild(j);
CharSequence subText = subChild.getText();
if (subText != null
&& subChild.getClassName().equals(
TextView.class.getName())) {
// response of different cases
if (subText.toString().length() > 10) {
fetchedResponse = subText.toString();
}
}
else if (subText != null
&& subChild.getClassName().equals(
Button.class.getName())) {
// dismiss dialog by performing action click in
// different
// cases
if ((subText.toString().toLowerCase()
.equals("ok") || subText
.toString().toLowerCase()
.equals("cancel"))) {
subChild.performAction(AccessibilityNodeInfo.ACTION_CLICK);
return fetchedResponse;
}
}
}
}
}
}
}
return fetchedResponse;
}
Hope this will solve the issue irrespective of the devices.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…