Search in sources :

Example 1 with Person

use of i2p.bote.android.util.Person in project i2p.i2p-bote by i2p.

the class NewEmailFragment method sendEmail.

private boolean sendEmail() {
    Email email = new Email(I2PBote.getInstance().getConfiguration().getIncludeSentTime());
    try {
        // Set sender
        EmailIdentity sender = (EmailIdentity) mSpinner.getSelectedItem();
        InternetAddress ia = new InternetAddress(sender == null ? "Anonymous" : BoteHelper.getNameAndDestination(sender.getKey()));
        email.setFrom(ia);
        // We must continue to set "Sender:" even with only one mailbox
        // in "From:", which is against RFC 2822 but required for older
        // Bote versions to see a sender (and validate the signature).
        email.setSender(ia);
        for (Object obj : mTo.getObjects()) {
            Person person = (Person) obj;
            email.addRecipient(Message.RecipientType.TO, new InternetAddress(person.getAddress(), person.getName()));
        }
        if (mMoreVisible) {
            for (Object obj : mCc.getObjects()) {
                Person person = (Person) obj;
                email.addRecipient(Message.RecipientType.CC, new InternetAddress(person.getAddress(), person.getName()));
            }
            for (Object obj : mBcc.getObjects()) {
                Person person = (Person) obj;
                email.addRecipient(Message.RecipientType.BCC, new InternetAddress(person.getAddress(), person.getName()));
            }
        }
        // Check that we have someone to send to
        Address[] rcpts = email.getAllRecipients();
        if (rcpts == null || rcpts.length == 0) {
            // No recipients
            mTo.setError(getActivity().getString(R.string.add_one_recipient));
            mTo.requestFocus();
            return false;
        } else {
            mTo.setError(null);
        }
        email.setSubject(mSubject.getText().toString(), "UTF-8");
        // Extract the attachments
        List<Attachment> attachments = new ArrayList<Attachment>();
        for (int i = 0; i < mAttachments.getChildCount(); i++) {
            View v = mAttachments.getChildAt(i);
            // Warning views don't have tags set
            if (v.getTag() != null)
                attachments.add((Attachment) v.getTag());
        }
        // Set the text and add attachments
        email.setContent(mContent.getText().toString(), attachments);
        // Cache the fact that we sent this email
        BoteHelper.setEmailSent(email, true);
        // Send the email
        I2PBote.getInstance().sendEmail(email);
        // Clean up attachments
        for (Attachment attachment : attachments) {
            if (!attachment.clean())
                Log.e(Constants.ANDROID_LOG_TAG, "Can't clean up attachment: <" + attachment + ">");
        }
        return true;
    } catch (PasswordException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (AddressException 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();
    }
    return false;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Email(i2p.bote.email.Email) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) Attachment(i2p.bote.email.Attachment) ContentAttachment(i2p.bote.android.util.ContentAttachment) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ContactsCompletionView(i2p.bote.android.widget.ContactsCompletionView) SuppressLint(android.annotation.SuppressLint) PasswordException(i2p.bote.fileencryption.PasswordException) AddressException(javax.mail.internet.AddressException) Person(i2p.bote.android.util.Person)

Example 2 with Person

use of i2p.bote.android.util.Person in project i2p.i2p-bote by i2p.

the class NewEmailFragment method onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mSpinner = (Spinner) view.findViewById(R.id.sender_spinner);
    mMore = (ImageView) view.findViewById(R.id.more);
    mTo = (ContactsCompletionView) view.findViewById(R.id.to);
    mCc = (ContactsCompletionView) view.findViewById(R.id.cc);
    mBcc = (ContactsCompletionView) view.findViewById(R.id.bcc);
    mSubject = (EditText) view.findViewById(R.id.subject);
    mContent = (EditText) view.findViewById(R.id.message);
    mAttachments = (LinearLayout) view.findViewById(R.id.attachments);
    String quoteMsgFolder = getArguments().getString(QUOTE_MSG_FOLDER);
    String quoteMsgId = getArguments().getString(QUOTE_MSG_ID);
    QuoteMsgType quoteMsgType = (QuoteMsgType) getArguments().getSerializable(QUOTE_MSG_TYPE);
    boolean hide = I2PBote.getInstance().getConfiguration().getHideLocale();
    List<Person> toRecipients = new ArrayList<Person>();
    List<Person> ccRecipients = new ArrayList<Person>();
    String origSubject = null;
    String origContent = null;
    String origFrom = null;
    try {
        Email origEmail = BoteHelper.getEmail(quoteMsgFolder, quoteMsgId);
        if (origEmail != null) {
            mSenderKey = BoteHelper.extractEmailDestination(BoteHelper.getOneLocalRecipient(origEmail).toString());
            if (quoteMsgType == QuoteMsgType.REPLY) {
                String recipient = BoteHelper.getNameAndDestination(origEmail.getReplyAddress(I2PBote.getInstance().getIdentities()));
                toRecipients.add(extractPerson(recipient));
            } else if (quoteMsgType == QuoteMsgType.REPLY_ALL) {
                // What happens if an email is received by multiple local identities?
                for (Address address : origEmail.getAllAddresses(true)) {
                    Person person = extractPerson(address.toString());
                    if (person != null)
                        toRecipients.add(person);
                }
            }
            origSubject = origEmail.getSubject();
            origContent = origEmail.getText();
            origFrom = BoteHelper.getShortSenderName(origEmail.getOneFromAddress(), 50);
        }
    } catch (PasswordException e) {
        // Should not happen, we cannot get to this page without authenticating
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Set up identities spinner
    IdentityAdapter identities = new IdentityAdapter(getActivity());
    mSpinner.setAdapter(identities);
    mSpinner.setSelection(mDefaultPos);
    // Set up Cc/Bcc button
    mMore.setImageDrawable(new IconicsDrawable(getActivity(), GoogleMaterial.Icon.gmd_unfold_more).colorRes(R.color.md_grey_600).sizeDp(24).paddingDp(3));
    mMore.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            mCc.setVisibility(mMoreVisible ? View.GONE : View.VISIBLE);
            mBcc.setVisibility(mMoreVisible ? View.GONE : View.VISIBLE);
            mMore.setImageDrawable(new IconicsDrawable(getActivity(), mMoreVisible ? GoogleMaterial.Icon.gmd_unfold_more : GoogleMaterial.Icon.gmd_unfold_less).colorRes(R.color.md_grey_600).sizeDp(24).paddingDp(mMoreVisible ? 3 : 4));
            mMoreVisible = !mMoreVisible;
        }
    });
    // Set up contacts auto-complete
    List<Person> contacts = new ArrayList<Person>();
    try {
        for (Contact contact : I2PBote.getInstance().getAddressBook().getAll()) {
            contacts.add(new Person(contact.getName(), contact.getBase64Dest(), BoteHelper.decodePicture(contact.getPictureBase64())));
        }
    } catch (PasswordException e) {
        // TODO handle
        e.printStackTrace();
    }
    mAdapter = new FilteredArrayAdapter<Person>(getActivity(), android.R.layout.simple_list_item_1, contacts) {

        @Override
        protected boolean keepObject(Person obj, String mask) {
            mask = mask.toLowerCase(Locale.US);
            return obj.getName().toLowerCase(Locale.US).startsWith(mask) || obj.getAddress().toLowerCase(Locale.US).startsWith(mask);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            if (convertView == null)
                v = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listitem_contact, parent, false);
            else
                v = convertView;
            setViewContent(v, position);
            return v;
        }

        private void setViewContent(View v, int position) {
            Person person = getItem(position);
            ((TextView) v.findViewById(R.id.contact_name)).setText(person.getName());
            ImageView picView = (ImageView) v.findViewById(R.id.contact_picture);
            Bitmap picture = person.getPicture();
            if (picture == null) {
                ViewGroup.LayoutParams lp = picView.getLayoutParams();
                picture = BoteHelper.getIdenticonForAddress(person.getAddress(), lp.width, lp.height);
            }
            picView.setImageBitmap(picture);
        }
    };
    mTo.setAdapter(mAdapter);
    mCc.setAdapter(mAdapter);
    mBcc.setAdapter(mAdapter);
    for (Person recipient : toRecipients) {
        mTo.addObject(recipient);
    }
    for (Person recipient : ccRecipients) {
        mCc.addObject(recipient);
    }
    if (origSubject != null) {
        String subjectPrefix;
        if (quoteMsgType == QuoteMsgType.FORWARD) {
            subjectPrefix = getResources().getString(hide ? R.string.subject_prefix_fwd_hide : R.string.subject_prefix_fwd);
        } else {
            subjectPrefix = getResources().getString(hide ? R.string.response_prefix_re_hide : R.string.response_prefix_re);
        }
        if (!origSubject.startsWith(subjectPrefix))
            origSubject = subjectPrefix + " " + origSubject;
        mSubject.setText(origSubject);
    }
    if (origContent != null) {
        StringBuilder quotation = new StringBuilder();
        quotation.append("\n\n");
        quotation.append(getResources().getString(hide ? R.string.response_quote_wrote_hide : R.string.response_quote_wrote, origFrom));
        String[] lines = origContent.split("\r?\n|\r");
        for (String line : lines) quotation = quotation.append("\n> ").append(line);
        mContent.setText(quotation);
    }
    if (savedInstanceState == null) {
        mTo.setPrefix(getResources().getString(R.string.email_to) + " ");
        mCc.setPrefix(getResources().getString(R.string.email_cc) + " ");
        mBcc.setPrefix(getResources().getString(R.string.email_bcc) + " ");
    }
    TextWatcher dirtyWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mDirty = true;
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    mSubject.addTextChangedListener(dirtyWatcher);
    mContent.addTextChangedListener(dirtyWatcher);
}
Also used : Email(i2p.bote.email.Email) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) PasswordException(i2p.bote.fileencryption.PasswordException) Bitmap(android.graphics.Bitmap) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) ImageView(android.widget.ImageView) IconicsDrawable(com.mikepenz.iconics.IconicsDrawable) MessagingException(javax.mail.MessagingException) ViewGroup(android.view.ViewGroup) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ContactsCompletionView(i2p.bote.android.widget.ContactsCompletionView) SuppressLint(android.annotation.SuppressLint) Contact(i2p.bote.packet.dht.Contact) Person(i2p.bote.android.util.Person)

