Show contact picture in email list and view (if available)

This commit is contained in:
str4d
2014-03-06 23:51:16 +00:00
parent 48d7d22638
commit 7143f40f6d
5 changed files with 95 additions and 17 deletions

View File

@@ -8,7 +8,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" > android:orientation="vertical" >
<LinearLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@android:color/darker_gray" android:background="@android:color/darker_gray"
@@ -22,11 +22,21 @@
android:text="Subject" android:text="Subject"
android:textAppearance="?android:attr/textAppearanceMedium" /> android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/contact_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/email_subject"
android:layout_below="@+id/email_subject"
android:src="@drawable/ic_launcher" />
<TableLayout <TableLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginLeft="10dp" android:layout_below="@+id/email_subject"
android:layout_marginBottom="5dp" > android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/contact_picture" >
<TableRow <TableRow
android:layout_width="wrap_content" android:layout_width="wrap_content"
@@ -60,7 +70,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" > android:orientation="vertical" >
</LinearLayout> </LinearLayout>
</TableRow> </TableRow>
@@ -96,10 +105,9 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Datetime" /> android:text="Datetime" />
</TableRow> </TableRow>
</TableLayout> </TableLayout>
</LinearLayout> </RelativeLayout>
<TextView <TextView
android:id="@+id/email_content" android:id="@+id/email_content"

View File

@@ -4,20 +4,27 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="5dp" > android:padding="5dp" >
<ImageView
android:id="@+id/contact_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView <TextView
android:id="@+id/email_subject" android:id="@+id/email_subject"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignTop="@+id/contact_picture"
android:layout_alignParentTop="true" android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" android:layout_toRightOf="@+id/contact_picture"
android:text="Email subject" /> android:text="Email subject"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView <TextView
android:id="@+id/email_from" android:id="@+id/email_from"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignLeft="@+id/email_subject"
android:layout_below="@+id/email_subject" android:layout_below="@+id/email_subject"
android:text="From" /> android:text="From" />
@@ -25,10 +32,10 @@
android:id="@+id/email_content" android:id="@+id/email_content"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignLeft="@+id/email_from"
android:layout_below="@+id/email_from" android:layout_below="@+id/email_from"
android:maxLines="1"
android:ellipsize="end" android:ellipsize="end"
android:maxLines="1"
android:text="Content" /> android:text="Content" />
<TextView <TextView

View File

