Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to get conversation list mode and messages from a number #407

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 97 additions & 44 deletions app/src/main/java/com/termux/api/SmsInboxAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,132 @@
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.provider.Telephony;
import android.provider.Telephony.Sms;
import android.provider.Telephony.Sms.Conversations;
import android.provider.Telephony.TextBasedSmsColumns;
import android.util.JsonWriter;
import android.util.Log;

import com.termux.api.util.ResultReturner;
import com.termux.api.util.ResultReturner.ResultJsonWriter;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static android.provider.Telephony.TextBasedSmsColumns.ADDRESS;
import static android.provider.Telephony.TextBasedSmsColumns.BODY;
import static android.provider.Telephony.TextBasedSmsColumns.DATE;
import static android.provider.Telephony.TextBasedSmsColumns.READ;
import static android.provider.Telephony.TextBasedSmsColumns.THREAD_ID;
import static android.provider.Telephony.TextBasedSmsColumns.TYPE;

public class SmsInboxAPI {

private static final String[] DISPLAY_NAME_PROJECTION = {PhoneLookup.DISPLAY_NAME};

static void onReceive(TermuxApiReceiver apiReceiver, final Context context, Intent intent) {
final int offset = intent.getIntExtra("offset", 0);
final int limit = intent.getIntExtra("limit", 50);
final Uri contentURI = typeToContentURI(intent.getIntExtra("type", TextBasedSmsColumns.MESSAGE_TYPE_INBOX));
final int limit = intent.getIntExtra("limit", 10);
final String number = intent.hasExtra("from") ? intent.getStringExtra("from"):"";
final boolean conversation_list = intent.getBooleanExtra("conversation-list", false);
final Uri contentURI = conversation_list ? typeToContentURI(0) :
typeToContentURI(number==null || number.isEmpty() ?
intent.getIntExtra("type", TextBasedSmsColumns.MESSAGE_TYPE_INBOX): 0);

ResultReturner.returnData(apiReceiver, intent, new ResultJsonWriter() {
@Override
public void writeJson(JsonWriter out) throws Exception {
getAllSms(context, out, offset, limit, contentURI);
if (conversation_list) getConversations(context, out, offset, limit);
else getAllSms(context, out, offset, limit, number, contentURI);
}
});
}

@SuppressLint("SimpleDateFormat")
public static void getAllSms(Context context, JsonWriter out, int offset, int limit, Uri contentURI) throws IOException {
public static void getConversations(Context context, JsonWriter out, int offset, int limit) throws IOException {
ContentResolver cr = context.getContentResolver();
String sortOrder = "date DESC LIMIT + " + limit + " OFFSET " + offset;
try (Cursor c = cr.query(contentURI, null, null, null, sortOrder)) {
String sortOrder = "date DESC";
try (Cursor c = cr.query(Conversations.CONTENT_URI, null, null, null , sortOrder)) {
c.moveToFirst();

c.moveToLast();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
Map<String, String> nameCache = new HashMap<>();

out.beginArray();
for (int i = 0, count = c.getCount(); i < count; i++) {
int threadID = c.getInt(c.getColumnIndexOrThrow(TextBasedSmsColumns.THREAD_ID));
String smsAddress = c.getString(c.getColumnIndexOrThrow(TextBasedSmsColumns.ADDRESS));
String smsBody = c.getString(c.getColumnIndexOrThrow(TextBasedSmsColumns.BODY));
boolean read = (c.getInt(c.getColumnIndex(TextBasedSmsColumns.READ)) != 0);
long smsReceivedDate = c.getLong(c.getColumnIndexOrThrow(TextBasedSmsColumns.DATE));
// long smsSentDate = c.getLong(c.getColumnIndexOrThrow(TextBasedSmsColumns.DATE_SENT));

String smsSenderName = getContactNameFromNumber(nameCache, context, smsAddress);
String messageType = getMessageType(c.getInt(c.getColumnIndexOrThrow(TextBasedSmsColumns.TYPE)));

out.beginObject();
out.name("threadid").value(threadID);
out.name("type").value(messageType);
out.name("read").value(read);

if (smsSenderName != null) {
out.name("sender").value(smsSenderName);
int id = c.getInt(c.getColumnIndex(THREAD_ID));

Cursor cc = cr.query(Sms.CONTENT_URI, null,
THREAD_ID + " == '" + id +"'",
null, "date DESC");
if (cc.getCount() == 0) {
c.moveToNext();
continue;
}
out.name("number").value(smsAddress);

out.name("received").value(dateFormat.format(new Date(smsReceivedDate)));
// if (Math.abs(smsReceivedDate - smsSentDate) >= 60000) {
// out.write(" (sent ");
// out.write(dateFormat.format(new Date(smsSentDate)));
// out.write(")");
// }
out.name("body").value(smsBody);

c.moveToPrevious();
out.endObject();
cc.moveToFirst();
writeElement(cc, out, nameCache, context);
cc.close();
c.moveToNext();
}
out.endArray();
}
}

@SuppressLint("SimpleDateFormat")
private static void writeElement(Cursor c, JsonWriter out, Map<String, String> nameCache ,Context context) throws IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm");

int threadID = c.getInt(c.getColumnIndexOrThrow(THREAD_ID));
String smsAddress = c.getString(c.getColumnIndexOrThrow(ADDRESS));
String smsBody = c.getString(c.getColumnIndexOrThrow(BODY));
boolean read = (c.getInt(c.getColumnIndex(READ)) != 0);
long smsReceivedDate = c.getLong(c.getColumnIndexOrThrow(DATE));
// long smsSentDate = c.getLong(c.getColumnIndexOrThrow(TextBasedSmsColumns.DATE_SENT));

String smsSenderName = getContactNameFromNumber(nameCache, context, smsAddress);
String messageType = getMessageType(c.getInt(c.getColumnIndexOrThrow(TYPE)));

out.beginObject();
out.name("threadid").value(threadID);
out.name("type").value(messageType);
out.name("read").value(read);

if (smsSenderName != null) {
out.name("sender").value(smsSenderName);
}
out.name("number").value(smsAddress);

out.name("received").value(dateFormat.format(new Date(smsReceivedDate)));
// if (Math.abs(smsReceivedDate - smsSentDate) >= 60000) {
// out.write(" (sent ");
// out.write(dateFormat.format(new Date(smsSentDate)));
// out.write(")");
// }
out.name("body").value(smsBody);

out.endObject();
}


@SuppressLint("SimpleDateFormat")
public static void getAllSms(Context context, JsonWriter out, int offset, int limit, String number, Uri contentURI) throws IOException {
ContentResolver cr = context.getContentResolver();
String sortOrder = "date DESC LIMIT + " + limit + " OFFSET " + offset;
try (Cursor c = cr.query(contentURI, null,
ADDRESS + " LIKE '%" + number + "%'",null, sortOrder)) {
c.moveToFirst();

new SimpleDateFormat("yyyy-MM-dd kk:mm");
Map<String, String> nameCache = new HashMap<>();

out.beginArray();
for (int i = 0, count = c.getCount(); i < count; i++) {
writeElement(c, out, nameCache, context);
c.moveToNext();
}
out.endArray();
}
Expand Down Expand Up @@ -116,17 +170,16 @@ private static String getMessageType(int type) {

private static Uri typeToContentURI(int type) {
switch (type) {
case TextBasedSmsColumns.MESSAGE_TYPE_ALL:
return Telephony.Sms.CONTENT_URI;
case TextBasedSmsColumns.MESSAGE_TYPE_SENT:
return Telephony.Sms.Sent.CONTENT_URI;
return Sms.Sent.CONTENT_URI;
case TextBasedSmsColumns.MESSAGE_TYPE_DRAFT:
return Telephony.Sms.Draft.CONTENT_URI;
return Sms.Draft.CONTENT_URI;
case TextBasedSmsColumns.MESSAGE_TYPE_OUTBOX:
return Telephony.Sms.Outbox.CONTENT_URI;
return Sms.Outbox.CONTENT_URI;
case TextBasedSmsColumns.MESSAGE_TYPE_INBOX:
case TextBasedSmsColumns.MESSAGE_TYPE_ALL:
default:
return Telephony.Sms.Inbox.CONTENT_URI;
return Sms.CONTENT_URI;
}
}

Expand Down