@SuppressLint("Range")
private suspend fun lastCallDetail(): String {
val delayDuration = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) 3000L else 2000L
delay(delayDuration)
var callDuration = "0"
try {
val cur: Cursor? = contentResolver.query(
CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC limit 1;"
)
if (cur != null) {
while (cur.moveToNext()) {
callDuration = cur.getString(cur.getColumnIndex(CallLog.Calls.DURATION))
break
}
cur.close()
}
} catch (e : Exception) {
e.printStackTrace()
}
return callDuration
}
private suspend fun lastCallIncoming(): String {
delay(3000)
var callDuration = "00:00:00"
try {
val cur = applicationContext.contentResolver.query(
CallLog.Calls.CONTENT_URI,
null,
null,
null,
CallLog.Calls.DATE + " DESC"
)
val duration = cur!!.getColumnIndex(CallLog.Calls.DURATION)
while (cur.moveToNext()) {
callDuration = cur.getString(duration)
Log.d("callTracking", callDuration)
break
}
cur.close()
val timeSec = callDuration.toInt() // Json output
callDuration = timeSec.toString() //hh:mm:ss formatted string
Log.d("callTracking", callDuration)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return callDuration
}
private fun secToTime(second: Int): String {
var sec = second
val hours = sec / 3600
val minutes = sec % 3600/ 60
sec %= 60
return String.format("%02d:%02d:%02d", hours, minutes, sec)
}
I faced an issue with my code-
check this situation – suppose I call client 1 and the duration is 50 seconds and it fetched correctly now I call client 2 its duration is 2 minutes which also fetched successfully now when I again call client 1 he doesn’t pick up the call duration is fetched here wrong 50 seconds which is the last second call duration and not the last call which is 0.
it works in new devices that have the(top to bottom) call history but it creates issues on those devices have bottom-to-top call history
**I also try this solution that returns the last-second call duration and not the last call but in some devices, it works fine where the devices have ToptoBottom call records.** -
@SuppressLint("Range")
private suspend fun lastCallDetail(): String {
val delayDuration = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) 3000L else 2000L
delay(delayDuration)
var callDuration = "0"
try {
val callLogCursor: Cursor? = contentResolver.query(
CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC limit 1;"
)
if (callLogCursor != null) {
// Check the order of the call logs
val isOrderTopToBottom = determineCallLogOrder(callLogCursor)
// Move accoreding to the determined order
callLogCursor.moveToFirst()
val durationIndex = callLogCursor.getColumnIndex(CallLog.Calls.DURATION)
if (durationIndex != -1) {
if (isOrderTopToBottom) {
callDuration = callLogCursor.getString(durationIndex)
} else {
callLogCursor.moveToLast()
callDuration = callLogCursor.getString(durationIndex)
}
}
callLogCursor.close()
}
} catch (e: Exception) {
e.printStackTrace()
}
return callDuration
}
@SuppressLint("Range")
private suspend fun lastCallIncoming(): String {
delay(3000)
var callDuration = "00:00:00"
try {
val callLogCursor = applicationContext.contentResolver.query(
CallLog.Calls.CONTENT_URI,
null,
null,
null,
CallLog.Calls.DATE + " DESC"
)
if (callLogCursor != null) {
// Check the order of the call logs
val isOrderTopToBottom = determineCallLogOrder(callLogCursor)
callLogCursor.moveToFirst()
val durationIndex = callLogCursor.getColumnIndex(CallLog.Calls.DURATION)
// Move according to the determined order
if (durationIndex != -1) {
if (isOrderTopToBottom) {
callDuration = callLogCursor.getString(durationIndex)
} else {
callLogCursor.moveToLast()
callDuration = callLogCursor.getString(durationIndex)
}
val timeSec = callDuration.toInt()
callDuration = secToTime(timeSec)
Log.d("callTracking", callDuration)
}
callLogCursor.close()
}
} catch (e: Exception) {
e.printStackTrace()
}
return callDuration
}
*here query fetches call logs in descending order (CallLog.Calls.DATE + " DESC"),
according to the latest entries are at the top*
private fun determineCallLogOrder(cursor: Cursor): Boolean {
var isOrderTopToBottom = true
if (cursor.moveToFirst()) {
val dateColumnIndex = cursor.getColumnIndex(CallLog.Calls.DATE)
if (dateColumnIndex != -1) {
val firstTimestamp = cursor.getLong(dateColumnIndex)
if (cursor.moveToNext()) {
val secondTimestamp = cursor.getLong(dateColumnIndex)
isOrderTopToBottom = firstTimestamp > secondTimestamp
}
}
}
return isOrderTopToBottom
}
private fun secToTime(second: Int): String {
var sec = second
val hours = sec / 3600
val minutes = sec % 3600/ 60
sec %= 60
return String.format("%02d:%02d:%02d", hours, minutes, sec)
}
**The query fetches call logs in descending order (CallLog.Calls.DATE + " DESC"),
according to the latest entries are at the top iterate through the call log entries to find the one with the latest timestamp.**