|
|
@ -43,6 +43,7 @@ import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
|
|
|
|
|
|
|
@ -854,8 +855,32 @@ public class MessageHelper {
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void downloadAttachment(Context context, int index, long id, String name) throws MessagingException, IOException {
|
|
|
|
void downloadAttachment(Context context, EntityAttachment local) throws IOException, MessagingException {
|
|
|
|
Log.i("downloading attachment id=" + id);
|
|
|
|
List<EntityAttachment> remotes = getAttachments();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Match attachment by attributes
|
|
|
|
|
|
|
|
// Some servers order attachments randomly
|
|
|
|
|
|
|
|
boolean found = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < remotes.size(); i++) {
|
|
|
|
|
|
|
|
EntityAttachment remote = remotes.get(i);
|
|
|
|
|
|
|
|
if (Objects.equals(remote.name, local.name) &&
|
|
|
|
|
|
|
|
Objects.equals(remote.disposition, local.disposition) &&
|
|
|
|
|
|
|
|
Objects.equals(remote.cid, local.cid)) {
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
downloadAttachment(context, i, local);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
|
|
|
|
Log.w("Attachment not found local=" + local);
|
|
|
|
|
|
|
|
for (EntityAttachment remote : remotes)
|
|
|
|
|
|
|
|
Log.w("Attachment remote=" + remote);
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Attachment not found");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void downloadAttachment(Context context, int index, EntityAttachment local) throws MessagingException, IOException {
|
|
|
|
|
|
|
|
Log.i("downloading attachment id=" + local.id);
|
|
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
|
|
|
@ -863,8 +888,8 @@ public class MessageHelper {
|
|
|
|
AttachmentPart apart = attachments.get(index);
|
|
|
|
AttachmentPart apart = attachments.get(index);
|
|
|
|
|
|
|
|
|
|
|
|
// Download attachment
|
|
|
|
// Download attachment
|
|
|
|
File file = EntityAttachment.getFile(context, id, name);
|
|
|
|
File file = EntityAttachment.getFile(context, local.id, local.name);
|
|
|
|
db.attachment().setProgress(id, null);
|
|
|
|
db.attachment().setProgress(local.id, null);
|
|
|
|
try (InputStream is = apart.part.getInputStream()) {
|
|
|
|
try (InputStream is = apart.part.getInputStream()) {
|
|
|
|
long size = 0;
|
|
|
|
long size = 0;
|
|
|
|
long total = apart.part.getSize();
|
|
|
|
long total = apart.part.getSize();
|
|
|
@ -881,25 +906,25 @@ public class MessageHelper {
|
|
|
|
int progress = (int) (size * 100 / total / 20 * 20);
|
|
|
|
int progress = (int) (size * 100 / total / 20 * 20);
|
|
|
|
if (progress != lastprogress) {
|
|
|
|
if (progress != lastprogress) {
|
|
|
|
lastprogress = progress;
|
|
|
|
lastprogress = progress;
|
|
|
|
db.attachment().setProgress(id, progress);
|
|
|
|
db.attachment().setProgress(local.id, progress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Store attachment data
|
|
|
|
// Store attachment data
|
|
|
|
db.attachment().setDownloaded(id, size);
|
|
|
|
db.attachment().setDownloaded(local.id, size);
|
|
|
|
|
|
|
|
|
|
|
|
Log.i("Downloaded attachment size=" + size);
|
|
|
|
Log.i("Downloaded attachment size=" + size);
|
|
|
|
} catch (FolderClosedIOException ex) {
|
|
|
|
} catch (FolderClosedIOException ex) {
|
|
|
|
db.attachment().setError(id, Helper.formatThrowable(ex));
|
|
|
|
db.attachment().setError(local.id, Helper.formatThrowable(ex));
|
|
|
|
throw new FolderClosedException(ex.getFolder(), "downloadAttachment", ex);
|
|
|
|
throw new FolderClosedException(ex.getFolder(), "downloadAttachment", ex);
|
|
|
|
} catch (MessageRemovedIOException ex) {
|
|
|
|
} catch (MessageRemovedIOException ex) {
|
|
|
|
db.attachment().setError(id, Helper.formatThrowable(ex));
|
|
|
|
db.attachment().setError(local.id, Helper.formatThrowable(ex));
|
|
|
|
throw new MessagingException("downloadAttachment", ex);
|
|
|
|
throw new MessagingException("downloadAttachment", ex);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
// Reset progress on failure
|
|
|
|
// Reset progress on failure
|
|
|
|
db.attachment().setError(id, Helper.formatThrowable(ex));
|
|
|
|
db.attachment().setError(local.id, Helper.formatThrowable(ex));
|
|
|
|
throw ex;
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|