mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-27 19:52:17 -05:00
Make MessageListHandler a real Handler that uses Android's message queue
This commit is contained in:
parent
691fb2a84a
commit
75a97a82c0
@ -17,6 +17,7 @@ import android.graphics.Color;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.style.AbsoluteSizeSpan;
|
||||
@ -348,17 +349,105 @@ public class MessageList
|
||||
}
|
||||
}
|
||||
|
||||
class MessageListHandler {
|
||||
class MessageListHandler extends Handler {
|
||||
private static final int ACTION_REMOVE_MESSAGES = 1;
|
||||
private static final int ACTION_ADD_MESSAGES = 2;
|
||||
private static final int ACTION_RESET_UNREAD_COUNT = 3;
|
||||
private static final int ACTION_SORT_MESSAGES = 4;
|
||||
private static final int ACTION_FOLDER_LOADING = 5;
|
||||
private static final int ACTION_REFRESH_TITLE = 6;
|
||||
private static final int ACTION_PROGRESS = 7;
|
||||
|
||||
|
||||
/**
|
||||
* @param messages Never {@code null}.
|
||||
*/
|
||||
public void removeMessages(final List<MessageInfoHolder> messages) {
|
||||
android.os.Message msg = android.os.Message.obtain(this, ACTION_REMOVE_MESSAGES, messages);
|
||||
sendMessage(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messages Never {@code null}.
|
||||
*/
|
||||
public void addMessages(final List<MessageInfoHolder> messages) {
|
||||
android.os.Message msg = android.os.Message.obtain(this, ACTION_ADD_MESSAGES, messages);
|
||||
sendMessage(msg);
|
||||
}
|
||||
|
||||
public void resetUnreadCount() {
|
||||
android.os.Message msg = android.os.Message.obtain(this, ACTION_RESET_UNREAD_COUNT);
|
||||
sendMessage(msg);
|
||||
}
|
||||
|
||||
public void sortMessages() {
|
||||
android.os.Message msg = android.os.Message.obtain(this, ACTION_SORT_MESSAGES);
|
||||
sendMessage(msg);
|
||||
}
|
||||
|
||||
public void folderLoading(String folder, boolean loading) {
|
||||
android.os.Message msg = android.os.Message.obtain(this, ACTION_FOLDER_LOADING,
|
||||
(loading) ? 1 : 0, 0, folder);
|
||||
sendMessage(msg);
|
||||
}
|
||||
|
||||
public void refreshTitle() {
|
||||
android.os.Message msg = android.os.Message.obtain(this, ACTION_REFRESH_TITLE);
|
||||
sendMessage(msg);
|
||||
}
|
||||
|
||||
public void progress(final boolean progress) {
|
||||
android.os.Message msg = android.os.Message.obtain(this, ACTION_PROGRESS,
|
||||
(progress) ? 1 : 0, 0);
|
||||
sendMessage(msg);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void handleMessage(android.os.Message msg) {
|
||||
switch (msg.what) {
|
||||
case ACTION_REMOVE_MESSAGES: {
|
||||
List<MessageInfoHolder> messages = (List<MessageInfoHolder>) msg.obj;
|
||||
MessageList.this.removeMessages(messages);
|
||||
break;
|
||||
}
|
||||
case ACTION_ADD_MESSAGES: {
|
||||
List<MessageInfoHolder> messages = (List<MessageInfoHolder>) msg.obj;
|
||||
MessageList.this.addMessages(messages);
|
||||
break;
|
||||
}
|
||||
case ACTION_RESET_UNREAD_COUNT: {
|
||||
MessageList.this.resetUnreadCount();
|
||||
break;
|
||||
}
|
||||
case ACTION_SORT_MESSAGES: {
|
||||
MessageList.this.sortMessages();
|
||||
break;
|
||||
}
|
||||
case ACTION_FOLDER_LOADING: {
|
||||
String folder = (String) msg.obj;
|
||||
boolean loading = (msg.arg1 == 1);
|
||||
MessageList.this.folderLoading(folder, loading);
|
||||
break;
|
||||
}
|
||||
case ACTION_REFRESH_TITLE: {
|
||||
MessageList.this.refreshTitle();
|
||||
break;
|
||||
}
|
||||
case ACTION_PROGRESS: {
|
||||
boolean progress = (msg.arg1 == 1);
|
||||
MessageList.this.progress(progress);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeMessages(final List<MessageInfoHolder> messages) {
|
||||
if (messages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
for (MessageInfoHolder message : messages) {
|
||||
if (message != null && (mFolderName == null || (
|
||||
message.folder != null &&
|
||||
@ -369,25 +458,19 @@ public class MessageList
|
||||
mAdapter.messages.remove(message);
|
||||
}
|
||||
}
|
||||
resetUnreadCountOnThread();
|
||||
resetUnreadCount();
|
||||
|
||||
mAdapter.notifyDataSetChanged();
|
||||
toggleBatchButtons();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messages Never {@code null}.
|
||||
*/
|
||||
public void addMessages(final List<MessageInfoHolder> messages) {
|
||||
private void addMessages(final List<MessageInfoHolder> messages) {
|
||||
if (messages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final boolean wasEmpty = mAdapter.messages.isEmpty();
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
for (final MessageInfoHolder message : messages) {
|
||||
if (mFolderName == null || (message.folder != null && message.folder.name.equals(mFolderName))) {
|
||||
int index;
|
||||
@ -406,23 +489,12 @@ public class MessageList
|
||||
if (wasEmpty) {
|
||||
mListView.setSelection(0);
|
||||
}
|
||||
resetUnreadCountOnThread();
|
||||
resetUnreadCount();
|
||||
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void resetUnreadCount() {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
resetUnreadCountOnThread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void resetUnreadCountOnThread() {
|
||||
if (mQueryString != null) {
|
||||
int unreadCount = 0;
|
||||
synchronized (mAdapter.messages) {
|
||||
@ -431,23 +503,18 @@ public class MessageList
|
||||
}
|
||||
}
|
||||
mUnreadMessageCount = unreadCount;
|
||||
refreshTitleOnThread();
|
||||
refreshTitle();
|
||||
}
|
||||
}
|
||||
|
||||
private void sortMessages() {
|
||||
final Comparator<MessageInfoHolder> chainComparator = getComparator();
|
||||
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (mAdapter.messages) {
|
||||
Collections.sort(mAdapter.messages, chainComparator);
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The comparator to use to display messages in an ordered
|
||||
@ -484,27 +551,14 @@ public class MessageList
|
||||
return chainComparator;
|
||||
}
|
||||
|
||||
public void folderLoading(String folder, boolean loading) {
|
||||
private void folderLoading(String folder, boolean loading) {
|
||||
if (mCurrentFolder != null && mCurrentFolder.name.equals(folder)) {
|
||||
mCurrentFolder.loading = loading;
|
||||
}
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override public void run() {
|
||||
updateFooterView();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void refreshTitle() {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
refreshTitleOnThread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void refreshTitleOnThread() {
|
||||
setWindowTitle();
|
||||
setWindowProgress();
|
||||
}
|
||||
@ -549,15 +603,9 @@ public class MessageList
|
||||
}
|
||||
}
|
||||
|
||||
public void progress(final boolean progress) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
private void progress(final boolean progress) {
|
||||
showProgressIndicator(progress);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the message list that was used to open the {@link MessageView} for a message.
|
||||
|
Loading…
Reference in New Issue
Block a user