1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-12 04:25:08 -05:00
k-9/src/com/fsck/k9/helper/MessageHelper.java

108 lines
3.7 KiB
Java
Raw Normal View History

package com.fsck.k9.helper;
import java.text.DateFormat;
import java.util.Date;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.util.Log;
import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.R;
import com.fsck.k9.activity.FolderInfoHolder;
import com.fsck.k9.activity.MessageInfoHolder;
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.Message.RecipientType;
import com.fsck.k9.mail.store.LocalStore.LocalMessage;
import com.fsck.k9.helper.DateFormatter;
public class MessageHelper {
private static MessageHelper sInstance;
public synchronized static MessageHelper getInstance(final Context context) {
if (sInstance == null) {
sInstance = new MessageHelper(context);
}
return sInstance;
}
private Context mContext;
private DateFormat mTodayDateFormat;
private DateFormat mDateFormat;
private MessageHelper(final Context context) {
mContext = context;
mDateFormat = DateFormatter.getDateFormat(mContext);
mTodayDateFormat = android.text.format.DateFormat.getTimeFormat(mContext);
}
public void populate(final MessageInfoHolder target, final Message m,
final FolderInfoHolder folder, final Account account) {
final Contacts contactHelper = K9.showContactName() ? Contacts.getInstance(mContext) : null;
try {
LocalMessage message = (LocalMessage) m;
2010-11-26 23:02:56 -05:00
target.message = message;
2012-03-11 18:48:56 -04:00
target.compareArrival = message.getInternalDate();
target.compareDate = message.getSentDate();
if (target.compareDate == null) {
target.compareDate = message.getInternalDate();
}
target.folder = folder;
target.read = message.isSet(Flag.SEEN);
target.answered = message.isSet(Flag.ANSWERED);
2012-08-20 22:09:34 -04:00
target.forwarded = message.isSet(Flag.FORWARDED);
target.flagged = message.isSet(Flag.FLAGGED);
Address[] addrs = message.getFrom();
if (addrs.length > 0 && account.isAnIdentity(addrs[0])) {
CharSequence to = Address.toFriendly(message .getRecipients(RecipientType.TO), contactHelper);
target.compareCounterparty = to.toString();
2010-10-29 18:35:11 -04:00
target.sender = new SpannableStringBuilder(mContext.getString(R.string.message_to_label)).append(to);
} else {
target.sender = Address.toFriendly(addrs, contactHelper);
target.compareCounterparty = target.sender.toString();
}
if (addrs.length > 0) {
target.senderAddress = addrs[0].getAddress();
} else {
// a reasonable fallback "whomever we were corresponding with
target.senderAddress = target.compareCounterparty;
}
target.uid = message.getUid();
target.account = account.getUuid();
target.uri = "email://messages/" + account.getAccountNumber() + "/" + m.getFolder().getName() + "/" + m.getUid();
} catch (MessagingException me) {
Log.w(K9.LOG_TAG, "Unable to load message info", me);
}
}
public String formatDate(Date date) {
if (Utility.isDateToday(date)) {
2010-10-21 16:49:20 -04:00
return mTodayDateFormat.format(date);
} else {
2010-10-21 16:49:20 -04:00
return mDateFormat.format(date);
}
}
2011-04-12 08:16:22 -04:00
public void refresh() {
mDateFormat = DateFormatter.getDateFormat(mContext);
2011-04-12 08:16:22 -04:00
mTodayDateFormat = android.text.format.DateFormat.getTimeFormat(mContext);
}
}