1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 19:52:17 -05:00

Cleaned up message loading threading solving issues 249 and 524

This commit is contained in:
Bao-Long Nguyen-Trong 2009-07-01 21:31:51 +00:00
parent 2521e032a7
commit a912f7aed5

View File

@ -68,9 +68,8 @@ import com.android.email.mail.store.LocalStore.LocalTextBody;
import com.android.email.provider.AttachmentProvider; import com.android.email.provider.AttachmentProvider;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MessageView extends K9Activity public class MessageView extends K9Activity
implements UrlInterceptHandler, OnClickListener { implements UrlInterceptHandler, OnClickListener {
@ -79,7 +78,6 @@ public class MessageView extends K9Activity
private static final String EXTRA_MESSAGE = "com.android.email.MessageView_message"; private static final String EXTRA_MESSAGE = "com.android.email.MessageView_message";
private static final String EXTRA_FOLDER_UIDS = "com.android.email.MessageView_folderUids"; private static final String EXTRA_FOLDER_UIDS = "com.android.email.MessageView_folderUids";
private static final String EXTRA_NEXT = "com.android.email.MessageView_next"; private static final String EXTRA_NEXT = "com.android.email.MessageView_next";
private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 120000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
private static final String CID_PREFIX = "http://cid/"; private static final String CID_PREFIX = "http://cid/";
@ -116,6 +114,10 @@ public class MessageView extends K9Activity
private DateFormat timeFormat = null; private DateFormat timeFormat = null;
private Menu optionsMenu = null; private Menu optionsMenu = null;
//Shall we use more threads? How often will the user move from non-fully-downloaded
//messages to another non-fully-downloaded message more than 3 times?
private final ExecutorService threadPool = Executors.newFixedThreadPool(3);
private DateFormat getDateFormat() private DateFormat getDateFormat()
{ {
@ -472,19 +474,6 @@ public class MessageView extends K9Activity
displayMessage(mMessageUid); displayMessage(mMessageUid);
} }
Thread loaderThread = new Thread() {
public void run() {
// TODO this is a spot that should be eventually handled by a MessagingController
// thread pool. We want it in a thread but it can't be blocked by the normal
// synchronization stuff in MC.
MessagingController.getInstance(getApplication()).loadMessageForViewSynchronous(
mAccount,
mFolder,
mMessageUid,
null);
}
};
private void displayMessage(String uid) private void displayMessage(String uid)
{ {
mMessageUid = uid; mMessageUid = uid;
@ -496,6 +485,17 @@ public class MessageView extends K9Activity
next_scrolling.setEnabled(mNextMessageUid != null ); next_scrolling.setEnabled(mNextMessageUid != null );
if (previous_scrolling != null) if (previous_scrolling != null)
previous_scrolling.setEnabled(mPreviousMessageUid != null); previous_scrolling.setEnabled(mPreviousMessageUid != null);
Runnable loaderThread = new Runnable() {
public void run() {
MessagingController.getInstance(getApplication()).loadMessageForViewSynchronous(
mAccount,
mFolder,
mMessageUid,
null);
}
};
threadPool.execute(loaderThread); threadPool.execute(loaderThread);
} }
@ -1116,6 +1116,10 @@ public class MessageView extends K9Activity
@Override @Override
public void loadMessageForViewHeadersAvailable(Account account, String folder, String uid, public void loadMessageForViewHeadersAvailable(Account account, String folder, String uid,
final Message message) { final Message message) {
if (!mMessageUid.equals(uid)) {
return;
}
MessageView.this.mMessage = message; MessageView.this.mMessage = message;
try { try {
setHeaders(account, folder, uid, message); setHeaders(account, folder, uid, message);
@ -1130,7 +1134,10 @@ public class MessageView extends K9Activity
@Override @Override
public void loadMessageForViewBodyAvailable(Account account, String folder, String uid, public void loadMessageForViewBodyAvailable(Account account, String folder, String uid,
Message message) { Message message) {
Spannable markup; if (!mMessageUid.equals(uid)) {
return;
}
MessageView.this.mMessage = message; MessageView.this.mMessage = message;
try { try {
String text; String text;
@ -1179,6 +1186,10 @@ public class MessageView extends K9Activity
@Override @Override
public void loadMessageForViewFailed(Account account, String folder, String uid, public void loadMessageForViewFailed(Account account, String folder, String uid,
final String message) { final String message) {
if (!mMessageUid.equals(uid)) {
return;
}
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
public void run() { public void run() {
setProgressBarIndeterminateVisibility(false); setProgressBarIndeterminateVisibility(false);
@ -1191,6 +1202,10 @@ public class MessageView extends K9Activity
@Override @Override
public void loadMessageForViewFinished(Account account, String folder, String uid, public void loadMessageForViewFinished(Account account, String folder, String uid,
Message message) { Message message) {
if (!mMessageUid.equals(uid)) {
return;
}
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
public void run() { public void run() {
setProgressBarIndeterminateVisibility(false); setProgressBarIndeterminateVisibility(false);
@ -1200,6 +1215,10 @@ public class MessageView extends K9Activity
@Override @Override
public void loadMessageForViewStarted(Account account, String folder, String uid) { public void loadMessageForViewStarted(Account account, String folder, String uid) {
if (!mMessageUid.equals(uid)) {
return;
}
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
public void run() { public void run() {
mMessageContentView.loadUrl("file:///android_asset/loading.html"); mMessageContentView.loadUrl("file:///android_asset/loading.html");
@ -1211,6 +1230,10 @@ public class MessageView extends K9Activity
@Override @Override
public void loadAttachmentStarted(Account account, Message message, public void loadAttachmentStarted(Account account, Message message,
Part part, Object tag, boolean requiresDownload) { Part part, Object tag, boolean requiresDownload) {
if (mMessage!=message) {
return;
}
mHandler.setAttachmentsEnabled(false); mHandler.setAttachmentsEnabled(false);
mHandler.progress(true); mHandler.progress(true);
if (requiresDownload) { if (requiresDownload) {
@ -1221,6 +1244,10 @@ public class MessageView extends K9Activity
@Override @Override
public void loadAttachmentFinished(Account account, Message message, public void loadAttachmentFinished(Account account, Message message,
Part part, Object tag) { Part part, Object tag) {
if (mMessage!=message) {
return;
}
mHandler.setAttachmentsEnabled(true); mHandler.setAttachmentsEnabled(true);
mHandler.progress(false); mHandler.progress(false);
@ -1262,6 +1289,10 @@ public class MessageView extends K9Activity
@Override @Override
public void loadAttachmentFailed(Account account, Message message, Part part, public void loadAttachmentFailed(Account account, Message message, Part part,
Object tag, String reason) { Object tag, String reason) {
if (mMessage!=message) {
return;
}
mHandler.setAttachmentsEnabled(true); mHandler.setAttachmentsEnabled(true);
mHandler.progress(false); mHandler.progress(false);
mHandler.networkError(); mHandler.networkError();