Whatsapp Accessibility Service for Image View (Android Studio)

Viewed 1291

I Have An Accessibility Code for My Whatsapp Application, However This Service Only Works if im Sending a Message Normally. Does anyone Know How i Can Get This Service to Work For Image Sending Too?

My WhatsappAccessibilityService

import android.accessibilityservice.AccessibilityService;
import android.provider.Settings;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;

import java.util.List;

public  class WhatAppAccessibilityService extends AccessibilityService {

    @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 ()) {
            return;
        }

        // check if the whatsapp message EditText field is filled with text and ending with your suffix (explanation above)
        AccessibilityNodeInfoCompat messageField = messageNodeList.get (0);
        if (messageField.getText () == null || messageField.getText ().length () == 0
                || !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);
    }

2 Answers

Add this class as AccessibiliyService, it is sending image in my whatsapp automatically.

public class WhatsappAccessibilityService extends AccessibilityService{

    @Override
        if(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
            Log.e("this", "ACC::onAccessibilityEvent : event=" + event);

            AccessibilityNodeInfo nodeInfo = event.getSource();
            Log.e("this", "ACC::onAccessibilityEvent : nodeInfo=" + nodeInfo);
            if (nodeInfo == null) {
                return;
            }


            //get whatsapp send message button node list
            List<AccessibilityNodeInfo> sendMessageNodeList = nodeInfo.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send");
            if (sendMessageNodeList == null) {
                Log.v("hritik", "sendMessageNodeList is null");
            }
            for (AccessibilityNodeInfo node : sendMessageNodeList) {
                Log.e("this", "ACC::onAccessibilityEvent : send_button=" + node);
                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                try {
                    Thread.sleep(1000); // hack for certain devices in which the immediate back click is too fast to handle
                    performGlobalAction(GLOBAL_ACTION_BACK);
                    Thread.sleep(1000);  // same hack as above
                    performGlobalAction(GLOBAL_ACTION_BACK);
                } catch (InterruptedException ignored) {
                }
                // performGlobalAction (GLOBAL_ACTION_BACK);
            }
        }

    }

    @Override
    public void onInterrupt() {
        Toast.makeText(getApplicationContext(), "not able to send", Toast.LENGTH_SHORT).show();
    
   }
}
/*Add this code in your AccessibiltyService*/    

if(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("sendesc", "").equals("imagens")) {
    Log.e("test","4");
    List<AccessibilityNodeInfo> nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("whatsz", "com.whatsapp.w4b") + ":id/send");
    if (nodes.size() > 0) {
    nodes.get(0).performAction(ACTION_CLICK);
    timer(2);
    Log.e("test","conversation_contact_name_A");
    List<AccessibilityNodeInfo> Whtsb = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("whatsz", "com.whatsapp.w4b") + ":id/conversation_contact_name");
    Log.e("test","conversation_contact_name_B");
    if (Whtsb.size() > 0) {
      if (Whtsb.get(0).getText().toString().contains("Contact_temp")) {
         Log.e("test","delete_contact_4");
    
     }
     }
    performGlobalAction(GLOBAL_ACTION_BACK);
    }
    } else {
       try {
     List<AccessibilityNodeInfo> Whtsb = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("whatsz", "com.whatsapp.w4b") + ":id/conversation_contact_name");
       if (Whtsb.size() > 0) {
    if (Whtsb.get(0).getText().toString().contains("Contact_temp")) {
    
    }
    }
    List<AccessibilityNodeInfo> nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("whatsz", "com.whatsapp.w4b") + ":id/send");
     if (nodes.size() > 0) {
     nodes.get(0).performAction(ACTION_CLICK);
    }
    }catch (Exception e){
    e.printStackTrace();
    Log.e("TAG", "Error - "+e.toString());
    }
    }
Related