2010-10-03 06:56:16 -04:00
|
|
|
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;
|
2011-01-10 12:47:16 -05:00
|
|
|
import com.fsck.k9.helper.DateFormatter;
|
2010-10-03 06:56:16 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public class MessageHelper {
|
2010-10-03 06:56:16 -04:00
|
|
|
|
|
|
|
private static MessageHelper sInstance;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public synchronized static MessageHelper getInstance(final Context context) {
|
|
|
|
if (sInstance == null) {
|
2010-10-03 06:56:16 -04:00
|
|
|
sInstance = new MessageHelper(context);
|
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
private DateFormat mTodayDateFormat;
|
|
|
|
|
|
|
|
private DateFormat mDateFormat;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private MessageHelper(final Context context) {
|
2010-10-03 06:56:16 -04:00
|
|
|
mContext = context;
|
|
|
|
mDateFormat = DateFormatter.getDateFormat(mContext);
|
|
|
|
mTodayDateFormat = android.text.format.DateFormat.getTimeFormat(mContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void populate(final MessageInfoHolder target, final Message m,
|
2011-02-06 17:09:48 -05:00
|
|
|
final FolderInfoHolder folder, final Account account) {
|
2010-10-08 20:24:43 -04:00
|
|
|
final Contacts contactHelper = K9.showContactName() ? Contacts.getInstance(mContext) : null;
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2010-10-03 06:56:16 -04:00
|
|
|
LocalMessage message = (LocalMessage) m;
|
2010-11-26 23:02:56 -05:00
|
|
|
target.message = message;
|
2010-10-03 06:56:16 -04:00
|
|
|
target.compareDate = message.getSentDate();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (target.compareDate == null) {
|
2010-10-03 06:56:16 -04:00
|
|
|
target.compareDate = message.getInternalDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
target.folder = folder;
|
|
|
|
|
|
|
|
target.read = message.isSet(Flag.SEEN);
|
|
|
|
target.answered = message.isSet(Flag.ANSWERED);
|
|
|
|
target.flagged = message.isSet(Flag.FLAGGED);
|
|
|
|
target.downloaded = message.isSet(Flag.X_DOWNLOADED_FULL);
|
|
|
|
target.partially_downloaded = message.isSet(Flag.X_DOWNLOADED_PARTIAL);
|
|
|
|
|
|
|
|
Address[] addrs = message.getFrom();
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (addrs.length > 0 && account.isAnIdentity(addrs[0])) {
|
2010-10-03 06:56:16 -04:00
|
|
|
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);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-10-03 06:56:16 -04:00
|
|
|
target.sender = Address.toFriendly(addrs, contactHelper);
|
|
|
|
target.compareCounterparty = target.sender.toString();
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (addrs.length > 0) {
|
2010-10-03 06:56:16 -04:00
|
|
|
target.senderAddress = addrs[0].getAddress();
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-10-03 06:56:16 -04:00
|
|
|
// a reasonable fallback "whomever we were corresponding with
|
|
|
|
target.senderAddress = target.compareCounterparty;
|
|
|
|
}
|
|
|
|
|
2010-10-21 16:48:45 -04:00
|
|
|
|
|
|
|
|
2010-10-03 06:56:16 -04:00
|
|
|
|
|
|
|
target.uid = message.getUid();
|
|
|
|
|
|
|
|
target.account = account.getDescription();
|
|
|
|
target.uri = "email://messages/" + account.getAccountNumber() + "/" + m.getFolder().getName() + "/" + m.getUid();
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (MessagingException me) {
|
2010-10-03 06:56:16 -04:00
|
|
|
Log.w(K9.LOG_TAG, "Unable to load message info", me);
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
public String formatDate(Date date) {
|
|
|
|
if (Utility.isDateToday(date)) {
|
2010-10-21 16:49:20 -04:00
|
|
|
return mTodayDateFormat.format(date);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-10-21 16:49:20 -04:00
|
|
|
return mDateFormat.format(date);
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 07:23:19 -04:00
|
|
|
|
2011-04-12 08:16:22 -04:00
|
|
|
public void refresh() {
|
2011-04-12 07:23:19 -04:00
|
|
|
mDateFormat = DateFormatter.getDateFormat(mContext);
|
2011-04-12 08:16:22 -04:00
|
|
|
mTodayDateFormat = android.text.format.DateFormat.getTimeFormat(mContext);
|
|
|
|
}
|
2010-10-03 06:56:16 -04:00
|
|
|
}
|