Sync/download improvements/fixes

pull/147/head
M66B 6 years ago
parent 1cb75e8720
commit 599e98840b

@ -987,20 +987,14 @@ public class ServiceSynchronize extends LifecycleService {
db.endTransaction();
}
if (folder.download) {
Boolean isMetered = Helper.isMetered(ServiceSynchronize.this);
boolean metered = (isMetered == null || isMetered);
try {
db.beginTransaction();
downloadMessage(
ServiceSynchronize.this, metered,
downloadMessage(ServiceSynchronize.this,
folder, ifolder, (IMAPMessage) imessage, message.id);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
} catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} catch (IOException ex) {
@ -1073,20 +1067,14 @@ public class ServiceSynchronize extends LifecycleService {
db.endTransaction();
}
if (folder.download) {
Boolean isMetered = Helper.isMetered(ServiceSynchronize.this);
boolean metered = (isMetered == null || isMetered);
try {
db.beginTransaction();
downloadMessage(
ServiceSynchronize.this, metered,
downloadMessage(ServiceSynchronize.this,
folder, ifolder, (IMAPMessage) e.getMessage(), message.id);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
} catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} catch (IOException ex) {
@ -2084,7 +2072,6 @@ public class ServiceSynchronize extends LifecycleService {
folder, ifolder, (IMAPMessage) isub[j], false, true);
ids[from + j] = message.id;
db.setTransactionSuccessful();
Thread.sleep(20);
} catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} catch (FolderClosedException ex) {
@ -2108,7 +2095,6 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderSyncState(folder.id, "downloading");
if (folder.download) {
//fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
// Download messages/attachments
@ -2119,18 +2105,11 @@ public class ServiceSynchronize extends LifecycleService {
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
// Fetch on demand
Boolean isMetered = Helper.isMetered(ServiceSynchronize.this);
boolean metered = (isMetered == null || isMetered);
for (int j = isub.length - 1; j >= 0 && state.running(); j--)
try {
db.beginTransaction();
if (ids[from + j] != null) {
downloadMessage(
this, metered,
folder, ifolder, (IMAPMessage) isub[j], ids[from + j]);
Thread.sleep(20);
}
if (ids[from + j] != null)
downloadMessage(this, folder, ifolder, (IMAPMessage) isub[j], ids[from + j]);
db.setTransactionSuccessful();
} catch (FolderClosedException ex) {
throw ex;
@ -2150,7 +2129,6 @@ public class ServiceSynchronize extends LifecycleService {
} catch (InterruptedException ignored) {
}
}
}
if (state.running)
db.folder().setFolderInitialized(folder.id);
@ -2384,22 +2362,26 @@ public class ServiceSynchronize extends LifecycleService {
return message;
}
private static void downloadMessage(Context context, boolean metered, EntityFolder folder, IMAPFolder ifolder, IMAPMessage imessage, long id) throws MessagingException, IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
long download = prefs.getInt("download", 32768);
if (download == 0)
download = Long.MAX_VALUE;
private static void downloadMessage(Context context, EntityFolder folder, IMAPFolder ifolder, IMAPMessage imessage, long id) throws MessagingException, IOException {
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
if (message == null)
return;
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
MessageHelper helper = new MessageHelper(imessage);
if (message.setContactInfo(context))
db.message().updateMessage(message);
if (folder.download) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
long download = prefs.getInt("download", 32768);
if (download == 0)
download = Long.MAX_VALUE;
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
MessageHelper helper = new MessageHelper(imessage);
Boolean isMetered = Helper.isMetered(context);
boolean metered = (isMetered == null || isMetered);
boolean fetch = false;
if (!message.content)
if (!metered || (message.size != null && message.size < download))
@ -2453,6 +2435,7 @@ public class ServiceSynchronize extends LifecycleService {
}
}
}
}
private class ServiceManager extends ConnectivityManager.NetworkCallback {
private ServiceState state;

Loading…
Cancel
Save