Collecting logs from deleting contacts in Android

44 Views Asked by At

I'm trying to save informations about deleting contacts in .txt log, but when i'm deleting any contact, nothing happens. If i Block or Unblock Wipe Data, there is info in logs, but no while i'm deleting or moving contacts to trash.

@SuppressLint("Range")
    private fun getContactDetails(contactUri: Uri): String {
        val resolver: ContentResolver = contentResolver
        val cursor: Cursor? = resolver.query(contactUri, null, null, null, null)
        cursor?.use {
            if (it.moveToFirst()) {
                val nameIndex = it.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
                val numberIndex = it.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)
                if (nameIndex != -1 && numberIndex != -1) {
                    val name = it.getString(nameIndex)
                    val number = it.getString(numberIndex)
                    return "Name: $name, Number: $number, Date: ${getCurrentDateTime()}"
                }
            } else {
                // The contact doesn't exist, likely moved to trash
                return "Contact moved to trash: $contactUri, Date: ${getCurrentDateTime()}"
            }
        }
        return ""

    }

Also added permissions to AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

I tried to retrieve contact information using ContentResolver.query() and Cursor. Initially, I attempted to access contact details through ContactsContract.Contacts.CONTENT_URI, and then checked if the cursor contains the relevant columns (e.g., DISPLAY_NAME and HAS_PHONE_NUMBER) and retrieved the data accordingly. Additionally, I tried to handle the situation when a contact does not exist (moved to trash), which was part of my approach to solving the problem.

0

There are 0 best solutions below