Search in sources :

Example 11 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class ContactAdapter method onBindViewHolder.

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch(holder.getItemViewType()) {
        case R.layout.listitem_empty:
            ((TextView) holder.itemView).setText(mCtx.getResources().getString(R.string.address_book_empty));
            break;
        case R.layout.listitem_contact:
            final ContactViewHolder cvh = (ContactViewHolder) holder;
            Contact contact = mContacts.get(position);
            String pic = contact.getPictureBase64();
            if (pic != null && !pic.isEmpty())
                cvh.mPicture.setImageBitmap(BoteHelper.decodePicture(pic));
            else {
                ViewGroup.LayoutParams lp = cvh.mPicture.getLayoutParams();
                cvh.mPicture.setImageBitmap(BoteHelper.getIdenticonForAddress(contact.getBase64Dest(), lp.width, lp.height));
            }
            cvh.mName.setText(contact.getName());
            cvh.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    mListener.onContactSelected(mContacts.get(cvh.getAdapterPosition()));
                }
            });
            break;
        default:
            break;
    }
}
Also used : ViewGroup(android.view.ViewGroup) TextView(android.widget.TextView) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View) Contact(i2p.bote.packet.dht.Contact)

Example 12 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class EditContactFragment method initializeContact.

private void initializeContact() {
    String newDest = getArguments().getString(NEW_DESTINATION);
    if (mDestination != null) {
        try {
            Contact contact = BoteHelper.getContact(mDestination);
            String pic = contact.getPictureBase64();
            if (pic != null && !pic.isEmpty()) {
                setPictureB64(pic);
            }
            mNameField.setText(contact.getName());
            mTextField.setText(contact.getText());
        } catch (PasswordException e) {
            // TODO Handle
            e.printStackTrace();
        }
    } else if (newDest != null) {
        mNameField.setText(getArguments().getString(NEW_NAME));
        mDestinationField.setText(newDest);
    }
}
Also used : PasswordException(i2p.bote.fileencryption.PasswordException) Contact(i2p.bote.packet.dht.Contact)

Example 13 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class AddressBook method loadFromProperties.

protected boolean loadFromProperties(Properties properties, boolean append, boolean replace) {
    if (contacts == null || !append) {
        contacts = new TreeSet<Contact>(new ContactComparator());
    }
    int index = 0;
    while (true) {
        String prefix = "contact" + index + ".";
        String name = properties.getProperty(prefix + "name");
        if (name == null)
            break;
        String destBase64 = properties.getProperty(prefix + "destination");
        if (destBase64 == null)
            continue;
        try {
            EmailDestination destination = new EmailDestination(destBase64);
            String pictureBase64 = properties.getProperty(prefix + "picture");
            String text = properties.getProperty(prefix + "text");
            Contact contact = new Contact(name, destination, pictureBase64, text);
            if (append && replace && contacts.contains(contact)) {
                contacts.remove(contact);
            }
            contacts.add(contact);
        } catch (GeneralSecurityException e) {
            log.error("Not a valid Email Destination: <" + destBase64 + ">", e);
        }
        index++;
    }
    return index > 0;
}
Also used : EmailDestination(i2p.bote.email.EmailDestination) GeneralSecurityException(java.security.GeneralSecurityException) Contact(i2p.bote.packet.dht.Contact)

Example 14 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class AddressBook method readContacts.

/**
 * Reads an <code>AddressBook</code> from the encrypted address book file.
 * Each contact is defined by one line that contains an Email Destination
 * and a name, separated by a tab character.
 * @throws PasswordException
 */
private void readContacts() throws PasswordException {
    if (!addressFile.exists()) {
        log.debug("Address file does not exist: <" + addressFile.getAbsolutePath() + ">");
        contacts = new TreeSet<Contact>(new ContactComparator());
        return;
    }
    log.debug("Reading address book from <" + addressFile.getAbsolutePath() + ">");
    BufferedReader input = null;
    try {
        InputStream encryptedStream = new EncryptedInputStream(new FileInputStream(addressFile), passwordHolder);
        input = new BufferedReader(new InputStreamReader(encryptedStream));
        // No PasswordException occurred, so parse the input stream
        Properties properties = new Properties();
        properties.load(new InputStreamReader(encryptedStream));
        loadFromProperties(properties, false, false);
    } catch (PasswordException e) {
        throw e;
    } catch (Exception e) {
        log.error("Can't read address book.", e);
    } finally {
        if (input != null)
            try {
                input.close();
            } catch (IOException e) {
                log.error("Error closing input stream.", e);
            }
    }
}
Also used : EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) PasswordException(i2p.bote.fileencryption.PasswordException) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Properties(java.util.Properties) SortedProperties(i2p.bote.util.SortedProperties) FileInputStream(java.io.FileInputStream) PasswordException(i2p.bote.fileencryption.PasswordException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Contact(i2p.bote.packet.dht.Contact)

Example 15 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class AddressDisplayFilter method getNameAndDestination.

/**
 * Looks up the name associated with a Base64-encoded Email Destination
 * in the address book and the local identities, and returns a string
 * that contains the name and the Base64-encoded destination.<br/>
 * If <code>address</code> already contains a name, it is replaced with
 * the one from the address book or identities.<br/>
 * If no name is found in the address book or the identities, or if
 * <code>address</code> does not contain a valid Email Destination,
 * <code>address</code> is returned.
 * @param address A Base64-encoded Email Destination, and optionally a name
 * @throws PasswordException
 * @throws GeneralSecurityException
 * @throws IOException
 */
public String getNameAndDestination(String address) throws PasswordException, IOException, GeneralSecurityException {
    String base64dest = EmailDestination.extractBase64Dest(address);
    if (base64dest != null) {
        // try the address book
        Contact contact = addressBook.get(base64dest);
        if (contact != null)
            return contact.getName() + " <" + contact.getBase64Dest() + ">";
        // if no address book entry, try the email identities
        EmailIdentity identity = identities.get(base64dest);
        if (identity != null)
            return identity.getPublicName() + " <" + identity.toBase64() + ">";
    }
    return address;
}
Also used : Contact(i2p.bote.packet.dht.Contact)

Aggregations

Contact (i2p.bote.packet.dht.Contact)18 GeneralSecurityException (java.security.GeneralSecurityException)7 EmailDestination (i2p.bote.email.EmailDestination)4 PasswordException (i2p.bote.fileencryption.PasswordException)4 IOException (java.io.IOException)4 File (java.io.File)3 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 AddressBook (i2p.bote.addressbook.AddressBook)2 Person (i2p.bote.android.util.Person)2 SortedProperties (i2p.bote.util.SortedProperties)2 FileInputStream (java.io.FileInputStream)2 Before (org.junit.Before)2 SuppressLint (android.annotation.SuppressLint)1 Bitmap (android.graphics.Bitmap)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1