termux-api icon indicating copy to clipboard operation
termux-api copied to clipboard

Feature wish: RawContacts read and write

Open Cigaes opened this issue 9 months ago • 0 comments

Feature description

Please consider adding the feature to allow:

  • dumping the complete RawContacts table and its corresponding Data table;
  • inserting and deleting lines into/from these tables.

Reference implementation

I have started my own app, to be called from Termux with am start -n $app, it is in very early stage, but it can already dump the RawContacts database:

    public void log_all_raw_contacts() {
        Cursor cursor = getContentResolver().query(
            ContactsContract.RawContacts.CONTENT_URI,
            null /* all columns TODO */,
            null, null, null
        );
        while (cursor.moveToNext()) {
            String name = cursor.getString(0);
            warn("raw_contact\n");
            for (int i = 0; i < cursor.getColumnCount(); i++)
                warn("  %s = \"%s\"\n", cursor.getColumnName(i), cursor.getString(i));
        }
    }

and an AI did for me the insertion, which happens to work:

    public void add_contact_test() {
        ContentResolver resolver = getContentResolver();
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();

        // 1. Insert a new raw contact
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
            .build());

        // 2. Insert the display name "Toto"
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Toto")
            .build());

        // 3. Insert the mobile phone number "42 17 19"
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "42 17 19")
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
            .build());

        // Apply the batch
        try {
            resolver.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException | OperationApplicationException e) {
            e.printStackTrace();
        }
    }

Thanks in advance.

Cigaes avatar Apr 21 '25 12:04 Cigaes