Notify on new emails
FolderListener is not ideal, it fires on any change. We only want to listen for new emails arriving (not user marking emails unread), and each email should only appear in a notification once.
This commit is contained in:
@@ -41,6 +41,11 @@
|
||||
<string name="folder_empty">Folder is empty</string>
|
||||
<string name="folder_does_not_exist">Folder does not exist</string>
|
||||
|
||||
<plurals name="n_new_emails">
|
||||
<item quantity="one">One new email</item>
|
||||
<item quantity="other">%d new emails</item>
|
||||
</plurals>
|
||||
|
||||
<string name="queued">Queued</string>
|
||||
<string name="sending">Sending</string>
|
||||
<string name="sent_to_short">Sent %1$d of %2$d</string>
|
||||
|
||||
@@ -1,18 +1,35 @@
|
||||
package i2p.bote.android.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
|
||||
import net.i2p.router.Router;
|
||||
import net.i2p.router.RouterContext;
|
||||
import net.i2p.router.RouterLaunch;
|
||||
import i2p.bote.I2PBote;
|
||||
import i2p.bote.android.EmailListActivity;
|
||||
import i2p.bote.android.R;
|
||||
import i2p.bote.android.ViewEmailActivity;
|
||||
import i2p.bote.android.service.Init.RouterChoice;
|
||||
import i2p.bote.android.util.BoteHelper;
|
||||
import i2p.bote.email.Email;
|
||||
import i2p.bote.fileencryption.PasswordException;
|
||||
import i2p.bote.folder.EmailFolder;
|
||||
import i2p.bote.folder.FolderListener;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
public class BoteService extends Service {
|
||||
public class BoteService extends Service implements FolderListener {
|
||||
public static final String ROUTER_CHOICE = "router_choice";
|
||||
public static final int NOTIF_ID_NEW_EMAIL = 80739047;
|
||||
|
||||
RouterChoice mRouterChoice;
|
||||
RouterContext mRouterContext;
|
||||
@@ -22,7 +39,11 @@ public class BoteService extends Service {
|
||||
mRouterChoice = (RouterChoice) intent.getSerializableExtra(ROUTER_CHOICE);
|
||||
if (mRouterChoice == RouterChoice.INTERNAL)
|
||||
new Thread(new RouterStarter()).start();
|
||||
|
||||
I2PBote.getInstance().startUp();
|
||||
|
||||
I2PBote.getInstance().getInbox().addFolderListener(this);
|
||||
|
||||
return START_REDELIVER_INTENT;
|
||||
}
|
||||
|
||||
@@ -33,7 +54,10 @@ public class BoteService extends Service {
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
I2PBote.getInstance().getInbox().removeFolderListener(this);
|
||||
|
||||
I2PBote.getInstance().shutDown();
|
||||
|
||||
if (mRouterChoice == RouterChoice.INTERNAL)
|
||||
new Thread(new RouterStopper()).start();
|
||||
}
|
||||
@@ -59,4 +83,87 @@ public class BoteService extends Service {
|
||||
ctx.router().shutdown(Router.EXIT_HARD);
|
||||
}
|
||||
}
|
||||
|
||||
// FolderListener
|
||||
|
||||
@Override
|
||||
public void elementAdded() {
|
||||
notifyUnread();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementUpdated() {
|
||||
notifyUnread();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementRemoved() {
|
||||
notifyUnread();
|
||||
}
|
||||
|
||||
private void notifyUnread() {
|
||||
NotificationManager nm = (NotificationManager) getSystemService(
|
||||
Context.NOTIFICATION_SERVICE);
|
||||
|
||||
NotificationCompat.Builder b =
|
||||
new NotificationCompat.Builder(this)
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.setAutoCancel(true);
|
||||
|
||||
try {
|
||||
EmailFolder inbox = I2PBote.getInstance().getInbox();
|
||||
List<Email> newEmails = BoteHelper.getNewEmails(inbox);
|
||||
int numNew = newEmails.size();
|
||||
switch (numNew) {
|
||||
case 0:
|
||||
nm.cancel(NOTIF_ID_NEW_EMAIL);
|
||||
return;
|
||||
|
||||
case 1:
|
||||
Email email = newEmails.get(0);
|
||||
b.setContentTitle(BoteHelper.getNameAndShortDestination(
|
||||
email.getOneFromAddress()));
|
||||
b.setContentText(email.getSubject());
|
||||
|
||||
Intent vei = new Intent(this, ViewEmailActivity.class);
|
||||
vei.putExtra(ViewEmailActivity.FOLDER_NAME, inbox.getName());
|
||||
vei.putExtra(ViewEmailActivity.MESSAGE_ID, email.getMessageID());
|
||||
vei.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
PendingIntent pvei = PendingIntent.getActivity(this, 0, vei, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
b.setContentIntent(pvei);
|
||||
break;
|
||||
|
||||
default:
|
||||
b.setContentTitle(getResources().getQuantityString(
|
||||
R.plurals.n_new_emails, numNew, numNew));
|
||||
|
||||
String bigText = "";
|
||||
for (Email ne : newEmails) {
|
||||
bigText += BoteHelper.getNameAndShortDestination(
|
||||
ne.getOneFromAddress());
|
||||
bigText += ": " + ne.getSubject() + "\n";
|
||||
}
|
||||
b.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));
|
||||
|
||||
Intent eli = new Intent(this, EmailListActivity.class);
|
||||
eli.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
PendingIntent peli = PendingIntent.getActivity(this, 0, eli, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
b.setContentIntent(peli);
|
||||
}
|
||||
} catch (PasswordException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (MessagingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (GeneralSecurityException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
nm.notify(NOTIF_ID_NEW_EMAIL, b.build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package i2p.bote.android.util;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.MessagingException;
|
||||
@@ -206,4 +208,15 @@ public class BoteHelper extends GeneralHelper {
|
||||
public static boolean isOutbox(String folderName) {
|
||||
return "Outbox".equalsIgnoreCase(folderName);
|
||||
}
|
||||
|
||||
public static List<Email> getNewEmails(EmailFolder folder) throws PasswordException {
|
||||
List<Email> emails = folder.getElements();
|
||||
Iterator<Email> iter = emails.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Email email = iter.next();
|
||||
if (!email.isUnread())
|
||||
iter.remove();
|
||||
}
|
||||
return emails;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user