Search in sources :

Example 6 with Email

use of i2p.bote.email.Email in project i2p.i2p-bote by i2p.

the class BoteService method emailReceived.

// NewEmailListener
@Override
public void emailReceived(String messageId) {
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder b = new NotificationCompat.Builder(this).setAutoCancel(true).setSmallIcon(R.drawable.ic_notif).setDefaults(Notification.DEFAULT_ALL);
    try {
        EmailFolder inbox = I2PBote.getInstance().getInbox();
        // Set the new email as \Recent
        inbox.setRecent(messageId, true);
        // Now display/update notification with all \Recent emails
        List<Email> newEmails = BoteHelper.getRecentEmails(inbox);
        int numNew = newEmails.size();
        switch(numNew) {
            case 0:
                nm.cancel(NOTIF_ID_NEW_EMAIL);
                return;
            case 1:
                Email email = newEmails.get(0);
                String fromAddress = email.getOneFromAddress();
                Bitmap picture = BoteHelper.getPictureForAddress(fromAddress);
                if (picture != null)
                    b.setLargeIcon(picture);
                else if (!email.isAnonymous()) {
                    int width = getResources().getDimensionPixelSize(R.dimen.notification_large_icon_width);
                    int height = getResources().getDimensionPixelSize(R.dimen.notification_large_icon_height);
                    b.setLargeIcon(BoteHelper.getIdenticonForAddress(fromAddress, width, height));
                } else
                    b.setSmallIcon(R.drawable.ic_contact_picture);
                b.setContentTitle(BoteHelper.getNameAndShortDestination(fromAddress));
                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));
                HashSet<Address> recipients = new HashSet<Address>();
                String bigText = "";
                for (Email ne : newEmails) {
                    recipients.add(BoteHelper.getOneLocalRecipient(ne));
                    bigText += BoteHelper.getNameAndShortDestination(ne.getOneFromAddress());
                    bigText += ": " + ne.getSubject() + "\n";
                }
                b.setContentText(BoteHelper.joinAddressNames(recipients));
                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());
}
Also used : Email(i2p.bote.email.Email) NotificationManager(android.app.NotificationManager) Address(javax.mail.Address) MessagingException(javax.mail.MessagingException) GeneralSecurityException(java.security.GeneralSecurityException) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) PasswordException(i2p.bote.fileencryption.PasswordException) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) EmailFolder(i2p.bote.folder.EmailFolder) HashSet(java.util.HashSet)

Example 7 with Email

use of i2p.bote.email.Email in project i2p.i2p-bote by i2p.

the class BoteHelper method getNumNewEmails.

public static int getNumNewEmails(Context ctx, EmailFolder folder) throws PasswordException, GeneralSecurityException, IOException, MessagingException {
    String selectedIdentityKey = ctx.getSharedPreferences(Constants.SHARED_PREFS, 0).getString(Constants.PREF_SELECTED_IDENTITY, null);
    if (selectedIdentityKey == null)
        return folder.getNumNewEmails();
    int numNew = 0;
    for (Email email : BoteHelper.getEmails(folder, null, true)) {
        if (email.getMetadata().isUnread()) {
            if (BoteHelper.isSentEmail(email)) {
                String senderDest = BoteHelper.extractEmailDestination(email.getOneFromAddress());
                if (selectedIdentityKey.equals(senderDest))
                    numNew++;
            } else {
                for (Address recipient : email.getAllRecipients()) {
                    String recipientDest = BoteHelper.extractEmailDestination(recipient.toString());
                    if (selectedIdentityKey.equals(recipientDest)) {
                        numNew++;
                        break;
                    }
                }
            }
        }
    }
    return numNew;
}
Also used : Email(i2p.bote.email.Email) Address(javax.mail.Address)

Example 8 with Email

use of i2p.bote.email.Email in project i2p.i2p-bote by i2p.

the class BoteHelper method getRecentEmails.

