2011-01-10 12:47:28 -05:00
|
|
|
package com.fsck.k9.view;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.style.StyleSpan;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import com.fsck.k9.FontSizes;
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.R;
|
|
|
|
import com.fsck.k9.helper.Contacts;
|
|
|
|
import com.fsck.k9.Account;
|
|
|
|
import com.fsck.k9.helper.DateFormatter;
|
|
|
|
import com.fsck.k9.mail.Address;
|
|
|
|
import com.fsck.k9.mail.Flag;
|
|
|
|
import com.fsck.k9.mail.Message;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
|
|
|
import com.fsck.k9.mail.internet.MimeUtility;
|
|
|
|
import com.fsck.k9.mail.store.LocalStore;
|
2011-11-14 23:32:46 -05:00
|
|
|
import java.util.LinkedHashSet;
|
2011-01-10 12:47:28 -05:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.text.DateFormat;
|
2011-01-18 18:54:49 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public class MessageHeader extends LinearLayout {
|
2011-01-10 12:47:28 -05:00
|
|
|
private Context mContext;
|
|
|
|
private TextView mFromView;
|
|
|
|
private TextView mDateView;
|
|
|
|
private TextView mTimeView;
|
|
|
|
private TextView mToView;
|
|
|
|
private TextView mCcView;
|
|
|
|
private TextView mSubjectView;
|
|
|
|
private DateFormat mDateFormat;
|
|
|
|
private DateFormat mTimeFormat;
|
|
|
|
|
|
|
|
private View mChip;
|
|
|
|
private CheckBox mFlagged;
|
|
|
|
private int defaultSubjectColor;
|
|
|
|
private LinearLayout mToContainerView;
|
|
|
|
private LinearLayout mCcContainerView;
|
|
|
|
private TextView mAdditionalHeadersView;
|
|
|
|
private View mAttachmentIcon;
|
2011-01-17 12:25:00 -05:00
|
|
|
private View mAnsweredIcon;
|
2011-01-10 12:47:28 -05:00
|
|
|
private Message mMessage;
|
|
|
|
private Account mAccount;
|
|
|
|
private FontSizes mFontSizes = K9.getFontSizes();
|
|
|
|
private Contacts mContacts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pair class is only available since API Level 5, so we need
|
|
|
|
* this helper class unfortunately
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private static class HeaderEntry {
|
2011-01-10 12:47:28 -05:00
|
|
|
public String label;
|
|
|
|
public String value;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public HeaderEntry(String label, String value) {
|
2011-01-10 12:47:28 -05:00
|
|
|
this.label = label;
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public MessageHeader(Context context, AttributeSet attrs) {
|
2011-01-10 12:47:28 -05:00
|
|
|
super(context, attrs);
|
|
|
|
mContext = context;
|
|
|
|
mDateFormat = DateFormatter.getDateFormat(mContext);
|
|
|
|
mTimeFormat = android.text.format.DateFormat.getTimeFormat(mContext); // 12/24 date format
|
|
|
|
mContacts = Contacts.getInstance(mContext);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private void initializeLayout() {
|
2011-01-10 12:47:28 -05:00
|
|
|
mAttachmentIcon = findViewById(R.id.attachment);
|
2011-01-17 12:25:00 -05:00
|
|
|
mAnsweredIcon = findViewById(R.id.answered);
|
2011-01-10 12:47:28 -05:00
|
|
|
mFromView = (TextView) findViewById(R.id.from);
|
|
|
|
mToView = (TextView) findViewById(R.id.to);
|
|
|
|
mCcView = (TextView) findViewById(R.id.cc);
|
|
|
|
mToContainerView = (LinearLayout) findViewById(R.id.to_container);
|
|
|
|
mCcContainerView = (LinearLayout) findViewById(R.id.cc_container);
|
|
|
|
mSubjectView = (TextView) findViewById(R.id.subject);
|
|
|
|
mAdditionalHeadersView = (TextView) findViewById(R.id.additional_headers_view);
|
|
|
|
mChip = findViewById(R.id.chip);
|
|
|
|
mDateView = (TextView) findViewById(R.id.date);
|
|
|
|
mTimeView = (TextView) findViewById(R.id.time);
|
|
|
|
mFlagged = (CheckBox) findViewById(R.id.flagged);
|
|
|
|
|
|
|
|
defaultSubjectColor = mSubjectView.getCurrentTextColor();
|
|
|
|
mSubjectView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewSubject());
|
|
|
|
mTimeView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewTime());
|
|
|
|
mDateView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewDate());
|
|
|
|
mAdditionalHeadersView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewAdditionalHeaders());
|
|
|
|
mAdditionalHeadersView.setVisibility(View.GONE);
|
|
|
|
mAttachmentIcon.setVisibility(View.GONE);
|
2011-01-17 12:25:00 -05:00
|
|
|
mAnsweredIcon.setVisibility(View.GONE);
|
2011-01-10 12:47:28 -05:00
|
|
|
mFromView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewSender());
|
|
|
|
mToView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewTo());
|
|
|
|
mCcView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewCC());
|
|
|
|
((TextView) findViewById(R.id.to_label)).setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewTo());
|
|
|
|
((TextView) findViewById(R.id.cc_label)).setTextSize(TypedValue.COMPLEX_UNIT_DIP, mFontSizes.getMessageViewCC());
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
setOnClickListener(new OnClickListener() {
|
2011-01-10 12:47:28 -05:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onClick(View v) {
|
2011-01-10 12:47:28 -05:00
|
|
|
onShowAdditionalHeaders();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
mFromView.setOnClickListener(new OnClickListener() {
|
2011-01-10 12:47:28 -05:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onClick(View v) {
|
|
|
|
if (mMessage != null) {
|
|
|
|
try {
|
2011-01-10 12:47:28 -05:00
|
|
|
final Address senderEmail = mMessage.getFrom()[0];
|
|
|
|
mContacts.createContact(senderEmail);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (Exception e) {
|
2011-01-10 12:47:28 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Couldn't create contact", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setOnFlagListener(OnClickListener listener) {
|
2011-01-10 12:47:28 -05:00
|
|
|
mFlagged.setOnClickListener(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean additionalHeadersVisible() {
|
|
|
|
if (mAdditionalHeadersView != null && mAdditionalHeadersView.getVisibility() == View.VISIBLE) {
|
2011-01-10 12:47:28 -05:00
|
|
|
return true;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2011-01-10 12:47:28 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the text field for the additional headers display if they are
|
|
|
|
* not shown, to save UI resources.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private void hideAdditionalHeaders() {
|
2011-01-10 12:47:28 -05:00
|
|
|
mAdditionalHeadersView.setVisibility(View.GONE);
|
|
|
|
mAdditionalHeadersView.setText("");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up and then show the additional headers view. Called by
|
|
|
|
* {@link #onShowAdditionalHeaders()}
|
|
|
|
* (when switching between messages).
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private void showAdditionalHeaders() {
|
2011-01-10 12:47:28 -05:00
|
|
|
Integer messageToShow = null;
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2011-01-10 12:47:28 -05:00
|
|
|
// Retrieve additional headers
|
|
|
|
boolean allHeadersDownloaded = mMessage.isSet(Flag.X_GOT_ALL_HEADERS);
|
|
|
|
List<HeaderEntry> additionalHeaders = getAdditionalHeaders(mMessage);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (!additionalHeaders.isEmpty()) {
|
2011-01-10 12:47:28 -05:00
|
|
|
// Show the additional headers that we have got.
|
|
|
|
populateAdditionalHeadersView(additionalHeaders);
|
|
|
|
mAdditionalHeadersView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (!allHeadersDownloaded) {
|
2011-01-10 12:47:28 -05:00
|
|
|
/*
|
|
|
|
* Tell the user about the "save all headers" setting
|
|
|
|
*
|
|
|
|
* NOTE: This is only a temporary solution... in fact,
|
|
|
|
* the system should download headers on-demand when they
|
|
|
|
* have not been saved in their entirety initially.
|
|
|
|
*/
|
|
|
|
messageToShow = R.string.message_additional_headers_not_downloaded;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (additionalHeaders.isEmpty()) {
|
2011-01-10 12:47:28 -05:00
|
|
|
// All headers have been downloaded, but there are no additional headers.
|
|
|
|
messageToShow = R.string.message_no_additional_headers_available;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (MessagingException e) {
|
2011-01-10 12:47:28 -05:00
|
|
|
messageToShow = R.string.message_additional_headers_retrieval_failed;
|
|
|
|
}
|
|
|
|
// Show a message to the user, if any
|
2011-02-06 17:09:48 -05:00
|
|
|
if (messageToShow != null) {
|
2011-01-10 12:47:28 -05:00
|
|
|
Toast toast = Toast.makeText(mContext, messageToShow, Toast.LENGTH_LONG);
|
|
|
|
toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
|
|
|
|
toast.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void populate(final Message message, final Account account) throws MessagingException {
|
2011-01-10 12:47:28 -05:00
|
|
|
final Contacts contacts = K9.showContactName() ? mContacts : null;
|
|
|
|
final CharSequence from = Address.toFriendly(message.getFrom(), contacts);
|
|
|
|
final String date = mDateFormat.format(message.getSentDate());
|
|
|
|
final String time = mTimeFormat.format(message.getSentDate());
|
|
|
|
final CharSequence to = Address.toFriendly(message.getRecipients(Message.RecipientType.TO), contacts);
|
|
|
|
final CharSequence cc = Address.toFriendly(message.getRecipients(Message.RecipientType.CC), contacts);
|
|
|
|
|
|
|
|
mMessage = message;
|
|
|
|
mAccount = account;
|
|
|
|
|
|
|
|
initializeLayout();
|
|
|
|
String subject = message.getSubject();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (subject == null || subject.equals("")) {
|
2011-01-10 12:47:28 -05:00
|
|
|
mSubjectView.setText(mContext.getText(R.string.general_no_subject));
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2011-01-10 12:47:28 -05:00
|
|
|
mSubjectView.setText(subject);
|
|
|
|
}
|
|
|
|
mSubjectView.setTextColor(0xff000000 | defaultSubjectColor);
|
|
|
|
|
|
|
|
mFromView.setText(from);
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (date != null) {
|
2011-01-10 12:47:28 -05:00
|
|
|
mDateView.setText(date);
|
|
|
|
mDateView.setVisibility(View.VISIBLE);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2011-01-10 12:47:28 -05:00
|
|
|
mDateView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
mTimeView.setText(time);
|
|
|
|
mToContainerView.setVisibility((to != null && to.length() > 0) ? View.VISIBLE : View.GONE);
|
|
|
|
mToView.setText(to);
|
|
|
|
mCcContainerView.setVisibility((cc != null && cc.length() > 0) ? View.VISIBLE : View.GONE);
|
|
|
|
mCcView.setText(cc);
|
|
|
|
mAttachmentIcon.setVisibility(((LocalStore.LocalMessage) message).hasAttachments() ? View.VISIBLE : View.GONE);
|
2011-01-17 12:25:00 -05:00
|
|
|
mAnsweredIcon.setVisibility(message.isSet(Flag.ANSWERED) ? View.VISIBLE : View.GONE);
|
2011-01-10 12:47:28 -05:00
|
|
|
mFlagged.setChecked(message.isSet(Flag.FLAGGED));
|
|
|
|
mChip.setBackgroundDrawable(mAccount.generateColorChip().drawable());
|
|
|
|
mChip.getBackground().setAlpha(!message.isSet(Flag.SEEN) ? 255 : 127);
|
|
|
|
setVisibility(View.VISIBLE);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mAdditionalHeadersView.getVisibility() == View.VISIBLE) {
|
2011-01-10 12:47:28 -05:00
|
|
|
showAdditionalHeaders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onShowAdditionalHeaders() {
|
2011-01-10 12:47:28 -05:00
|
|
|
int currentVisibility = mAdditionalHeadersView.getVisibility();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (currentVisibility == View.VISIBLE) {
|
2011-01-10 12:47:28 -05:00
|
|
|
hideAdditionalHeaders();
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2011-01-10 12:47:28 -05:00
|
|
|
showAdditionalHeaders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<HeaderEntry> getAdditionalHeaders(final Message message)
|
2011-02-06 17:09:48 -05:00
|
|
|
throws MessagingException {
|
2011-01-10 12:47:28 -05:00
|
|
|
List<HeaderEntry> additionalHeaders = new LinkedList<HeaderEntry>();
|
|
|
|
/*
|
|
|
|
* Remove "Subject" header as it is already shown in the standard
|
|
|
|
* message view header. But do show "From", "To", and "Cc" again.
|
|
|
|
* This time including the email addresses. See issue 1805.
|
|
|
|
*/
|
2011-11-14 23:32:46 -05:00
|
|
|
Set<String> headerNames = new LinkedHashSet<String>(message.getHeaderNames());
|
2011-01-10 12:47:28 -05:00
|
|
|
headerNames.remove("Subject");
|
2011-02-06 17:09:48 -05:00
|
|
|
for (String headerName : headerNames) {
|
2011-01-10 12:47:28 -05:00
|
|
|
String[] headerValues = message.getHeader(headerName);
|
2011-02-06 17:09:48 -05:00
|
|
|
for (String headerValue : headerValues) {
|
2011-01-10 12:47:28 -05:00
|
|
|
additionalHeaders.add(new HeaderEntry(headerName, headerValue));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return additionalHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the additional headers text view with the supplied header data.
|
|
|
|
*
|
|
|
|
* @param additionalHeaders List of header entries. Each entry consists of a header
|
|
|
|
* name and a header value. Header names may appear multiple
|
|
|
|
* times.
|
|
|
|
* <p/>
|
|
|
|
* This method is always called from within the UI thread by
|
|
|
|
* {@link #showAdditionalHeaders()}.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private void populateAdditionalHeadersView(final List<HeaderEntry> additionalHeaders) {
|
2011-01-10 12:47:28 -05:00
|
|
|
SpannableStringBuilder sb = new SpannableStringBuilder();
|
|
|
|
boolean first = true;
|
2011-02-06 17:09:48 -05:00
|
|
|
for (HeaderEntry additionalHeader : additionalHeaders) {
|
|
|
|
if (!first) {
|
2011-01-10 12:47:28 -05:00
|
|
|
sb.append("\n");
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2011-01-10 12:47:28 -05:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
|
|
|
|
SpannableString label = new SpannableString(additionalHeader.label + ": ");
|
|
|
|
label.setSpan(boldSpan, 0, label.length(), 0);
|
|
|
|
sb.append(label);
|
|
|
|
sb.append(MimeUtility.unfoldAndDecode(additionalHeader.value));
|
|
|
|
}
|
|
|
|
mAdditionalHeadersView.setText(sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|