From kde-commits Sat Mar 31 22:52:51 2018 From: Simon Redman Date: Sat, 31 Mar 2018 22:52:51 +0000 To: kde-commits Subject: [kdeconnect-android/sms-history] src/org/kde/kdeconnect/Helpers: Add custom type for Message Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=152253678606207 Git commit 10e43a93c92fe68f9512665e413d1cddd83e5c8c by Simon Redman. Committed on 31/03/2018 at 22:52. Pushed by sredman into branch 'sms-history'. Add custom type for Message M +37 -23 src/org/kde/kdeconnect/Helpers/SMSHelper.java https://commits.kde.org/kdeconnect-android/10e43a93c92fe68f9512665e413d1cdd= d83e5c8c diff --git a/src/org/kde/kdeconnect/Helpers/SMSHelper.java b/src/org/kde/kd= econnect/Helpers/SMSHelper.java index 3c45011..ffbcccf 100644 --- a/src/org/kde/kdeconnect/Helpers/SMSHelper.java +++ b/src/org/kde/kdeconnect/Helpers/SMSHelper.java @@ -35,19 +35,6 @@ import java.util.Map; = public class SMSHelper { = - /** - * Define the columns which are extracted from the Android SMS database - */ - protected static final String[] smsProjection =3D new String[]{ - Telephony.Sms.ADDRESS, // Phone number of the remote - Telephony.Sms.BODY, // Body of the message - Telephony.Sms.DATE, // Some date associated with the messa= ge (Received?) - Telephony.Sms.TYPE, // Compare with Telephony.TextBasedSms= Columns.MESSAGE_TYPE_* - Telephony.Sms.PERSON, // Some obscure value that corresponds= to the contact - Telephony.Sms.READ, // Whether we have received a read rep= ort for this message (int) - ThreadID.lookupColumn, // Magic number which binds (message) t= hreads - }; - /** * Get the base address for the SMS content *

@@ -98,14 +85,14 @@ public class SMSHelper { * @param context android.content.Context running the request * @return Mapping of thread ID to list of messages in that thread */ - public static Map>> getSMS(Context = context) { - HashMap>> toReturn =3D new Hash= Map<>(); + public static Map> getSMS(Context context) { + HashMap> toReturn =3D new HashMap<>(); = Uri smsUri =3D getSMSUri(); = Cursor smsCursor =3D context.getContentResolver().query( smsUri, - smsProjection, + Message.smsColumns, null, null, null); @@ -115,9 +102,9 @@ public class SMSHelper { do { int thread =3D smsCursor.getInt(threadColumn); if (!toReturn.containsKey(thread)) { - toReturn.put(new ThreadID(thread), new ArrayList>()); + toReturn.put(new ThreadID(thread), new ArrayList()); } - Map messageInfo =3D new HashMap<>(); + Message messageInfo =3D new Message(); for (int columnIdx =3D 0; columnIdx < smsCursor.getColumnC= ount(); columnIdx++) { String colName =3D smsCursor.getColumnName(columnIdx); String body =3D smsCursor.getString(columnIdx); @@ -141,16 +128,16 @@ public class SMSHelper { * 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 + * @return Mapping of thread_id to the first message in each thread */ - public static Map> getConversations(Cont= ext context) { - HashMap> toReturn =3D new HashMap<>(= ); + public static Map getConversations(Context context)= { + HashMap toReturn =3D new HashMap<>(); = Uri conversationUri =3D getConversationUri(); = Cursor conversationsCursor =3D context.getContentResolver().query( conversationUri, - smsProjection, + Message.smsColumns, null, null, null); @@ -160,7 +147,7 @@ public class SMSHelper { do { int thread =3D conversationsCursor.getInt(threadColumn); = - Map messageInfo =3D new HashMap<>(); + Message messageInfo =3D new Message(); for (int columnIdx =3D 0; columnIdx < conversationsCursor.= getColumnCount(); columnIdx++) { String colName =3D conversationsCursor.getColumnName(c= olumnIdx); String body =3D conversationsCursor.getString(columnId= x); @@ -208,5 +195,32 @@ public class SMSHelper { return false; } } + + /** + * Represent a message and all of its interesting data columns + */ + public static class Message extends HashMap { + /** + * Define the columns which are extracted from the Android SMS dat= abase + */ + public static final String[] smsColumns =3D new String[]{ + Telephony.Sms.ADDRESS, // Phone number of the remote + Telephony.Sms.BODY, // Body of the message + Telephony.Sms.DATE, // Some date associated with the m= essage (Received?) + Telephony.Sms.TYPE, // Compare with Telephony.TextBase= dSmsColumns.MESSAGE_TYPE_* + Telephony.Sms.PERSON, // Some obscure value that corresp= onds to the contact + Telephony.Sms.READ, // Whether we have received a read= report for this message (int) + ThreadID.lookupColumn, // Magic number which binds (messag= e) threads + }; + + @Override + public String toString() { + if (this.containsKey(Telephony.Sms.BODY)) { + return this.get(Telephony.Sms.BODY); + } + + return super.toString(); + } + } } =20