0

I'm trying to send an image to a certain number of Whatsapp by pressing the Send button through the Accessibility Service indicated by “user1079425” in Stackoverflow Send message via whatsapp programmatically.

Here's the code I'm using:

private void sendImageWhatsapp (String telephone){

    String toNumber, tel, message;
    String mystring = "\u0007";

    // adjust tel.number
    tel = telephone.replaceAll("[^0-9+]","");
    if (tel.substring(0,1).equalsIgnoreCase("+")){
        tel = tel.substring(1);
    }
    else{
        if (tel.substring(0,2).equalsIgnoreCase("00")){
            tel = tel.substring(2);
        }
        else{
            tel = context.getApplicationContext().getResources().getString(R.string.prefix_code) + tel;
        }
    }

    //String toNumber = "+91 98765 43210"; // contains spaces.
    //toNumber = toNumber.replace("+", "").replace(" ", "");

    toNumber = tel;     // E164 format without '+' sign
    message = imageDidascalia.getText().toString() + mystring;

    Intent sendIntent = new Intent("android.intent.action.MAIN");
    //sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
    sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net");
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setPackage("com.whatsapp");
    sendIntent.setType("image/png");
    context.startActivity(sendIntent);
}

public class WhatsappAccessibilityService extends AccessibilityService {

private int index = 0;

@Override
public void onAccessibilityEvent (AccessibilityEvent event) {
    if (getRootInActiveWindow () == null) {
        return;
    }

    AccessibilityNodeInfoCompat rootInActiveWindow = AccessibilityNodeInfoCompat.wrap (getRootInActiveWindow ());

    // Whatsapp Message EditText id
    List<AccessibilityNodeInfoCompat> messageNodeList = rootInActiveWindow.findAccessibilityNodeInfosByViewId ("com.whatsapp:id/entry");
    if (messageNodeList == null || messageNodeList.isEmpty ()) {
        // Whatsapp Image EditText id
        index=1;
        messageNodeList = rootInActiveWindow.findAccessibilityNodeInfosByViewId ("com.whatsapp:id/pickfiletype_gallery");
        if (messageNodeList == null || messageNodeList.isEmpty ()) {
            return;
        }`enter code here`

        //return;
    }

    // check if the whatsapp message EditText field is filled with text and ending with your suffix (explanation above)
    AccessibilityNodeInfoCompat messageField = messageNodeList.get (index);
    if (messageField.getText () == null || messageField.getText ().length () == 0
            || !messageField.getText ().toString ().endsWith ("\u0007")) { // So your service doesn't process any message, but the ones ending your apps suffix
        //               || !messageField.getText ().toString ().endsWith (getApplicationContext ().getString (R.string.whatsapp_suffix))) { // So your service doesn't process any message, but the ones ending your apps suffix
        return;
    }

    // Whatsapp send button id
    List<AccessibilityNodeInfoCompat> sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId ("com.whatsapp:id/send");
    if (sendMessageNodeInfoList == null || sendMessageNodeInfoList.isEmpty ()) {
        return;
    }

    AccessibilityNodeInfoCompat sendMessageButton = sendMessageNodeInfoList.get (0);
    if (!sendMessageButton.isVisibleToUser ()) {
        return;
    }

    // Now fire a click on the send button
    sendMessageButton.performAction (AccessibilityNodeInfo.ACTION_CLICK);

    // Now go back to your app by clicking on the Android back button twice:
    // First one to leave the conversation screen
    // Second one to leave whatsapp
    try {
        Thread.sleep (500); // hack for certain devices in which the immediate back click is too fast to handle
        performGlobalAction (GLOBAL_ACTION_BACK);
        Thread.sleep (500);  // same hack as above
    } catch (InterruptedException ignored) {}
    performGlobalAction (GLOBAL_ACTION_BACK);
}

@Override
public void onInterrupt() {
}
}

This code works for the messages but doesn't work for the images.

Stefano
  • 1
  • 1

1 Answers1

0

I also got stuck in same issue. After spending quite a time over it, I solved the issue. If you want to send an image without user interaction, you simply remove the code and condition you are applying to the edit text of the WhatsApp. I will directly jump to the button code and your image will be sent automatically to the number you have passed. ` override fun onAccessibilityEvent(event: AccessibilityEvent) { if (rootInActiveWindow == null) { return } val rootInActiveWindow = AccessibilityNodeInfoCompat.wrap(rootInActiveWindow)

    // Whatsapp Message EditText id
    /*val messageNodeList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.whatsapp:id/entry")
    if (messageNodeList == null || messageNodeList.isEmpty()) {
        return
    }

    // check if the whatsapp message EditText field is filled with text and ending with your suffix (explanation above)
    val messageField = messageNodeList[0]
    if (messageField.text == null || messageField.text.length == 0 || !messageField.text.toString().endsWith("abcd")) { // So your service doesn't process any message, but the ones ending your apps suffix
        return
    }*/

    // Whatsapp send button id
    val sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send")
    if (sendMessageNodeInfoList == null || sendMessageNodeInfoList.isEmpty()) {
        return
    }
    val sendMessageButton = sendMessageNodeInfoList[0]
    if (!sendMessageButton.isVisibleToUser) {
        return
    }

    // Now fire a click on the send button
    sendMessageButton.performAction(AccessibilityNodeInfo.ACTION_CLICK)

    // Now go back to your app by clicking on the Android back button twice:
    // First one to leave the conversation screen
    // Second one to leave whatsapp
    try {
        Thread.sleep(2800) // hack for certain devices in which the immediate back click is too fast to handle
        performGlobalAction(GLOBAL_ACTION_BACK)
        Thread.sleep(2800) // same hack as above
    } catch (ignored: InterruptedException) {
    }
    performGlobalAction(GLOBAL_ACTION_BACK)
}

` If you pass image along with text, the edit text will return null, so we comment the code for edit text. Please go through this how I have commented code for the edit text of the Whatsapp so it directly performs button click.

Nimit Goyal
  • 149
  • 2
  • 7