Git commit 6a983dd0bde78f05f25473524e0a4f8c94556e37 by Simon Redman. Committed on 31/03/2018 at 22:26. Pushed by sredman into branch 'sms-history'. Add helper to get latest message from each conversation (both mms and sms) M +63 -4 src/org/kde/kdeconnect/Helpers/SMSHelper.java https://commits.kde.org/kdeconnect-android/6a983dd0bde78f05f25473524e0a4f8c= 94556e37 diff --git a/src/org/kde/kdeconnect/Helpers/SMSHelper.java b/src/org/kde/kd= econnect/Helpers/SMSHelper.java index 5650065..9272de9 100644 --- a/src/org/kde/kdeconnect/Helpers/SMSHelper.java +++ b/src/org/kde/kdeconnect/Helpers/SMSHelper.java @@ -77,6 +77,19 @@ public class SMSHelper { } } = + /** + * Get the base address for all message conversations + */ + protected static Uri getConversationUri() { + if (Build.VERSION.SDK_INT >=3D Build.VERSION_CODES.KITKAT) { + return Telephony.MmsSms.CONTENT_CONVERSATIONS_URI; + } else { + // As with getSMSUriBad, this is potentially unsafe depending = on whether a specific + // manufacturer decided to do their own thing + return Uri.parse("content://mms-sms/conversations"); + } + } + /** * Get all the SMS threads on the phone as well as a bunch of useful-l= ooking data * @@ -98,7 +111,7 @@ public class SMSHelper { null, null); = - if (smsCursor.moveToFirst()) { + if (smsCursor !=3D null && smsCursor.moveToFirst()) { int threadColumn =3D smsCursor.getColumnIndexOrThrow(Telephony= .Sms.THREAD_ID); do { int thread =3D smsCursor.getInt(threadColumn); @@ -114,12 +127,58 @@ public class SMSHelper { } toReturn.get(thread).add(messageInfo); } while (smsCursor.moveToNext()); - } else - { + } else { // No SMSes available? } = - return toReturn; + if (smsCursor !=3D null){ + smsCursor.close(); + } + + return toReturn; + } + + /** + * Get the last message from each conversation. Can use those thread_i= ds to look up more + * messages in those conversations + * + * @param context android.content.Context running the request + * @return Mapping of thread_id to the interesting information from th= e first message in each thread + */ + public static Map> getConversations(Conte= xt context) { + HashMap> toReturn =3D new HashMap<>(); + + Uri conversationUri =3D getConversationUri(); + + Cursor conversationsCursor =3D context.getContentResolver().query( + conversationUri, + smsProjection, + null, + null, + null); + + if (conversationsCursor !=3D null && conversationsCursor.moveToFir= st()) { + int threadColumn =3D conversationsCursor.getColumnIndexOrThrow= (Telephony.Sms.THREAD_ID); + do { + int thread =3D conversationsCursor.getInt(threadColumn); + + Map messageInfo =3D new HashMap<>(); + for (int columnIdx =3D 0; columnIdx < conversationsCursor.= getColumnCount(); columnIdx++) { + String colName =3D conversationsCursor.getColumnName(c= olumnIdx); + String body =3D conversationsCursor.getString(columnId= x); + messageInfo.put(colName, body); + } + toReturn.put(thread, messageInfo); + } while (conversationsCursor.moveToNext()); + } else { + // No conversations available? + } + + if (conversationsCursor !=3D null){ + conversationsCursor.close(); + } + + return toReturn; } = }