Example 3 with Person

use of i2p.bote.android.util.Person in project i2p.i2p-bote by i2p.

the class ContactsCompletionView method getViewForObject.

@Override
protected View getViewForObject(Object object) {
    Person person = (Person) object;
    LayoutInflater l = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    LinearLayout view = (LinearLayout) l.inflate(R.layout.contact_token, (ViewGroup) ContactsCompletionView.this.getParent(), false);
    ((TextView) view.findViewById(R.id.contact_name)).setText(person.getName());
    ImageView picView = (ImageView) view.findViewById(R.id.contact_picture);
    Bitmap picture = person.getPicture();
    if (picture == null) {
        ViewGroup.LayoutParams lp = picView.getLayoutParams();
        picture = BoteHelper.getIdenticonForAddress(person.getAddress(), lp.width, lp.height);
    }
    picView.setImageBitmap(picture);
    return view;
}
Also used : Bitmap(android.graphics.Bitmap) ViewGroup(android.view.ViewGroup) LayoutInflater(android.view.LayoutInflater) TokenCompleteTextView(com.tokenautocomplete.TokenCompleteTextView) TextView(android.widget.TextView) ImageView(android.widget.ImageView) Person(i2p.bote.android.util.Person) LinearLayout(android.widget.LinearLayout)

