8

I have just started with phoneGap and Android. Built basic samples.

I would like to know, if there is an API to get the call logs. I wish to create a grid showing:

  • Number of missed calls over a period of time
  • Number of received calls
  • Number of calls made
  • Total time of the received calls and calls made

Thanks in advance.

sudipto
  • 2,472
  • 1
  • 17
  • 21

2 Answers2

24

I did a bit of research and has successfully built a PhoneGap plugin which would fetch the CallLog from android.provider.CallLog.

This returns an JSON { Rows: [] } where Rows is an 2 dimensional array of call records containing the following fields (as Array) in the following order :

  • Date (as UNIX Time stamp)
  • Number,
  • Type (1- incoming, 2- outgoing, 3- missed)
  • Duration (in seconds)
  • New
  • Cached Name
  • Cached number type
  • Cached number label

Details are in http://developer.android.com/reference/android/provider/CallLog.Calls.html

I have also made a small sample using this plugin which would show total number of outgoing calls, missed calls and incoming calls and plot them in a Pie chart. The sample is using FusionCharts' Pie chart.

You can download a beta try-out .apk from :

http://www.sudipto.net/download/android/apps/CallLog/beta/CallChart.apk.zip

(using JavaScript SVG charts that works in Android version 3 or above)

Here is the source-code zip for you to delve into:

http://www.sudipto.net/download/android/apps/CallLog/beta/calllog_phonegap_eclipseclassic_source.zip

Here is my complete code:

CallLog.java

package com.fusioncharts.phonegap.plugin;

import org.json.*;

import android.database.*;
import android.util.Log;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

public class CallLog extends Plugin {

        @Override
        public PluginResult execute(String actionName, JSONArray arguments, String callback) 
        {


                JSONObject callLogs = new JSONObject();
                PluginResult result = null;


                try {
                        switch (getActionItem(actionName))
                        {
                                case 1:
                                        callLogs = getAllCallLog(arguments);
                                        result = new PluginResult(Status.OK, callLogs);
                                        break;
                                default:
                                        result = new PluginResult(Status.INVALID_ACTION);
                        }
                } catch (JSONException jsonEx) {
                        result = new PluginResult(Status.JSON_EXCEPTION);
                }



                return result;
        }


        private JSONObject getAllCallLog(JSONArray requirements) throws JSONException
        {
                JSONObject callLog = new JSONObject();

                String[] strFields = {
                        android.provider.CallLog.Calls.DATE,
                        android.provider.CallLog.Calls.NUMBER, 
                        android.provider.CallLog.Calls.TYPE,
                        android.provider.CallLog.Calls.DURATION,
                        android.provider.CallLog.Calls.NEW,
                        android.provider.CallLog.Calls.CACHED_NAME,
                        android.provider.CallLog.Calls.CACHED_NUMBER_TYPE,
                        android.provider.CallLog.Calls.CACHED_NUMBER_LABEL//,
                };

                try {
                        Cursor callLogCursor = ctx.getContentResolver().query(
                                android.provider.CallLog.Calls.CONTENT_URI,
                                strFields,
                                null,
                                null,
                                android.provider.CallLog.Calls.DEFAULT_SORT_ORDER
                            );



                int callCount = callLogCursor.getCount();

                if(callCount>0){
                        JSONArray callLogItem = new JSONArray();
                        JSONArray callLogItems = new JSONArray();

                        String[] columnNames = callLogCursor.getColumnNames();

                        callLogCursor.moveToFirst();
                        do
                        {
                                callLogItem.put(callLogCursor.getLong(0));
                                callLogItem.put(callLogCursor.getString(1));
                                callLogItem.put(callLogCursor.getInt(2));
                                callLogItem.put(callLogCursor.getLong(3));
                                callLogItem.put(callLogCursor.getInt(4));
                                callLogItem.put(callLogCursor.getString(5));
                                callLogItem.put(callLogCursor.getInt(6));
                                callLogItems.put(callLogItem);
                                callLogItem = new JSONArray();

                        }while(callLogCursor.moveToNext());

                        callLog.put("Rows", callLogItems);
                }


                callLogCursor.close();
                }catch(Exception e)
                {

                        Log.d("CallLog_Plugin", " ERROR : SQL to get cursor: ERROR " + e.getMessage());
                }



                return callLog;
        }

        private JSONObject getTimeRangeCallLog(JSONArray requirements)
        {

        private int getActionItem(String actionName) throws JSONException 
        {
                JSONObject actions = new JSONObject("{'all':1,'last':2,'time':3}");
                if (actions.has(actionName))
                        return actions.getInt(actionName);

                return 0;
        }
}

calllog.phonegap.js

    var CallLog = function() {};
    CallLog.prototype.all = function(params, successCallback, failureCallback) 
    {
        return PhoneGap.exec(successCallback, failureCallback, 'CallLog', 'all', [params]);
    };

    PhoneGap.addConstructor( function() {
          PhoneGap.addPlugin("calllog", new CallLog());
          PluginManager.addService("CallLog","com.fusioncharts.phonegap.plugin.CallLog");
    });

Application.java

var CallLog = function() {};
CallLog.prototype.all = function(params, successCallback, failureCallback) 
{
    /* @param   successCallback
     * @param   failureCallback
     * @param   plugin name
     * @param   action
     * @param   JSONArray of parameters
     */ 
    return PhoneGap.exec(successCallback, failureCallback, 'CallLog', 'all', [params]);
};

PhoneGap.addConstructor( function() {
      //Register the javascript plugin with PhoneGap
      PhoneGap.addPlugin("calllog", new CallLog());

      //Register the native class of plugin with PhoneGap
      PluginManager.addService("CallLog","com.fusioncharts.phonegap.plugin.CallLog");
});
sudipto
  • 2,472
  • 1
  • 17
  • 21
  • Thanks for this important answer, your JS function returns array of array, will you please tell what are the contents of child array?? – Laxmikant Dange May 28 '14 at 15:13
  • 1
    The JSON comes as { Rows: [] }. You must be talking of rows array. Rows is an indexed array of all callLog entries, so its an array - one row is one entry. Each entry is an indexed array where the calllog data for the entry is stored. The order is Date in index 0; Number in index 1, Type in index 2, Duration in index 3, New in index 4, Cached Name in index 5, Cached number type in index 6, Cached number label in index 7. This is already listed above or you can check http://developer.android.com/reference/android/provider/CallLog.Calls.html – sudipto May 29 '14 at 14:50
  • sudipto,Thanks for your answer. If I assign null for strFields then its returning with sub_id, but not duration, and if I pass sub_id, as extra field in above array, then its not giving me any output, I need both duration and sub_id, please help me.. – Laxmikant Dange Jun 02 '14 at 11:11
  • You need to dig into the API from http://developer.android.com/reference/android/provider/CallLog.Calls.html and need to try. – sudipto Jun 03 '14 at 09:54
4

There is no standard API in phonegap to achieve what you want to do, but you can always write a plugin for your platform and get the information from the android API. ( I don´t actually know if you can get the information you need with java on android).

Here is a link on how to write a phonegap plugin: http://wiki.phonegap.com/w/page/36752779/PhoneGap-Plugins

Daniel Kurka
  • 7,973
  • 2
  • 24
  • 43