public static List<Email> getRecentEmails(EmailFolder folder) throws PasswordException, MessagingException {
    List<Email> emails = folder.getElements();
    Iterator<Email> iter = emails.iterator();
    while (iter.hasNext()) {
        Email email = iter.next();
        if (!email.isRecent())
            iter.remove();
    }
    return emails;
}
Also used : Email(i2p.bote.email.Email)

Example 9 with Email

use of i2p.bote.email.Email in project i2p.i2p-bote by i2p.

the class EmailFolderTest method setUp.

@Before
public void setUp() throws Exception {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    testDir = new File(tempDir, "EmailFolderTest-" + System.currentTimeMillis());
    folderDir1 = new File(testDir, "folder1");
    folderDir2 = new File(testDir, "folder2");
    email1 = new Email(true);
    email1.setText("Und nun lag die Entscheidung schon nahe vor ihm, es\n" + "war alles klar geworden. Die Kunst war eine schöne Sache,\n" + "aber sie war keine Göttin und kein Ziel, für ihn nicht, nicht\n" + "der Kunst hatte er zu folgen, nur dem Ruf der Mutter. Was\n" + "konnte es nutzen, seine Finger noch immer geschickter zu\n" + "machen? Am Meister Niklaus konnte man sehen, wohin\n" + "das führte. Es führte zu Ruhm und Namen, zu Geld und\n" + "seßhaftem Leben, und zu einer Verdorrung und Verküm-\n" + "merung jener inneren Sinne, denen allein das Geheimnis\n" + "zugänglich ist. Es führte zum Herstellen hübscher kostba-\n" + "rer Spielwaren, zu allerlei reichen Altären und Kanzeln,\n" + "heiligen Sebastianen und hübsch gelockten Engels-\n" + "köpfchen, das Stück zu vier Talern. Oh, das Gold im Aug’\n" + "eines Karpfens und der süße dünne Silberflaum am Rand\n" + "eines Schmetterlingsflügels war unendlich viel schöner,\n" + "lebendiger, köstlicher als ein ganzer Saal voll von jenen\n" + "Kunstwerken.");
    email2 = new Email(false);
    email2.setText("\"And when the trial continued,\" he said in a weeping whisper, \"they asked Prak a most unfortunate thing.\n" + "They asked him,\" he paused and shivered, \"to tell the Truth, the Whole Truth and Nothing but the Truth.\n" + "Only, don't you see?\"\n" + "He suddenly hoisted himself up on to his elbows again and shouted at them.\n" + "\"They'd given him much too much of the drug!\"\n" + "He collapsed again, moaning quietly. \"Much too much too much too much too ...\"\n" + "The group gathered round his bedside glanced at each other. There were goose pimples on backs.\n" + "\"What happened?\" said Zaphod at last.\n" + "\"Oh, he told it all right,\" said the man savagely, \"for all I know he's still telling it now. Strange, terrible\n" + "things ... terrible, terrible!\" he screamed.\n" + "They tried to calm him, but he struggled to his elbows again.\n" + "\"Terrible things, incomprehensible things,\" he shouted, \"things that would drive a man mad!\"\n" + "He stared wildly at them.\n" + "\"Or in my case,\" he said, \"half-mad. I'm a journalist.\"\n" + "\"You mean,\" said Arthur quietly, \"that you are used to confronting the truth?\"\n" + "\"No,\" said the man with a puzzled frown. \"I mean that I made an excuse and left early.\"\n" + "He collapsed into a coma from which he recovered only once and briefly.");
    PasswordCache passwordCache = TestUtil.createPasswordCache(testDir);
    folder1 = new EmailFolder(folderDir1, passwordCache);
    folder2 = new EmailFolder(folderDir2, passwordCache);
}
Also used : PasswordCache(i2p.bote.fileencryption.PasswordCache) Email(i2p.bote.email.Email) File(java.io.File) Before(org.junit.Before)

Example 10 with Email

use of i2p.bote.email.Email in project i2p.i2p-bote by i2p.

the class IncompleteEmailFolderTest method testAddEmail.

