Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Answering a Whatsapp video call programmatically

Is there a way to auto answer whatsapp video call using AccessibilityService in Android?

OR is there a way to stimulate a click on headset's/bluetooth's call answering button? How can i get the id of the answering button?? to perform a click with accessibility service

I know that starting from Android 8.0 Oreo we have ANSWER_PHONE_CALLS permission, but for my project i want to use an old device for remote monitoring.

Any help would be appreciated!

----- Update: Thanks to the answer of Mr. hemisphire and Mr. Kahbazi, the app is able to answer the call,but needs to be a system app to work! is there any way to make it work without being a system app? without the headset's button hack?

public class AnswerCall  extends AccessibilityService {
    @Override
    public void onAccessibilityEvent( AccessibilityEvent event )
    {
        if(event.getEventType() == TYPE_WINDOW_CONTENT_CHANGED)
        {

            if(event.getPackageName().equals("com.whatsapp"))
            {

                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            while(true) {
                                Instrumentation inst = new Instrumentation();
                                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_HEADSETHOOK);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };

                thread.start();
                StringBuilder sb = new StringBuilder();
                List<CharSequence> texts = event.getText();
                if (!texts.isEmpty())
                {
                    for (CharSequence s : event.getText()) {
                        sb.append(s);
                    }
                    if(sb.toString().equals("Incoming video call"))
                        Log.d( "onAccessibilityEvent", "whatsapp video call" );

                }
            }
        }
    }

    @Override
    public void onInterrupt() {

    }
}
like image 704
Nizar Avatar asked Apr 11 '18 12:04

Nizar


People also ask

How to start WhatsApp video call?

Alternatively, open WhatsApp, then tap the Calls tab > New Call . Find the contact you want to video call, then tap Video Call .


3 Answers

I don't think you can do what you want. Using the AccessibilityService you can know when the video call comes in:

@Override
public void onAccessibilityEvent( AccessibilityEvent event )
{
    if(event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)
    {
        if(event.getPackageName().equals("com.whatsapp"))
        {
            StringBuilder sb = new StringBuilder();
            List<CharSequence> texts = event.getText();
            if (!texts.isEmpty()) 
            {
                for (CharSequence s : event.getText()) 
                {
                    sb.append(s);
                }
                if(sb.toString().equals("Incoming video call"))
                {
                    Log.d( "onAccessibilityEvent", "whatsapp video call" );
                }
            }
        }
    }
}

However, I've never been able to answer the call programmatically. The question at How can incoming calls be answered programmatically in Android 5.0 (Lollipop)? does a great job of enumerating all possible options, but most require root and/or being a system app.

like image 126
hemisphire Avatar answered Sep 24 '22 20:09

hemisphire


You can use sendKeyDownUpSync method from Instrumentation class.

Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_HEADSETHOOK);

if this code didn't work, try to use another KeyEvent to find the correct one.

You can see the list of KeyEvent from this link : https://developer.android.com/reference/android/view/KeyEvent.html

You can check more info in from here : Instrumentation

like image 37
Kahbazi Avatar answered Sep 24 '22 20:09

Kahbazi


A classic way to achieve this is to observe notifications using the NotificationListenerService and act on the relevant action of the notification.

like image 22
rds Avatar answered Sep 27 '22 20:09

rds