Example 4 with Person

use of i2p.bote.android.util.Person in project i2p.i2p-bote by i2p.

the class NewEmailFragment method extractPerson.

private Person extractPerson(String recipient) {
    if (recipient.equals("Anonymous"))
        return null;
    String recipientName = BoteHelper.extractName(recipient);
    try {
        recipientName = BoteHelper.getName(recipient);
    } catch (PasswordException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }
    String recipientAddr = BoteHelper.extractEmailDestination(recipient);
    if (recipientAddr == null) {
        // Assume external address
        recipientAddr = recipient;
        if (recipientName.isEmpty())
            recipientName = recipientAddr;
        return new Person(recipientName, recipientAddr, null, true);
    } else {
        if (// Dest with no name
        recipientName.isEmpty())
            recipientName = recipientAddr.substring(0, 5);
        Bitmap recipientPic = null;
        try {
            recipientPic = BoteHelper.getPictureForDestination(recipientAddr);
        } catch (PasswordException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        return new Person(recipientName, recipientAddr, recipientPic);
    }
}
Also used : PasswordException(i2p.bote.fileencryption.PasswordException) Bitmap(android.graphics.Bitmap) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) Person(i2p.bote.android.util.Person)

Example 5 with Person

use of i2p.bote.android.util.Person in project i2p.i2p-bote by i2p.

