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);
}
use of com.fsck.k9.K9 in project k-9 by k9mail.
the class MessageListFragment method changeSort.
/**
* Change the sort type and sort order used for the message list.
*
* @param sortType
* Specifies which field to use for sorting the message list.
* @param sortAscending
* Specifies the sort order. If this argument is {@code null} the default search order
* for the sort type is used.
*/
// FIXME: Don't save the changes in the UI thread
private void changeSort(SortType sortType, Boolean sortAscending) {
this.sortType = sortType;
Account account = this.account;
if (account != null) {
account.setSortType(this.sortType);
if (sortAscending == null) {
this.sortAscending = account.isSortAscending(this.sortType);
} else {
this.sortAscending = sortAscending;
}
account.setSortAscending(this.sortType, this.sortAscending);
sortDateAscending = account.isSortAscending(SortType.SORT_DATE);
account.save(preferences);
} else {
K9.setSortType(this.sortType);
if (sortAscending == null) {
this.sortAscending = K9.isSortAscending(this.sortType);
} else {
this.sortAscending = sortAscending;
}
K9.setSortAscending(this.sortType, this.sortAscending);
sortDateAscending = K9.isSortAscending(SortType.SORT_DATE);
StorageEditor editor = preferences.getStorage().edit();
K9.save(editor);
editor.commit();
}
reSort();
}
use of com.fsck.k9.K9 in project k-9 by k9mail.
the class LocalFolder method moveMessages.
@Override
public Map<String, String> moveMessages(final List<? extends Message> msgs, final Folder destFolder) throws MessagingException {
if (!(destFolder instanceof LocalFolder)) {
throw new MessagingException("moveMessages called with non-LocalFolder");
}
final LocalFolder lDestFolder = (LocalFolder) destFolder;
final Map<String, String> uidMap = new HashMap<>();
try {
this.localStore.database.execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try {
lDestFolder.open(OPEN_MODE_RW);
for (Message message : msgs) {
LocalMessage lMessage = (LocalMessage) message;
String oldUID = message.getUid();
Timber.d("Updating folder_id to %s for message with UID %s, " + "id %d currently in folder %s", lDestFolder.getId(), message.getUid(), lMessage.getId(), getName());
String newUid = K9.LOCAL_UID_PREFIX + UUID.randomUUID().toString();
message.setUid(newUid);
uidMap.put(oldUID, newUid);
// Message threading in the target folder
ThreadInfo threadInfo = lDestFolder.doMessageThreading(db, message);
/*
* "Move" the message into the new folder
*/
long msgId = lMessage.getId();
String[] idArg = new String[] { Long.toString(msgId) };
ContentValues cv = new ContentValues();
cv.put("folder_id", lDestFolder.getId());
cv.put("uid", newUid);
db.update("messages", cv, "id = ?", idArg);
// Create/update entry in 'threads' table for the message in the
// target folder
cv.clear();
cv.put("message_id", msgId);
if (threadInfo.threadId == -1) {
if (threadInfo.rootId != -1) {
cv.put("root", threadInfo.rootId);
}
if (threadInfo.parentId != -1) {
cv.put("parent", threadInfo.parentId);
}
db.insert("threads", null, cv);
} else {
db.update("threads", cv, "id = ?", new String[] { Long.toString(threadInfo.threadId) });
}
/*
* Add a placeholder message so we won't download the original
* message again if we synchronize before the remote move is
* complete.
*/
// We need to open this folder to get the folder id
open(OPEN_MODE_RW);
cv.clear();
cv.put("uid", oldUID);
cv.putNull("flags");
cv.put("read", 1);
cv.put("deleted", 1);
cv.put("folder_id", mFolderId);
cv.put("empty", 0);
String messageId = message.getMessageId();
if (messageId != null) {
cv.put("message_id", messageId);
}
final long newId;
if (threadInfo.msgId != -1) {
// There already existed an empty message in the target folder.
// Let's use it as placeholder.
newId = threadInfo.msgId;
db.update("messages", cv, "id = ?", new String[] { Long.toString(newId) });
} else {
newId = db.insert("messages", null, cv);
}
/*
* Update old entry in 'threads' table to point to the newly
* created placeholder.
*/
cv.clear();
cv.put("message_id", newId);
db.update("threads", cv, "id = ?", new String[] { Long.toString(lMessage.getThreadId()) });
}
} catch (MessagingException e) {
throw new WrappedException(e);
}
return null;
}
});
this.localStore.notifyChange();
return uidMap;
} catch (WrappedException e) {
throw (MessagingException) e.getCause();
}
}
use of com.fsck.k9.K9 in project k-9 by k9mail.
the class MessagingController method checkMail.
/**
* Checks mail for one or multiple accounts. If account is null all accounts
* are checked.
*/
public void checkMail(final Context context, final Account account, final boolean ignoreLastCheckedTime, final boolean useManualWakeLock, final MessagingListener listener) {
TracingWakeLock twakeLock = null;
if (useManualWakeLock) {
TracingPowerManager pm = TracingPowerManager.getPowerManager(context);
twakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9 MessagingController.checkMail");
twakeLock.setReferenceCounted(false);
twakeLock.acquire(K9.MANUAL_WAKE_LOCK_TIMEOUT);
}
final TracingWakeLock wakeLock = twakeLock;
for (MessagingListener l : getListeners()) {
l.checkMailStarted(context, account);
}
putBackground("checkMail", listener, new Runnable() {
@Override
public void run() {
try {
Timber.i("Starting mail check");
Preferences prefs = Preferences.getPreferences(context);
Collection<Account> accounts;
if (account != null) {
accounts = new ArrayList<>(1);
accounts.add(account);
} else {
accounts = prefs.getAvailableAccounts();
}
for (final Account account : accounts) {
checkMailForAccount(context, account, ignoreLastCheckedTime, listener);
}
} catch (Exception e) {
Timber.e(e, "Unable to synchronize mail");
addErrorMessage(account, null, e);
}
putBackground("finalize sync", null, new Runnable() {
@Override
public void run() {
Timber.i("Finished mail sync");
if (wakeLock != null) {
wakeLock.release();
}
for (MessagingListener l : getListeners()) {
l.checkMailFinished(context, account);
}
}
});
}
});
}
Aggregations