@@ -11,11 +11,13 @@ import i2p.bote.email.Email;
import i2p.bote.fileencryption.PasswordException; import i2p.bote.fileencryption.PasswordException;
import i2p.bote.util.BoteHelper; import i2p.bote.util.BoteHelper;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
public class EmailListAdapter extends ArrayAdapter<Email> { public class EmailListAdapter extends ArrayAdapter<Email> {
@@ -40,15 +42,21 @@ public class EmailListAdapter extends ArrayAdapter<Email> {
View v = mInflater.inflate(R.layout.listitem_email, parent, false); View v = mInflater.inflate(R.layout.listitem_email, parent, false);
Email email = getItem(position); Email email = getItem(position);
ImageView picture = (ImageView) v.findViewById(R.id.contact_picture);
TextView subject = (TextView) v.findViewById(R.id.email_subject); TextView subject = (TextView) v.findViewById(R.id.email_subject);
TextView from = (TextView) v.findViewById(R.id.email_from); TextView from = (TextView) v.findViewById(R.id.email_from);
TextView content = (TextView) v.findViewById(R.id.email_content); TextView content = (TextView) v.findViewById(R.id.email_content);
TextView sent = (TextView) v.findViewById(R.id.email_sent); TextView sent = (TextView) v.findViewById(R.id.email_sent);
try { try {
String fromAddress = email.getOneFromAddress();
Bitmap pic = BoteHelper.getPictureForAddress(fromAddress);
if (pic != null)
picture.setImageBitmap(pic);
subject.setText(email.getSubject()); subject.setText(email.getSubject());
from.setText(BoteHelper.getNameAndShortDestination( from.setText(BoteHelper.getNameAndShortDestination(fromAddress));
email.getOneFromAddress()));
if (email.getSentDate() != null) if (email.getSentDate() != null)
sent.setText(DateFormat.getInstance().format( sent.setText(DateFormat.getInstance().format(
email.getSentDate())); email.getSentDate()));

View File

@@ -10,11 +10,13 @@ import javax.mail.MessagingException;
import i2p.bote.email.Email; import i2p.bote.email.Email;
import i2p.bote.fileencryption.PasswordException; import i2p.bote.fileencryption.PasswordException;
import i2p.bote.util.BoteHelper; import i2p.bote.util.BoteHelper;
import android.graphics.Bitmap;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
@@ -64,6 +66,7 @@ public class ViewEmailFragment extends Fragment {
private void displayEmail(Email email, View v) { private void displayEmail(Email email, View v) {
TextView subject = (TextView) v.findViewById(R.id.email_subject); TextView subject = (TextView) v.findViewById(R.id.email_subject);
ImageView picture = (ImageView) v.findViewById(R.id.contact_picture);
TextView sender = (TextView) v.findViewById(R.id.email_sender); TextView sender = (TextView) v.findViewById(R.id.email_sender);
LinearLayout recipients = (LinearLayout) v.findViewById(R.id.email_recipients); LinearLayout recipients = (LinearLayout) v.findViewById(R.id.email_recipients);
TextView sent = (TextView) v.findViewById(R.id.email_sent); TextView sent = (TextView) v.findViewById(R.id.email_sent);
@@ -71,10 +74,15 @@ public class ViewEmailFragment extends Fragment {
TextView content = (TextView) v.findViewById(R.id.email_content); TextView content = (TextView) v.findViewById(R.id.email_content);
try { try {
String fromAddress = email.getOneFromAddress();
subject.setText(email.getSubject()); subject.setText(email.getSubject());
sender.setText(BoteHelper.getDisplayAddress( Bitmap pic = BoteHelper.getPictureForAddress(fromAddress);
email.getOneFromAddress())); if (pic != null)
picture.setImageBitmap(pic);
sender.setText(BoteHelper.getDisplayAddress(fromAddress));
for (Address recipient : email.getToAddresses()) { for (Address recipient : email.getToAddresses()) {
TextView tv = new TextView(getActivity()); TextView tv = new TextView(getActivity());

View File

@@ -6,10 +6,17 @@ import java.security.GeneralSecurityException;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import i2p.bote.I2PBote;
import i2p.bote.R; import i2p.bote.R;
import i2p.bote.email.EmailDestination;
import i2p.bote.email.EmailIdentity;
import i2p.bote.fileencryption.PasswordException; import i2p.bote.fileencryption.PasswordException;
import i2p.bote.folder.EmailFolder; import i2p.bote.folder.EmailFolder;
import i2p.bote.packet.dht.Contact;
public class BoteHelper extends GeneralHelper { public class BoteHelper extends GeneralHelper {
/** /**
@@ -63,4 +70,44 @@ public class BoteHelper extends GeneralHelper {
: (name.isEmpty() ? emailDest.substring(0, 10) : (name.isEmpty() ? emailDest.substring(0, 10)
: name + " <" + emailDest.substring(0, 10) + "...>")); : name + " <" + emailDest.substring(0, 10) + "...>"));
} }
/**
* Get a Bitmap containing the picture for the contact or identity
* corresponding to the given address.
* @param address
* @return a Bitmap, or null if no picture was found.
* @throws PasswordException
* @throws IOException
* @throws GeneralSecurityException
*/
public static Bitmap getPictureForAddress(String address) throws PasswordException, IOException, GeneralSecurityException {
String fullAdr = getNameAndDestination(address);
if (!address.equals(fullAdr)) {
// Address was found; try address book first
String base64dest = EmailDestination.extractBase64Dest(fullAdr);
Contact c = getContact(base64dest);
if (c != null) {
// Address is in address book
String pic = c.getPictureBase64();
if (pic != null) {
byte[] decodedPic = Base64.decode(pic, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedPic, 0, decodedPic.length);
}
} else {
// Address is an identity
EmailIdentity i = getIdentity(base64dest);
if (i != null) {
String pic = i.getPictureBase64();
if (pic != null) {
byte[] decodedPic = Base64.decode(pic, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedPic, 0, decodedPic.length);
}
}
}
}
// Address not found anywhere, or found and has no picture
return null;
}
} }