private void testAddEmail(String mailContent, int expectedNumPackets) throws Exception {
    Email email = new Email(true);
    String recipient = "test@bote.i2p";
    email.addRecipient(RecipientType.TO, new InternetAddress(recipient));
    email.setText(mailContent);
    EmailIdentity identity = new EmailIdentity("DVkhqF6R9SHB5svViGtqRYZO7oI-0-omnIFtae29fNnNtTTH2j37Fr5fWp4t6rseTjiJ8gwg08DnbA4qP72aSQcDQPSErOELOMSU5BUTtsT8hnv1-DKdhIn~1qoIjxzIFHbxT3xnR3nFI7lKd6couscilzPBCjoFDUKb5ds2u23RO29K7~EKxU1O7Ltu6sT5etXkJkhAziOcuyfZyxJXqH1caYX5e2aWIhY3D2ESfy4nMK66r5KcDVQOPTzCkJq6d1FFOmnDGrlJjN~HgHmfUCtLbO~TLugWx9FCiDGfPkBb-3ODYTDaUR1zobOj1tiffV3Nm73PsYddRt84emLKzIRsC77JJpflw~h8UIRYJ29vJDf4VQ54BhZcelmN192sIrWr2nKN8n6PpSP4LI4RAuG2UvLytnDYzFM7O9WcnFP2-Qs3t1lD9aF72JVTYTpH5PZupnB1cglSsdRg8RmtRa41Fseyx8D3EdH~DCdpMGmfupaWp9~dKpFMleqk9scRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABTjDxn3wEOjCjJ4APg~2IGpqWwy2Hw728aZ3eCC5l0MP913BLdIfSUiXPbs6sN9A2");
    KeyUpdateHandler keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler();
    Collection<UnencryptedEmailPacket> packets = email.createEmailPackets(identity, keyUpdateHandler, recipient, I2PBotePacket.MAX_DATAGRAM_SIZE);
    assertTrue("Expected " + expectedNumPackets + " email packets, got " + packets.size(), packets.size() == expectedNumPackets);
    assertTrue("The inbox should be empty at this point!", inbox.getElements().size() == 0);
    for (UnencryptedEmailPacket emailPacket : packets) incompleteFolder.addEmailPacket(emailPacket);
    assertTrue("The incomplete emails folder is not empty!", incompleteFolder.getElements().size() == 0);
    assertTrue("Expected: one email in the inbox, actual number = " + inbox.getElements().size(), inbox.getElements().size() == 1);
    // Verify that the original email and the email in the folder are the same except for the signature header
    Email storedEmail = inbox.getElements().iterator().next();
    storedEmail.removeHeader("X-I2PBote-Sig-Valid");
    TestUtil.assertEquals("Stored email differs from original email!", email, storedEmail);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Email(i2p.bote.email.Email) KeyUpdateHandler(i2p.bote.crypto.KeyUpdateHandler) EmailIdentity(i2p.bote.email.EmailIdentity) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket)

Aggregations

Email (i2p.bote.email.Email)24 PasswordException (i2p.bote.fileencryption.PasswordException)10 MessagingException (javax.mail.MessagingException)8 IOException (java.io.IOException)7 GeneralSecurityException (java.security.GeneralSecurityException)6 InternetAddress (javax.mail.internet.InternetAddress)6 View (android.view.View)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 Address (javax.mail.Address)4 Bitmap (android.graphics.Bitmap)3 Attachment (i2p.bote.email.Attachment)3 EmailIdentity (i2p.bote.email.EmailIdentity)3 Test (org.junit.Test)3 SuppressLint (android.annotation.SuppressLint)2 ContentAttachment (i2p.bote.android.util.ContentAttachment)2 Person (i2p.bote.android.util.Person)2 ContactsCompletionView (i2p.bote.android.widget.ContactsCompletionView)2 NoIdentityForSenderException (i2p.bote.email.NoIdentityForSenderException)2 EmailFolder (i2p.bote.folder.EmailFolder)2