From 2260d60bac45dd3fde40bdba033dd2e1c86ca2b5 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Mon, 16 Mar 2015 16:18:11 +0100 Subject: [PATCH] K-9 Mail notifications: Hopefully fix #7 and #8 K-9 Mail seems to completely ignore the where clause when querying for messages. That is why we got data from the newest message instead of the one requested. Work around by walking through the messages and match the uri manually. This is not as expensive as I thought because in most cases it is the first message is the one we want anyway. --- .../gadgetbridge/K9Receiver.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/K9Receiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/K9Receiver.java index 109b36cb..765725ca 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/K9Receiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/K9Receiver.java @@ -28,19 +28,36 @@ public class K9Receiver extends BroadcastReceiver { } } - // get sender and subject from the Intent - String sender = intent.getStringExtra("com.fsck.k9.intent.extra.FROM"); - String subject = intent.getStringExtra("com.fsck.k9.intent.extra.SUBJECT"); + String uriWanted = intent.getData().toString(); - // get preview from K9 Content Provider, unfortunately this does not come with the Intent - String[] whereParameters = {intent.getData().toString()}; String[] messagesProjection = { - "preview" + "senderAddress", + "subject", + "preview", + "uri" }; - Cursor c = context.getContentResolver().query(k9Uri, null, "uri=?", whereParameters, " LIMIT 1"); + String sender = ""; + String subject = ""; + String preview = ""; + + /* + * there seems to be no way to specify the the uri in the where clause. + * If we do so, we just get the newest message, not the one requested. + * So, we will just search our message and match the uri manually. + * It should be the first one returned by the query in most cases, + */ + Cursor c = context.getContentResolver().query(k9Uri, messagesProjection, null, null, null); c.moveToFirst(); - String preview = c.getString(c.getColumnIndex("preview")); + do { + String uri = c.getString(c.getColumnIndex("uri")); + if (uri.equals(uriWanted)) { + sender = c.getString(c.getColumnIndex("senderAddress")); + subject = c.getString(c.getColumnIndex("subject")); + preview = c.getString(c.getColumnIndex("preview")); + break; + } + } while (c.moveToNext()); c.close(); Intent startIntent = new Intent(context, BluetoothCommunicationService.class);