From 135633efbdf209caf8495f9e873299282e8e5407 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Sun, 31 Oct 2010 02:19:19 +0000 Subject: Fixed a bug that caused multiple delivery reports to be sent. --- .../BlaMail/src/com/blatech/blamail/BlaMail.java | 37 +++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'workspace/BlaMail/src') diff --git a/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java b/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java index d693355..6e99a12 100644 --- a/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java +++ b/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java @@ -62,7 +62,8 @@ public class BlaMail extends Activity { String host = "imap.gmail.com"; String user = "blamailtest@gmail.com"; String pass = "thankssueaddo"; - String mbox = "Inbox"; + String mbox = "Inbox"; + SmsManager sms = SmsManager.getDefault(); /** Called when the activity is first created. */ @Override @@ -124,16 +125,17 @@ public class BlaMail extends Activity { store.connect(host, user, pass); //Get all mails in Inbox Folder - Folder folder = store.getFolder(mbox); + final Folder folder = store.getFolder(mbox); + if (folder == null || !folder.exists()) { //System.out.println("Invalid folder"); - Toast.makeText(getBaseContext(), "Invalid Folder", + Toast.makeText(getBaseContext(), "Invalid Source Folder", Toast.LENGTH_SHORT).show(); System.exit(1); - } + } - folder.open(Folder.READ_ONLY); - + folder.open(Folder.READ_WRITE); + // Add messageCountListener to listen for new messages folder.addMessageCountListener(new MessageCountAdapter() { public void messagesAdded(MessageCountEvent ev) { @@ -143,8 +145,8 @@ public class BlaMail extends Activity { id = msgs[i].getSubject().substring(0, 19); phoneNumber = msgs[i].getSubject().substring(20, 33); message= msgs[i].getSubject().substring(34); - sendSMS(id, phoneNumber, message); - } + sendSMS(id, phoneNumber, message); + } } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -172,7 +174,7 @@ public class BlaMail extends Activity { PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); - PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, + final PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); //---when the SMS has been sent--- @@ -182,8 +184,7 @@ public class BlaMail extends Activity { switch (getResultCode()) { case Activity.RESULT_OK: - Toast.makeText(getBaseContext(), "SMS sent", - Toast.LENGTH_SHORT).show(); + //sms sent break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), "Generic failure", @@ -204,31 +205,29 @@ public class BlaMail extends Activity { } } }, new IntentFilter(SENT)); - + //---when the SMS has been delivered--- - registerReceiver(new BroadcastReceiver(){ + registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { - SmsManager sms = SmsManager.getDefault(); switch (getResultCode()) { case Activity.RESULT_OK: + deliveredPI.cancel(); if(!id.equals("0000000000000000000")){ + unregisterReceiver(this); sms.sendTextMessage("07927278978", null, "!delivered " + id, null, null); - Toast.makeText(getBaseContext(), "SMS delivered", - Toast.LENGTH_SHORT).show(); } break; case Activity.RESULT_CANCELED: + deliveredPI.cancel(); + unregisterReceiver(this); sms.sendTextMessage("07927278978", null, "\"" + message + "\" was not delivered to " + phoneNumber, null, null); - Toast.makeText(getBaseContext(), "SMS not delivered", - Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED)); - SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); /*GMailSender sender = new GMailSender(user, pass); -- cgit v1.2.3