Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap API to query call-logs

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.

like image 990
sudipto Avatar asked Jun 08 '11 10:06

sudipto


2 Answers

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");
});
like image 158
sudipto Avatar answered Nov 01 '22 11:11

sudipto


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

like image 4
Daniel Kurka Avatar answered Nov 01 '22 10:11

Daniel Kurka