package com.termux.api; import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract.PhoneLookup; import android.provider.Telephony; import android.provider.Telephony.TextBasedSmsColumns; import android.util.JsonWriter; import com.termux.api.util.ResultReturner; import com.termux.api.util.ResultReturner.ResultJsonWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * Call with *
** $ am broadcast --user 0 -n net.aterm.extras/.SmsLister * * Broadcasting: Intent { cmp=net.aterm.extras/.SmsLister } * Broadcast completed: result=13, data="http://fornwall.net" **/ 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); ResultReturner.returnData(apiReceiver, intent, new ResultJsonWriter() { @Override public void writeJson(JsonWriter out) throws Exception { getAllSms(context, out, offset, limit); } }); } @SuppressLint("SimpleDateFormat") public static void getAllSms(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(Telephony.Sms.Inbox.CONTENT_URI, null, null, null, sortOrder)) { c.moveToLast(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm"); Map