Search code examples
androidkotlininteractiveussd

Stuck in developing an interactive USSD app


Halo great people?

I am using the below code to get, dismiss and toast a USSD response message, However, I would like to interact with the ussd response containing menus or options,

e.g, reply with one in the USSD's edit text and click the send button.

How can I go about this. if you have some reference code, examples, or you can point me in the right direction I'll be very grateful.

Any help is appreciated in advance.

Thanks, Keep safe.

class USSDService : AccessibilityService() {

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
    val ussdResponse = event!!.text.toString().replace("[", "").replace("]", "")
    if( TextUtils.isEmpty(ussdResponse) ) return;
    if (event.className == "android.app.AlertDialog") {
        performGlobalAction(GLOBAL_ACTION_BACK)
        val intent = Intent("com.times.ussd.action.REFRESH")
        intent.putExtra("message", ussdResponse)

        val nodeInfo = event.source
        val list: List<AccessibilityNodeInfo> = nodeInfo.findAccessibilityNodeInfosByText("Send")
        for (node in list) {

            if (ussdResponse.contains("Select User")){
                /** I would like to perform the setting of text and replying back here... */
            }
        }



        Toast.makeText(this, ussdResponse, Toast.LENGTH_LONG).show()
        // 
    }
}

override fun onInterrupt() {
    TODO("Not yet implemented")
}

override fun onServiceConnected() {
    super.onServiceConnected()
    val info = AccessibilityServiceInfo()
    info.flags = AccessibilityServiceInfo.DEFAULT
    info.packageNames = arrayOf("com.android.phone")
    info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC
    serviceInfo = info
}

}

/** I found the Following code; */

val inputNode = source.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)
val arguments = Bundle() 

arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "2")

inputNode.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments)
for (node in list) {                                        
      node.performAction(AccessibilityNodeInfo.ACTION_CLICK)
}

Supposed to reply with 6 but returns a null. There's an exception the input is null.

Anyone seeing where or what I am doin wrong!


Solution

  • I noticed that this is NOT supported for Android below API 5, but runs fine in API 6 and above.