Delete background SYNC ops on stopping sync service

This commit is contained in:
M66B 2019-03-03 12:43:09 +00:00
parent ad5812f4f8
commit bc2d3a3d15
2 changed files with 20 additions and 3 deletions

View File

@ -72,6 +72,9 @@ public interface DaoOperation {
@Query("SELECT * FROM operation ORDER BY id")
List<EntityOperation> getOperations();
@Query("SELECT * FROM operation WHERE name = :name")
List<EntityOperation> getOperations(String name);
@Query("SELECT * FROM operation WHERE error IS NOT NULL")
List<EntityOperation> getOperationsError();

View File

@ -41,6 +41,9 @@ import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPStore;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@ -1065,6 +1068,8 @@ public class ServiceSynchronize extends LifecycleService {
@Override
public void run() {
DB db = DB.getInstance(ServiceSynchronize.this);
try {
wl.acquire();
@ -1074,9 +1079,7 @@ public class ServiceSynchronize extends LifecycleService {
if (doStop)
stop();
DB db = DB.getInstance(ServiceSynchronize.this);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
for (EntityAccount account : db.account().getAccountsTbd())
nm.deleteNotificationChannel(EntityAccount.getNotificationChannelName(account.id));
@ -1103,6 +1106,17 @@ public class ServiceSynchronize extends LifecycleService {
}
if (queued == 0 && !isEnabled()) {
EntityLog.log(ServiceSynchronize.this, "Service stop");
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC);
for (EntityOperation op : ops)
try {
JSONArray jargs = new JSONArray(op.args);
if (!jargs.getBoolean(3) /* foreground */) {
Log.i("Deleting bacground SYNC args=" + jargs);
db.operation().deleteOperation(op.id);
}
} catch (JSONException ex) {
Log.e(ex);
}
stopSelf();
}
}