the class ContactsCompletionView method defaultObject.

@Override
protected Object defaultObject(String completionText) {
    // Stupid simple example of guessing if we have an email or not
    int index = completionText.indexOf('@');
    if (index == -1) {
        try {
            // Check if it is a known Destination
            Contact c = BoteHelper.getContact(completionText);
            if (c != null)
                return new Person(c.getName(), c.getBase64Dest(), BoteHelper.decodePicture(c.getPictureBase64()));
            // Check if it is a name
            SortedSet<Contact> contacts = I2PBote.getInstance().getAddressBook().getAll();
            for (Contact contact : contacts) {
                if (contact.getName().startsWith(completionText))
                    return new Person(contact.getName(), contact.getBase64Dest(), BoteHelper.decodePicture(contact.getPictureBase64()));
            }
            // Try as a new Destination
            try {
                new EmailDestination(completionText);
                return new Person(completionText.substring(0, 5), completionText, null);
            } catch (GeneralSecurityException e) {
                // Not a valid Destination
                // Assume the user meant an external address
                completionText = completionText.replace(" ", "") + "@example.com";
                return new Person(completionText, completionText, null, true);
            }
        } catch (PasswordException e) {
            // TODO handle
            completionText = completionText.replace(" ", "") + "@example.com";
            return new Person(completionText, completionText, null, true);
        }
    } else {
        return new Person(completionText, completionText, null, true);
    }
}
Also used : PasswordException(i2p.bote.fileencryption.PasswordException) EmailDestination(i2p.bote.email.EmailDestination) GeneralSecurityException(java.security.GeneralSecurityException) Person(i2p.bote.android.util.Person) Contact(i2p.bote.packet.dht.Contact)

Aggregations

Person (i2p.bote.android.util.Person)5 PasswordException (i2p.bote.fileencryption.PasswordException)4 GeneralSecurityException (java.security.GeneralSecurityException)4 Bitmap (android.graphics.Bitmap)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3 IOException (java.io.IOException)3 SuppressLint (android.annotation.SuppressLint)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 ContactsCompletionView (i2p.bote.android.widget.ContactsCompletionView)2 Email (i2p.bote.email.Email)2 Contact (i2p.bote.packet.dht.Contact)2 ArrayList (java.util.ArrayList)2 Address (javax.mail.Address)2 MessagingException (javax.mail.MessagingException)2 InternetAddress (javax.mail.internet.InternetAddress)2 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 LayoutInflater (android.view.LayoutInflater)1