Accesssing android call logs

A simple android library for accessing device call logs

So, say you are developing this cool app of yours, and somewhere along the way you find yourself in need of getting your hands on the call logs. You search the internet and end up seeing a bunch of sample codes with all these cursors and what not. And it seems like a lot for such a simple thing.

Well, I created a simpler and much cleaner solution to that problem. Today I’ll introduce you to a library I created to help with just that, because I too have been there.

Library installation

Add the following lines in your root build.gradle file:

Then add this lines to your module/app build.gradle file:

After adding the repository and the library dependency, perform a gradle sync if you haven’t done it yet.

Handling permissions

For those targeting API 23+, you should handle the run-time permissions for READ_CONTACTS and READ_CALL_LOG. This is done in the sample app found in the GitHub repository. Other than that, permissions are automatically added to the manifest file.

Retrieving the call logs

The following lines demonstrate how easy it is to access the call logs. Its a proof how helpful the library actually is.

You can access the call logs in just 3 lines of code. It’s that easy! The library provides you with a List<LogObject> containing all the logs available, each enclosed in a LogObject.

LogsManager

The LogsManager class does all the heavy work for you, all you need is to initialize it and you are ready to get some of the following data:

  • logsManager.getIncomingDuration(): returns the number of seconds for all the incoming calls made.
  • logsManager.getOutgoingDuration(): returns the number of seconds for all the outgoing calls made.
  • logsManager.getTotalDuration(): returns the number of seconds for all the calls made.
  • logsManager.getCoolDuration(type): returns a String in the form of “x hrs xx mins” or “xx mins xx secs”

The ‘getCoolDuration(type)’ method accepts various types defining which data is to be extracted from the device. The following types can be used specified data:

  • LogsManager.INCOMING_CALLS: for getting the list (LogObject) of all the incoming calls.
  • LogsManager.OUTGOING_CALLS: for getting the list (LogObject) of all the incoming calls.
  • LogsManager.MISSED_CALLS: for getting the list (LogObject) of all the incoming calls.
  • LogsManager.ALL_CALLS: for getting the list (LogObject) of all the calls.

LogObject

The LogsManager objects retrieves the sum total of all the data in the device, and not  that of the individual log. The LogObject however, covers this by providing data that is bound to a specific call log in the device.

methods that the object supports:

  • logObject.getDuration(): returns an int of the seconds used for that particular call.
  • logObject.getNumber(): returns a String of the number associated with the call made.
  • logObject.getContactName(): returns a String of the contact name for the number in the log, if not found, it returns the number.
  • logObject.getType(): returns an int for the type of the call log.
  • logObject.getCoolDuration(): returns a String in the form of “x hrs xx mins” or “xx mins xx secs” based on the duration.

Note: The ‘getContactName()’ should be run only when the permission to read contacts ( android.permission.READ_CONTACTS) has been granted, otherwise your app will crash.

Thank you very much for reading all this way. I hope our library gets to help you out.