Wednesday, July 3, 2019

Making A WhatsApp Video Call Without User Intervention

The code is pretty self explanatory. It needs a view with a TextView with id "editText" and a Button with id "button".

You enter the phone number to TextView and click on the button. It finds a user with this number and makes a video call using whatsapp with no user intervention.

The code can be changed to support VoIP calls also. Then the mimetype should show
"vnd.android.cursor.item/vnd.com.whatsapp.voip.call"

package com.example.whatsappcall;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        askPermissions();

        Button clickButton = findViewById(R.id.button);
        clickButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (
                        checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED ||
                                checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED
                ) {
                    askPermissions();
                } else {
                    EditText eText = findViewById(R.id.editText);
                    // I am not checking correctness of number
                    Long _ID = getContactIdUsingNumber(eText.getText().toString(), v.getContext());
                    if (_ID != null) {
                        videoCall(_ID);
                    } else {
                        Toast.makeText(v.getContext(), "Number not found or can't accept video call!!!",
                                Toast.LENGTH_LONG).show();
                    }
                }
            }
        });


    }

    // Get required permissions
    public void askPermissions() {
        // Ask permissions
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE}, 1);
    }

    /*
     * Finds out if the given phone number has a whatsapp video call _ID
     */
    public Long getContactIdUsingNumber(String phoneNumber, Context context) {

        // Search contact using phone number
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);

        // Store ID of the contact we are searching
        long contactId = 0L;
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.PhoneLookup.CONTACT_ID));
        } else
            return null;

        // Make array of 1 element
        String[] selectionArgs = {Long.toString(contactId)};
        // Select clause to search for contact I
        String selectionClause = ContactsContract.Data.CONTACT_ID + " = ? ";

        Long _ID = null;
        cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, selectionClause, selectionArgs, null);
        // Cursor can't be null but anyway...
        if (cursor != null)
            while (cursor.moveToNext()) {
                String mimeType = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
                if (mimeType.equals("vnd.android.cursor.item/vnd.com.whatsapp.video.call")) {
                    _ID = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data._ID));
                }
            }
        else
            return null;

        cursor.close();

        return _ID;
    }


    public void videoCall(Long _ID) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        String data = "content://com.android.contacts/data/" + Long.toString(_ID);
        String type = "vnd.android.cursor.item/vnd.com.whatsapp.video.call";
        intent.setDataAndType(Uri.parse(data), type);
        intent.setPackage("com.whatsapp");
        startActivity(intent);
    }

}

1 comment: