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"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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: