use of com.fsck.k9.K9 in project k-9 by k9mail.
the class MessagingController method addErrorMessage.
private void addErrorMessage(Account account, String subject, String body) {
if (!K9.isDebug()) {
return;
}
if (!loopCatch.compareAndSet(false, true)) {
return;
}
try {
if (body == null || body.length() < 1) {
return;
}
Store localStore = account.getLocalStore();
LocalFolder localFolder = (LocalFolder) localStore.getFolder(account.getErrorFolderName());
MimeMessage message = new MimeMessage();
MimeMessageHelper.setBody(message, new TextBody(body));
message.setFlag(Flag.X_DOWNLOADED_FULL, true);
message.setSubject(subject);
long nowTime = System.currentTimeMillis();
Date nowDate = new Date(nowTime);
message.setInternalDate(nowDate);
message.addSentDate(nowDate, K9.hideTimeZone());
message.setFrom(new Address(account.getEmail(), "K9mail internal"));
localFolder.appendMessages(Collections.singletonList(message));
localFolder.clearMessagesOlderThan(nowTime - (15 * 60 * 1000));
} catch (Throwable it) {
Timber.e(it, "Could not save error message to %s", account.getErrorFolderName());
} finally {
loopCatch.set(false);
}
}
use of com.fsck.k9.K9 in project k-9 by k9mail.
the class MessagingController method sendPendingMessagesSynchronous.
/**
* Attempt to send any messages that are sitting in the Outbox.
*/
@VisibleForTesting
protected void sendPendingMessagesSynchronous(final Account account) {
LocalFolder localFolder = null;
Exception lastFailure = null;
boolean wasPermanentFailure = false;
try {
LocalStore localStore = account.getLocalStore();
localFolder = localStore.getFolder(account.getOutboxFolderName());
if (!localFolder.exists()) {
Timber.v("Outbox does not exist");
return;
}
for (MessagingListener l : getListeners()) {
l.sendPendingMessagesStarted(account);
}
localFolder.open(Folder.OPEN_MODE_RW);
List<LocalMessage> localMessages = localFolder.getMessages(null);
int progress = 0;
int todo = localMessages.size();
for (MessagingListener l : getListeners()) {
l.synchronizeMailboxProgress(account, account.getSentFolderName(), progress, todo);
}
/*
* The profile we will use to pull all of the content
* for a given local message into memory for sending.
*/
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.BODY);
Timber.i("Scanning folder '%s' (%d) for messages to send", account.getOutboxFolderName(), localFolder.getId());
Transport transport = transportProvider.getTransport(K9.app, account);
for (LocalMessage message : localMessages) {
if (message.isSet(Flag.DELETED)) {
message.destroy();
continue;
}
try {
AtomicInteger count = new AtomicInteger(0);
AtomicInteger oldCount = sendCount.putIfAbsent(message.getUid(), count);
if (oldCount != null) {
count = oldCount;
}
Timber.i("Send count for message %s is %d", message.getUid(), count.get());
if (count.incrementAndGet() > K9.MAX_SEND_ATTEMPTS) {
Timber.e("Send count for message %s can't be delivered after %d attempts. " + "Giving up until the user restarts the device", message.getUid(), MAX_SEND_ATTEMPTS);
notificationController.showSendFailedNotification(account, new MessagingException(message.getSubject()));
continue;
}
localFolder.fetch(Collections.singletonList(message), fp, null);
try {
if (message.getHeader(K9.IDENTITY_HEADER).length > 0) {
Timber.v("The user has set the Outbox and Drafts folder to the same thing. " + "This message appears to be a draft, so K-9 will not send it");
continue;
}
message.setFlag(Flag.X_SEND_IN_PROGRESS, true);
Timber.i("Sending message with UID %s", message.getUid());
transport.sendMessage(message);
message.setFlag(Flag.X_SEND_IN_PROGRESS, false);
message.setFlag(Flag.SEEN, true);
progress++;
for (MessagingListener l : getListeners()) {
l.synchronizeMailboxProgress(account, account.getSentFolderName(), progress, todo);
}
moveOrDeleteSentMessage(account, localStore, localFolder, message);
} catch (AuthenticationFailedException e) {
lastFailure = e;
wasPermanentFailure = false;
handleAuthenticationFailure(account, false);
handleSendFailure(account, localStore, localFolder, message, e, wasPermanentFailure);
} catch (CertificateValidationException e) {
lastFailure = e;
wasPermanentFailure = false;
notifyUserIfCertificateProblem(account, e, false);
handleSendFailure(account, localStore, localFolder, message, e, wasPermanentFailure);
} catch (MessagingException e) {
lastFailure = e;
wasPermanentFailure = e.isPermanentFailure();
handleSendFailure(account, localStore, localFolder, message, e, wasPermanentFailure);
} catch (Exception e) {
lastFailure = e;
wasPermanentFailure = true;
handleSendFailure(account, localStore, localFolder, message, e, wasPermanentFailure);
}
} catch (Exception e) {
lastFailure = e;
wasPermanentFailure = false;
Timber.e(e, "Failed to fetch message for sending");
addErrorMessage(account, "Failed to fetch message for sending", e);
notifySynchronizeMailboxFailed(account, localFolder, e);
}
}
for (MessagingListener l : getListeners()) {
l.sendPendingMessagesCompleted(account);
}
if (lastFailure != null) {
if (wasPermanentFailure) {
notificationController.showSendFailedNotification(account, lastFailure);
} else {
notificationController.showSendFailedNotification(account, lastFailure);
}
}
} catch (UnavailableStorageException e) {
Timber.i("Failed to send pending messages because storage is not available - trying again later.");
throw new UnavailableAccountException(e);
} catch (Exception e) {
Timber.v(e, "Failed to send pending messages");
for (MessagingListener l : getListeners()) {
l.sendPendingMessagesFailed(account);
}
addErrorMessage(account, null, e);
} finally {
if (lastFailure == null) {
notificationController.clearSendFailedNotification(account);
}
closeFolder(localFolder);
}
}
use of com.fsck.k9.K9 in project k-9 by k9mail.
the class MessageListFragment method onArchive.
private void onArchive(final List<MessageReference> messages) {
Map<Account, List<MessageReference>> messagesByAccount = groupMessagesByAccount(messages);
for (Entry<Account, List<MessageReference>> entry : messagesByAccount.entrySet()) {
Account account = entry.getKey();
String archiveFolder = account.getArchiveFolderName();
if (!K9.FOLDER_NONE.equals(archiveFolder)) {
move(entry.getValue(), archiveFolder);
}
}
}
use of com.fsck.k9.K9 in project k-9 by k9mail.
the class MessagingController method processPendingSetFlag.
/**
* Processes a pending mark read or unread command.
*/
void processPendingSetFlag(PendingSetFlag command, Account account) throws MessagingException {
String folder = command.folder;
if (account.getErrorFolderName().equals(folder) || account.getOutboxFolderName().equals(folder)) {
return;
}
boolean newState = command.newState;
Flag flag = command.flag;
Store remoteStore = account.getRemoteStore();
Folder remoteFolder = remoteStore.getFolder(folder);
if (!remoteFolder.exists() || !remoteFolder.isFlagSupported(flag)) {
return;
}
try {
remoteFolder.open(Folder.OPEN_MODE_RW);
if (remoteFolder.getMode() != Folder.OPEN_MODE_RW) {
return;
}
List<Message> messages = new ArrayList<>();
for (String uid : command.uids) {
if (!uid.startsWith(K9.LOCAL_UID_PREFIX)) {
messages.add(remoteFolder.getMessage(uid));
}
}
if (messages.isEmpty()) {
return;
}
remoteFolder.setFlags(messages, Collections.singleton(flag), newState);
} finally {
closeFolder(remoteFolder);
}
}
use of com.fsck.k9.K9 in project k-9 by k9mail.
the class ContactPicture method getContactPictureLoader.
public static ContactPictureLoader getContactPictureLoader(Context context) {
final int defaultBgColor;
if (!K9.isColorizeMissingContactPictures()) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.contactPictureFallbackDefaultBackgroundColor, outValue, true);
defaultBgColor = outValue.data;
} else {
defaultBgColor = 0;
}
return new ContactPictureLoader(context, defaultBgColor);
}
Aggregations