Android how to Get the Calls counts for specific date

Viewed 334

I am trying to get the calls count for missed/incoming/outgoing calls for a specific date

Got the below code working fine but its getting all the calls count overall (for every date)

String[] projectionMissed = { CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
        String whereMissed = CallLog.Calls.TYPE+"="+CallLog.Calls.MISSED_TYPE;
        Cursor m = getContentResolver().query(CallLog.Calls.CONTENT_URI, projectionMissed,whereMissed, null, null);
        m.moveToFirst();

        String[] projectionIncoming = { CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
        String whereIncoming = CallLog.Calls.TYPE+"="+CallLog.Calls.INCOMING_TYPE;
        Cursor i = getContentResolver().query(CallLog.Calls.CONTENT_URI, projectionIncoming,whereIncoming, null, null);
        i.moveToFirst();

        String[] projectionOutgoing = { CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
        String whereOutgoing = CallLog.Calls.TYPE+"="+CallLog.Calls.OUTGOING_TYPE;
        Cursor o = getContentResolver().query(CallLog.Calls.CONTENT_URI, projectionOutgoing,whereOutgoing, null, null);
        o.moveToFirst();

        String numberOfIn = String.valueOf(i.getCount());
        String numberOfOut = String.valueOf(o.getCount());
        String numberOfMiss = String.valueOf(m.getCount());

        inCalls.setText(numberOfIn);
        outCalls.setText(numberOfOut);
        missCalls.setText(numberOfMiss);

I cant wrap my head on how to get the count of calls for example todays date can anyone provide me with atleast hints on where shall i look or what i am missing

i tried this solution but it returns the calls as 0 which is not

String[] projectionMissed = { CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
        String whereMissed = CallLog.Calls.TYPE+"="+CallLog.Calls.MISSED_TYPE+ " AND " + CallLog.Calls.DATE + "=" + datetoday.getDate();
        Cursor m = getContentResolver().query(CallLog.Calls.CONTENT_URI, projectionMissed,whereMissed, null, null);
        m.moveToFirst();
2 Answers

Alrighty,

Problem is the Date objects you are comparing in your SQL statements are completely different.

CallLog.Calls.DATE is returning the date the call occured, in milliseconds since the epoch.

And the datetoday.getDate() is returning YYYY-MM-DD hh:mm:ss.mmm format.

Well you caught me bored, so something like this works:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView mCallView = (TextView) findViewById(R.id.call);


      /*checkPermission(Manifest.permission.READ_CALL_LOG, 100);
        checkPermission(Manifest.permission.WRITE_CALL_LOG, 101);
        checkPermission(Manifest.permission.READ_CONTACTS, 102);*/

        Date date1 = new Date(calculateMilliSince1970("2020-05-28"));
        Date date2 = new Date(calculateMilliSince1970("2020-05-29"));

       mCallView.setText(getCallDetails(date1,date2));

    }


    long calculateMilliSince1970(String date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try
        {
            Date mDate = sdf.parse(date);
            long timeInMilliseconds = mDate.getTime();
            Log.d("TAG","Date in milli :: " + timeInMilliseconds);
            return timeInMilliseconds;
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);


        if (requestCode == 100) {

            // Checking whether user granted the permission or not.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // Showing the toast message
                Toast.makeText(MainActivity.this,
                        "Permission Granted",
                        Toast.LENGTH_SHORT)
                        .show();
            }
            else {
                Toast.makeText(MainActivity.this,
                        "Permission Denied",
                        Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    public void checkPermission(String permission, int requestCode) {

        // Checking if permission is not granted
        if (ContextCompat.checkSelfPermission(
                MainActivity.this,
                permission)
                == PackageManager.PERMISSION_DENIED) {
            ActivityCompat
                    .requestPermissions(
                            MainActivity.this,
                            new String[] { permission },
                            requestCode);
        }
        else {
            Toast
                    .makeText(MainActivity.this,
                            "Permission already granted",
                            Toast.LENGTH_SHORT)
                    .show();
        }
    }

    private StringBuffer getCallDetails(Date date1, Date date2) {

        StringBuffer sb = new StringBuffer();
        Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
        int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
        int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
        int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
        sb.append( "Call Details :");
        while ( managedCursor.moveToNext() ) {
            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            Date callDayTime = new Date(Long.parseLong(callDate));
            String callDuration = managedCursor.getString(duration);
            String dir = null;
            int dircode = Integer.parseInt(callType);
            switch (dircode) {
                case CallLog.Calls.OUTGOING_TYPE:
                    dir = "OUTGOING";
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    dir = "INCOMING";
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    dir = "MISSED";
                    break;
            }
            if (isWithinRange(callDayTime, date1, date2)) {
                sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
                sb.append("\n------------------------------------------------------------------------------------------------------------------------------");
            }
        }
        managedCursor.close();
        return sb;
    }


    boolean isWithinRange(Date testDate, Date startDate, Date endDate) {

        Log.i("TAG", "isWithinRange: "+ "Test date is "+ testDate+ " start date is "+ startDate +"  compare is "+testDate.before(startDate));
        return !(testDate.before(startDate) || testDate.after(endDate));
    }

}

After looking into the answer provided by @forthelulx and getting to know that the CallLog.Calls.DATE is returning the date in milliseconds.

got it working exactly as intended and returning the number of the type of calls for the day

Here is the code if anyone requires it

    Date c = Calendar.getInstance().getTime();
    System.out.println("Current time => " + c);

    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yy");
    final String formattedDate = df.format(c);

    long timeInMilliseconds = c.getTime();
    long timeInMiliDayBefore = timeInMilliseconds - 86400000;

    String[] projectionMissed = { CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
    String whereMissed = CallLog.Calls.TYPE+"="+CallLog.Calls.MISSED_TYPE+ " AND " + CallLog.Calls.DATE + ">=" + timeInMiliDayBefore;
    Cursor m = getContentResolver().query(CallLog.Calls.CONTENT_URI, projectionMissed,whereMissed, null, null);
    m.moveToFirst();

    String[] projectionIncoming = { CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
    String whereIncoming = CallLog.Calls.TYPE+"="+CallLog.Calls.INCOMING_TYPE+ " AND " + CallLog.Calls.DATE + ">=" + timeInMiliDayBefore;
    Cursor i = getContentResolver().query(CallLog.Calls.CONTENT_URI, projectionIncoming,whereIncoming , null, null);
    i.moveToFirst();

    String[] projectionOutgoing = { CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
    String whereOutgoing = CallLog.Calls.TYPE+"="+CallLog.Calls.OUTGOING_TYPE+ " AND " + CallLog.Calls.DATE + ">=" + timeInMiliDayBefore;
    Cursor o = getContentResolver().query(CallLog.Calls.CONTENT_URI, projectionOutgoing,whereOutgoing , null, null);
    o.moveToFirst();

    String numberOfIn = String.valueOf(i.getCount());
    String numberOfOut = String.valueOf(o.getCount());
    String numberOfMiss = String.valueOf(m.getCount());

    inCalls.setText(numberOfIn);
    outCalls.setText(numberOfOut);
    missCalls.setText(numberOfMiss);
Related