Sree Reddy Menon Sree Reddy Menon - 15 days ago 10
Android Question

Android Call Log data between two dates and Total duration of calls from same number.

want to get the call log history by today, yesterday, last seven days and last 30days along with that i want to show the total duration of incoming and outgoing calls of that particular number.

suppose abc has 3 outgoing and 1 incoming calls. i should get the total duration of those calls.

just let me know if we can get duration and calls log by cursor GroupBy or ORDER BY clause rather than looping and adding duration. Just give me rough structure for better solution and can work effectively .

String[] whereValue=null;
Calendar calendar = Calendar.getInstance();
String currentDate = String.valueOf(calendar.getTimeInMillis());
switch (period) {
case DAY:
whereValue = new String[]{getTimeFrom(period),currentDate};
break;
case YESTERDAY:
whereValue = new String[]{getTimeFrom(period),currentDate};
break;
case WEEK:
whereValue = new String[]{getTimeFrom(period),currentDate};

break;
case MONTH:
whereValue = new String[]{getTimeFrom(period),currentDate};
break;
default:
Log.d(Utils.LOG_TAG, "Error:");
}
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Uri callUri = Uri.parse("content://call_log/calls");

Cursor cur = context.getContentResolver().query(callUri, null, android.provider.CallLog.Calls.DATE+" BETWEEN ? AND ?", whereValue, strOrder);

String callNumber = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.NUMBER));

String callName = cur
.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
String callType = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.TYPE));
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
case CallLog.Calls.INCOMING_TYPE:
break;

}
String duration = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.DURATION));


the above code is even not working for getting call log between fromdate to till date. any help?

Answer

I have managed to get the call log between two dates. You can get call log of today, yesterday, last seven days, last 30days. as well duration of calls (calls from same number multiple times also)

you should pass selection as
android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?" and

selectionArgs

whereValue = new String[]{String.valueOf(calendar1.getTimeInMillis()),String.valueOf(calendar.getTimeInMillis());};

  Map<String, StatEntry> callLogMap1 = new HashMap<>();
            callLogMap1.clear();

            String strOrder1 = android.provider.CallLog.Calls.DATE + " DESC limit 500";

            Uri callUri = Uri.parse("content://call_log/calls");
            Cursor cur = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue,
                    strOrder1);


            if (cur != null) {
                try {
                    while (cur.moveToNext()) {

                        String callNumber = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                        //    String callDate = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.DATE));
                        int duration = cur.getInt(cur.getColumnIndex(android.provider.CallLog.Calls.DURATION));

                        String name = cur.getString(cur.getColumnIndex(CallLog.Calls.CACHED_NAME));

                        StatEntry StatEntry1 = null;

                        int id = cur.getInt(cur.getColumnIndex(CallLog.Calls._ID));

                        int type = cur.getInt(cur.getColumnIndex(CallLog.Calls.TYPE));

                        if (callNumber != null & duration > 0 && (type == 1 || type == 2)) {

                            int n = callNumber.length();
                            String lastDigits;
String number = callNumber.replaceAll(Pattern.quote("+"), ""); //replacing the plus
    //am just checking last 5digitis and saving to map so that we can get same //number duration
                            if (n >= 5) {

                                try {

                                    lastDigits = String.valueOf(Long.parseLong(number) % 100000);
                                } catch (NumberFormatException e) {
                                    e.printStackTrace();
                                    lastDigits = callNumber;
                                }
                            } else {
                                lastDigits = callNumber;
                            }

                            if (callLogMap1.containsKey(lastDigits)) {
                                StatEntry1 = callLogMap1.get(callNumber);

                                    StatEntry1.setTitle(callNumber);
                                StatEntry1.Duration += duration;


                            } else {
                                StatEntry1 = new StatEntry();

                  StatEntry1.setTitle(callNumber);

      StatEntry1.Duration += duration;
                           }


                    StatEntry1.setTime((StatEntry1.Duration) / 60);


                            callLogMap1.put(callNumber, StatEntry1);


                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace(

                    );
                } finally {
                    cur.close();
                }
            }

atlast passing hashmap data to arraylist.

ArrayList<StatEntry> callLogList1 = new ArrayList<>(callLogMap1.size());

        if (callLogMap1.size() > 0) {
            for (Map.Entry<String, StatEntry> entry : callLogMap1.entrySet()) {
                callLogList1.add(entry.getValue());


            }
        }

hope this will be helpful for viewers.