Programmatically add a list of numbers to phone and mark them as blocked

106 Views Asked by At

I am trying to develop an application which would add 1800+ numbers to the phone and mark them as blocked.

These are spam calls and few scam calls that we as a group received and would like to share this app within our group to not receive the calls.

Any help is much appreciated.

I am able to add the contacts from a file to the contacts app but not able to mark them as blocked.

Below is the code snippet for adding the contacts



import android.Manifest;
import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BlockedNumberContract;
import android.provider.ContactsContract;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static final int READ_CONTACTS_PERMISSION_REQUEST = 1;

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

        // Request READ_CONTACTS and WRITE_CONTACTS permissions if not granted
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
                        != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS},
                    READ_CONTACTS_PERMISSION_REQUEST);
        } else {
            addBlockedNumbersToContacts();
        }
    }

    private void addBlockedNumbersToContacts() {
        try {
            InputStream inputStream = getAssets().open("numbers.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            ArrayList<String> blockedNumbers = new ArrayList<>();
            String line;
            while ((line = reader.readLine()) != null) {
                String normalizedNumber = line.trim();
                if (!normalizedNumber.isEmpty()) {
                    blockedNumbers.add(normalizedNumber);
                }
            }

            reader.close();

            for (String number : blockedNumbers) {
                // Add the number to contacts with a custom label "Blocked"
                addNumberToContacts(number, "Blocked");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addNumberToContacts(String phoneNumber, String label) {
        // Prepare the contact values
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();
        int rawContactInsertIndex = ops.size();

        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                .build());

        // Add the phone number to the contact data with the custom label
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
                .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, label)
                .build());


        try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            Log.d("Contact", "Number added to contacts: " + phoneNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == READ_CONTACTS_PERMISSION_REQUEST) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                addBlockedNumbersToContacts();
            } else {
               
            }
        }
    }
}


Now how do I mark the numbers as blocked ?

0

There are 0 best solutions below