1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-01-30 23:00:09 -05:00

More performance improvements. Still get occasional connection errors with 50+ messages viewable due to timeout issues.

This commit is contained in:
Matthew Brace 2008-12-10 23:30:00 +00:00
parent 8b6a8c4bfa
commit 8d3579fe74

View File

@ -33,6 +33,7 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Store; import com.fsck.k9.mail.Store;
import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.internet.TextBody;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
@ -877,24 +878,166 @@ public class WebDavStore extends Store {
return; return;
} }
if (needAuth()) {
authenticate();
}
if (WebDavStore.this.mAuthenticated == false ||
WebDavStore.this.mAuthCookies == null) {
return;
}
/** /**
* Get message info for all messages here since it can be pulled with * Fetch message flag info for the array
* a single request. Header data will be set in the for loop. */
* Listener isn't started yet since it isn't a per-message lookup. if (fp.contains(FetchProfile.Item.FLAGS)) {
fetchFlags(messages, listener);
}
/**
* Fetch message envelope information for the array
*/ */
if (fp.contains(FetchProfile.Item.ENVELOPE)) { if (fp.contains(FetchProfile.Item.ENVELOPE)) {
fetchEnvelope(messages, listener); fetchEnvelope(messages, listener);
} }
for (int i = 0, count = messages.length; i < count; i++) {
if (!(messages[i] instanceof WebDavMessage)) {
throw new MessagingException("WebDavStore fetch called with non-WebDavMessage");
}
WebDavMessage wdMessage = (WebDavMessage) messages[i];
if (listener != null) {
listener.messageStarted(wdMessage.getUid(), i, count);
}
/** /**
* Check for flags and get the status here since it can be pulled with * Set the body to null if it's asking for the structure because
* just one request. Flags will be set inside the for loop. * we don't support it yet.
* Listener isn't started yet since it isn't a per-message lookup.
*/ */
if (fp.contains(FetchProfile.Item.FLAGS)) { if (fp.contains(FetchProfile.Item.STRUCTURE)) {
wdMessage.setBody(null);
}
/**
* Message fetching that we can pull as a stream
*/
if (fp.contains(FetchProfile.Item.BODY) ||
fp.contains(FetchProfile.Item.BODY_SANE)) {
DefaultHttpClient httpclient = new DefaultHttpClient();
InputStream istream = null;
InputStream resultStream = null;
HttpGet httpget;
HttpEntity entity;
HttpResponse response;
int statusCode = 0;
try {
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
/**
* If fetch is called outside of the initial list (ie, a locally stored
* stored message), it may not have a URL associated. Verify and fix that
*/
if (wdMessage.getUrl().equals("")) {
wdMessage.setUrl(getMessageUrls(new String[] {wdMessage.getUid()}).get(wdMessage.getUid()));
if (wdMessage.getUrl().equals("")) {
throw new MessagingException("Unable to get URL for message");
}
}
httpget = new HttpGet(new URI(wdMessage.getUrl()));
httpget.setHeader("translate", "f");
response = httpclient.execute(httpget);
statusCode = response.getStatusLine().getStatusCode();
if (statusCode < 200 ||
statusCode > 300) {
throw new IOException("Status Code in invalid range");
}
entity = response.getEntity();
if (entity != null) {
StringBuffer buffer = new StringBuffer();
String tempText = new String();
String resultText = new String();
String bodyBoundary = "";
BufferedReader reader;
int totalLines = FETCH_BODY_SANE_SUGGESTED_SIZE / 76;
int lines = 0;
istream = entity.getContent();
if (fp.contains(FetchProfile.Item.BODY_SANE)) {
reader = new BufferedReader(new InputStreamReader(istream), 8192);
while ((tempText = reader.readLine()) != null &&
(lines < totalLines)) {
buffer.append(tempText+"\r\n");
lines++;
}
istream.close();
resultText = buffer.toString();
istream = new ByteArrayInputStream(resultText.getBytes("UTF-8"));
}
wdMessage.parse(istream);
}
} catch (IllegalArgumentException iae) {
Log.e(k9.LOG_TAG, "IllegalArgumentException caught " + iae);
} catch (URISyntaxException use) {
Log.e(k9.LOG_TAG, "URISyntaxException caught " + use);
} catch (IOException ioe) {
Log.e(k9.LOG_TAG, "Non-success response code loading message, response code was " + statusCode);
}
}
if (listener != null) {
listener.messageFinished(wdMessage, i, count);
}
}
}
/**
* Fetches and sets the message flags for the supplied messages.
* The idea is to have this be recursive so that we do a series of medium calls
* instead of one large massive call or a large number of smaller calls.
*/
private void fetchFlags(Message[] startMessages, MessageRetrievalListener listener) throws MessagingException {
HashMap<String, Boolean> uidToReadStatus = new HashMap<String, Boolean>();
DefaultHttpClient httpclient = new DefaultHttpClient(); DefaultHttpClient httpclient = new DefaultHttpClient();
String messageBody = new String(); String messageBody = new String();
String[] uids = new String[messages.length]; Message[] messages = new Message[20];
String[] uids;
if (startMessages == null ||
startMessages.length == 0) {
return;
}
if (startMessages.length > 20) {
Message[] newMessages = new Message[startMessages.length - 20];
for (int i = 0, count = startMessages.length; i < count; i++) {
if (i < 20) {
messages[i] = startMessages[i];
} else {
newMessages[i - 20] = startMessages[i];
}
}
fetchFlags(newMessages, listener);
} else {
messages = startMessages;
}
uids = new String[messages.length];
for (int i = 0, count = messages.length; i < count; i++) { for (int i = 0, count = messages.length; i < count; i++) {
uids[i] = messages[i].getUid(); uids[i] = messages[i].getUid();
@ -950,7 +1093,6 @@ public class WebDavStore extends Store {
} catch (IOException ioe) { } catch (IOException ioe) {
Log.e(k9.LOG_TAG, "IOException: " + ioe); Log.e(k9.LOG_TAG, "IOException: " + ioe);
} }
}
for (int i = 0, count = messages.length; i < count; i++) { for (int i = 0, count = messages.length; i < count; i++) {
if (!(messages[i] instanceof WebDavMessage)) { if (!(messages[i] instanceof WebDavMessage)) {
@ -959,96 +1101,13 @@ public class WebDavStore extends Store {
WebDavMessage wdMessage = (WebDavMessage) messages[i]; WebDavMessage wdMessage = (WebDavMessage) messages[i];
if (listener != null) { if (listener != null) {
listener.messageStarted(wdMessage.getUid(), i, count); listener.messageStarted(messages[i].getUid(), i, count);
} }
if (fp.contains(FetchProfile.Item.FLAGS)) {
wdMessage.setFlagInternal(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid())); wdMessage.setFlagInternal(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid()));
}
/**
* Set the body to null if it's asking for the structure because
* we don't support it yet.
*/
if (fp.contains(FetchProfile.Item.STRUCTURE)) {
wdMessage.setBody(null);
}
/**
* Message fetching that we can pull as a stream
*/
if (fp.contains(FetchProfile.Item.BODY) ||
fp.contains(FetchProfile.Item.BODY_SANE)) {
DefaultHttpClient httpclient = new DefaultHttpClient();
InputStream istream = null;
InputStream resultStream = null;
HttpGet httpget;
HttpEntity entity;
HttpResponse response;
int statusCode = 0;
try {
httpclient.setCookieStore(WebDavStore.this.mAuthCookies);
httpget = new HttpGet(new URI(wdMessage.getUrl()));
httpget.setHeader("translate", "f");
response = httpclient.execute(httpget);
statusCode = response.getStatusLine().getStatusCode();
if (statusCode < 200 ||
statusCode > 300) {
throw new IOException("Status Code in invalid range");
}
entity = response.getEntity();
if (entity != null) {
String resultText = new String();
String tempText = new String();
BufferedReader reader;
resultText = "";
istream = entity.getContent();
/**
* Keep this commented out for now, messages won't display properly if
* we do it like this.
*/
/**
if (fp.contains(FetchProfile.Item.BODY_SANE)) {
int lines = FETCH_BODY_SANE_SUGGESTED_SIZE / 76;
int line = 0;
reader = new BufferedReader(new InputStreamReader(istream),4096);
while ((tempText = reader.readLine()) != null &&
(line < lines)) {
if (resultText.equals("")) {
resultText = tempText;
} else {
resultText = resultText + "\r\n" + tempText;
}
line++;
}
istream.close();
istream = new ByteArrayInputStream(resultText.getBytes("UTF-8"));
}*/
wdMessage.parse(istream);
}
} catch (IllegalArgumentException iae) {
Log.e(k9.LOG_TAG, "IllegalArgumentException caught " + iae);
} catch (URISyntaxException use) {
Log.e(k9.LOG_TAG, "URISyntaxException caught " + use);
} catch (IOException ioe) {
Log.e(k9.LOG_TAG, "Non-success response code loading message, response code was " + statusCode);
}
}
if (listener != null) { if (listener != null) {
listener.messageFinished(wdMessage, i, count); listener.messageFinished(messages[i], i, count);
} }
} }
} }
@ -1151,10 +1210,7 @@ public class WebDavStore extends Store {
if (listener != null) { if (listener != null) {
listener.messageStarted(messages[i].getUid(), i, count); listener.messageStarted(messages[i].getUid(), i, count);
} }
/**
((WebDavMessage) messages[i]).setNewHeaders(envelopes.get(((WebDavMessage) messages[i]).getUid()));
((WebDavMessage) messages[i]).setFlagInternal(Flag.SEEN, envelopes.get(((WebDavMessage) messages[i]).getUid()).getReadStatus());
*/
wdMessage.setNewHeaders(envelopes.get(wdMessage.getUid())); wdMessage.setNewHeaders(envelopes.get(wdMessage.getUid()));
wdMessage.setFlagInternal(Flag.SEEN, envelopes.get(wdMessage.getUid()).getReadStatus()); wdMessage.setFlagInternal(Flag.SEEN, envelopes.get(wdMessage.getUid()).getReadStatus());
@ -1301,7 +1357,7 @@ public class WebDavStore extends Store {
* A WebDav Message * A WebDav Message
*/ */
class WebDavMessage extends MimeMessage { class WebDavMessage extends MimeMessage {
private String mUrl = null; private String mUrl = new String();
WebDavMessage(String uid, Folder folder) throws MessagingException { WebDavMessage(String uid, Folder folder) throws MessagingException {
this.mUid = uid; this.mUid = uid;