12

Is it by anyway possible to write to call logs database?? I mean i want to add selected numbers to the call history. I tried searching the tutorial on net but couldn't find any... Though 1 thing is for sure, IT CAN BE DONE. coz i have seen lots of applications online, which restore call logs, so i guess we can modify the call history database somehow(but how exactly is not clear with me).

i already read this post but it happens to be posted a long time back.

Any help will be appreciated! Thanx!

Community
  • 1
  • 1
JaVadid
  • 7,107
  • 13
  • 42
  • 54
  • possible duplicate of [How to add new field(s) and records to the call logs(call history) database?](http://stackoverflow.com/questions/2738421/how-to-add-new-fields-and-records-to-the-call-logscall-history-database) – GalacticCowboy Jul 02 '10 at 13:38
  • Also, restoring the entire Log file is very different from injecting something into it. – GalacticCowboy Jul 02 '10 at 13:39
  • @GalacticCowboy I guess i have already mentioned the same post in my question itself. i am aware about that, but as i mentioned the post is a bit old and is of the time of Android 1.6. and i just want to insert a record! – JaVadid Jul 03 '10 at 05:13
  • However, the software you're linking does *not* "poke" items into the call log, it completely replaces (overwrites) it. As I said above, that is completely different. – GalacticCowboy Jul 03 '10 at 17:24
  • @GalacticCowboy If that is the thing then can u suggest a method for replacing the call logs??? coz i can manage both ways! whether it is "poking" my contacts in or replacing the whole call log... any guidance will be useful! – JaVadid Jul 06 '10 at 04:27
  • Android is open-source (http://source.android.com) so you might be able to figure out where this call log exists and read/modify it based on that. Everything exposed by the Android SDK appears to be read-only. – GalacticCowboy Jul 06 '10 at 11:49
  • @GalacticCowboy Well thanx for the info but i very well know its open-source :) The thing is that i wanted to have an opinion about how my desired task can be accomplished in the best possible way, if some1 does know about how it is done in the aforementioned app... Thanx anyway! – JaVadid Jul 06 '10 at 12:29

3 Answers3

17

You can use this snippet to add new records to the existing Call logs content provider:

public static void insertPlaceholderCall(ContentResolver contentResolver, String number){
    ContentValues values = new ContentValues();
    values.put(CallLog.Calls.NUMBER, number);
    values.put(CallLog.Calls.DATE, System.currentTimeMillis());
    values.put(CallLog.Calls.DURATION, 0);
    values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
    values.put(CallLog.Calls.NEW, 1);
    values.put(CallLog.Calls.CACHED_NAME, "");
    values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
    values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
    Log.d(TAG, "Inserting call log placeholder for " + number);
    contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
}

(Code taken from Google Voice Callback for Android)

Remember to add the permissions in the Manifest

<uses-permission
    android:name="android.permission.READ_CONTACTS"/>
<uses-permission
    android:name="android.permission.WRITE_CONTACTS"/>
Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69
  • This version is working perfect on my Galaxy Nexus running ICS. Thanks Roberto! – mikerowehl Feb 01 '12 at 22:42
  • Hello i want to do the same but it's throwing Permission denial exception even after adding these two permissions. Any guess? – Suresh Sharma Sep 27 '13 at 09:43
  • 3
    Good answer. But you need – Ton Apr 15 '14 at 07:52
  • This permission was included with API 16. Anyway, if your app uses the READ_CONTACTS/WRITE_CONTACTS permission and both your minSdkVersion and targetSdkVersion values are set to 15 or lower, the system implicitly grants your app this permission. – Roberto Leinardi Apr 15 '14 at 08:13
  • Ton seems to be right. It didn't work without this permissions for me. – kcpr Nov 17 '14 at 18:19
  • @Roberto i need to get call history,i m using call log read all call log its fine, but if i m using dual sim means , i need sim1 history only its possible – saravanan May 10 '18 at 05:15
1

The linked post explains it very well so I don't know why you are asking again. You cannot modify the call logs unless you keep your own database or your own firmware.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • please check this link.. Many apps in android market prove this capability... check this 4 example http://android.riteshsahu.com/apps/call-logs-backup-restore – JaVadid Jul 03 '10 at 04:24
  • Actually that isn't true. The call logs are accessible via a content provider. You can read, update and write to the call logs using the content provider. You cannot add additional fields (columns) to the call logs database (ie: **modify the schema**), but that isn't what OP is asking. You might want to reconsider your answer. – David Wasser Aug 21 '13 at 19:52
  • @Robbyi need to get call history,i m using call log read all call log its fine, but if i m using dual sim means , i need sim1 history only its possible – saravanan May 10 '18 at 05:16
0

I've prepared a Kotlin version, which adds random items, and with different types:

manifest

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

MainActivity Note: I didn't handle granting the permissions here. I assume they are granted.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<View>(R.id.button).setOnClickListener {
            thread {
                val itemsToAdd = 2000
                val phoneListSize = 10
                val phonesList = ArrayList<String>(phoneListSize)
                //Israeli cell phone prefix
                val phonePrefix = "+97250"
                val extraDigitsAfterPhonePrefix = 7
                for (i in 0 until phoneListSize) {
                    val sb = StringBuilder(phonePrefix.length + 9)
                    sb.append(phonePrefix)
                    for (j in 0 until extraDigitsAfterPhonePrefix)
                        sb.append(Random.nextInt(10).toString())
                    phonesList.add(sb.toString())
                }
                Log.d("AppLog", "list of phones to use:${phonesList}")
                try {
                    for (i in 0 until itemsToAdd) {
                        val number = phonesList.get(Random.nextInt(phonesList.size))
                        Log.d("AppLog", "$i - Inserting call log placeholder for $number")
                        insertPlaceholderCall(contentResolver, number)
                    }
                } catch (e: Throwable) {
                    Log.e("AppLog", "failed:$e")
                    e.printStackTrace()
                }
                Log.d("AppLog", "done")
            }
        }
    }

    companion object {
        fun insertPlaceholderCall(contentResolver: ContentResolver, number: String) {
            val values = ContentValues()
            values.put(CallLog.Calls.NUMBER, number)
            val cal = Calendar.getInstance()
            //go back up to 30 days, in seconds
            cal.add(Calendar.SECOND, -Random.nextInt(30 * 24 * 60 * 1000))
            values.put(CallLog.Calls.DATE, cal.timeInMillis)
            // up to 10 minutes call
            values.put(CallLog.Calls.DURATION, Random.nextInt(60 * 10))
            val type = when (Random.nextInt(4)) {
                0 -> CallLog.Calls.OUTGOING_TYPE
                1 -> CallLog.Calls.INCOMING_TYPE
                2 -> CallLog.Calls.MISSED_TYPE
                3 -> CallLog.Calls.REJECTED_TYPE
                //BLOCKED_TYPE is ignored in the Phone app, for some reason
                4 -> CallLog.Calls.BLOCKED_TYPE
                //VOICEMAIL_TYPE causes an exception of IllegalArgumentException: Uri content://call_log/calls cannot be used for voicemail record. Please set 'allow_voicemails=true' in the uri.
                else -> CallLog.Calls.VOICEMAIL_TYPE
            }
            values.put(CallLog.Calls.TYPE, type)
            values.put(CallLog.Calls.NEW, 1)
            values.put(CallLog.Calls.CACHED_NAME, "")
            values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0)
            values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "")
            contentResolver.insert(CallLog.Calls.CONTENT_URI, values)
        }
    }
}

The documentation is a bit weird though, as it says it supports 3 types, yet it lists 5 types:

https://developer.android.com/reference/android/provider/CallLog.Calls#TYPE

For some reason I got an exception when trying to use VOICEMAIL_TYPE, though:

java.lang.IllegalArgumentException: Uri content://call_log/calls cannot be used for voicemail record. Please set 'allow_voicemails=true' in the uri.

Also, when trying to use BLOCKED_TYPE, I can't see the item anywhere in the Phone app.

If anyone can tell me how to fix it, please let me know.

android developer
  • 114,585
  • 152
  • 739
  • 1,270