1
0
mirror of https://github.com/danog/termux-api.git synced 2024-11-30 04:19:20 +01:00

Add CallLogAPI (#170)

This commit is contained in:
David Kramer 2018-06-05 16:20:40 -06:00 committed by Fredrik Fornwall
parent 9bf693380d
commit 0ead304e4b
3 changed files with 107 additions and 0 deletions

View File

@ -5,6 +5,7 @@
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- Resolve phone numbers to contact names: -->

View File

@ -0,0 +1,101 @@
package com.termux.api;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.CallLog;
import android.util.JsonWriter;
import com.termux.api.util.ResultReturner;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* API that allows you to get call log history information
*/
public class CallLogAPI {
static void onReceive(final Context context, final Intent intent) {
final int offset = intent.getIntExtra("offset", 0);
final int limit = intent.getIntExtra("limit", 50);
ResultReturner.returnData(context, intent, new ResultReturner.ResultJsonWriter() {
public void writeJson(JsonWriter out) throws Exception {
getCallLogs(context, out, offset, limit);
}
});
}
private static void getCallLogs(Context context, JsonWriter out, int offset, int limit) throws IOException {
ContentResolver cr = context.getContentResolver();
String sortOrder = "date DESC LIMIT + " + limit + " OFFSET " + offset;
try (Cursor cur = cr.query(CallLog.Calls.CONTENT_URI, null, null, null, sortOrder)) {
cur.moveToLast();
int nameIndex = cur.getColumnIndex(CallLog.Calls.CACHED_NAME);
int numberIndex = cur.getColumnIndex(CallLog.Calls.NUMBER);
int dateIndex = cur.getColumnIndex(CallLog.Calls.DATE);
int durationIndex = cur.getColumnIndex(CallLog.Calls.DURATION);
int callTypeIndex = cur.getColumnIndex(CallLog.Calls.TYPE);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.getDefault());
out.beginArray();
for (int j = 0, count = cur.getCount(); j < count; ++j) {
out.beginObject();
out.name("name").value(getCallerNameString(cur.getString(nameIndex)));
out.name("phone_number").value(cur.getString(numberIndex));
out.name("type").value(getCallTypeString(cur.getInt(callTypeIndex)));
out.name("date").value(getDateString(cur.getLong(dateIndex), dateFormat));
out.name("duration").value(getTimeString(cur.getInt(durationIndex)));
cur.moveToPrevious();
out.endObject();
}
out.endArray();
}
}
private static String getCallTypeString(int type) {
switch (type) {
case CallLog.Calls.BLOCKED_TYPE: return "BLOCKED";
case CallLog.Calls.INCOMING_TYPE: return "INCOMING";
case CallLog.Calls.MISSED_TYPE: return "MISSED";
case CallLog.Calls.OUTGOING_TYPE: return "OUTGOING";
case CallLog.Calls.REJECTED_TYPE: return "REJECTED";
case CallLog.Calls.VOICEMAIL_TYPE: return "VOICEMAIL";
default: return "UNKNOWN_TYPE";
}
}
private static String getCallerNameString(String name) {
return name == null ? "UNKNOWN_CALLER" : name;
}
private static String getDateString(Long date, DateFormat dateFormat) {
return dateFormat.format(new Date(date));
}
private static String getTimeString(int totalSeconds) {
int hours = (totalSeconds / 3600);
int mins = (totalSeconds % 3600) / 60;
int secs = (totalSeconds % 60);
String result = "";
// only show hours if we have them
if (hours > 0) {
result += String.format(Locale.getDefault(), "%02d:", hours);
}
result += String.format(Locale.getDefault(), "%02d:%02d", mins, secs);
return result;
}
}

View File

@ -4,6 +4,7 @@ import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.CallLog;
import com.termux.api.util.TermuxApiLogger;
import com.termux.api.util.TermuxApiPermissionActivity;
@ -33,6 +34,10 @@ public class TermuxApiReceiver extends BroadcastReceiver {
PhotoAPI.onReceive(this, context, intent);
}
break;
case "CallLog":
if (TermuxApiPermissionActivity.checkAndRequestPermissions(context, intent, Manifest.permission.READ_CALL_LOG)) {
CallLogAPI.onReceive(context, intent);
}
case "Clipboard":
ClipboardAPI.onReceive(this, context, intent);
break;