Browse Source

Yield wake lock

main
M66B 6 years ago
parent
commit
681a8a31e2
1 changed files with 27 additions and 26 deletions
  1. +27
    -26
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java

+ 27
- 26
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -583,9 +583,15 @@ public class ServiceSynchronize extends LifecycleService {
istore.addStoreListener(new StoreListener() { istore.addStoreListener(new StoreListener() {
@Override @Override
public void notification(StoreEvent e) { public void notification(StoreEvent e) {
Log.i(Helper.TAG, account.name + " event: " + e.getMessage());
db.account().setAccountError(account.id, e.getMessage());
state.thread.interrupt();
try {
wl.acquire();
Log.i(Helper.TAG, account.name + " event: " + e.getMessage());
db.account().setAccountError(account.id, e.getMessage());
state.thread.interrupt();
yieldWakelock();
} finally {
wl.release();
}
} }
}); });
@ -597,10 +603,7 @@ public class ServiceSynchronize extends LifecycleService {
wl.acquire(); wl.acquire();
Log.i(Helper.TAG, "Folder created=" + e.getFolder().getFullName()); Log.i(Helper.TAG, "Folder created=" + e.getFolder().getFullName());
state.thread.interrupt(); state.thread.interrupt();
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
yieldWakelock();
} finally { } finally {
wl.release(); wl.release();
} }
@ -618,10 +621,7 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(Helper.TAG, "Renamed to " + name + " count=" + count); Log.i(Helper.TAG, "Renamed to " + name + " count=" + count);
state.thread.interrupt(); state.thread.interrupt();
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
yieldWakelock();
} finally { } finally {
wl.release(); wl.release();
} }
@ -633,10 +633,7 @@ public class ServiceSynchronize extends LifecycleService {
wl.acquire(); wl.acquire();
Log.i(Helper.TAG, "Folder deleted=" + e.getFolder().getFullName()); Log.i(Helper.TAG, "Folder deleted=" + e.getFolder().getFullName());
state.thread.interrupt(); state.thread.interrupt();
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
yieldWakelock();
} finally { } finally {
wl.release(); wl.release();
} }
@ -752,6 +749,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
state.thread.interrupt(); state.thread.interrupt();
yieldWakelock();
} finally { } finally {
wl.release(); wl.release();
} }
@ -833,6 +831,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
state.thread.interrupt(); state.thread.interrupt();
yieldWakelock();
} finally { } finally {
wl.release(); wl.release();
} }
@ -846,6 +845,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
state.thread.interrupt(); state.thread.interrupt();
yieldWakelock();
} finally { } finally {
wl.release(); wl.release();
} }
@ -874,6 +874,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
state.thread.interrupt(); state.thread.interrupt();
yieldWakelock();
} finally { } finally {
Log.i(Helper.TAG, folder.name + " end idle"); Log.i(Helper.TAG, folder.name + " end idle");
wl.release(); wl.release();
@ -984,11 +985,7 @@ public class ServiceSynchronize extends LifecycleService {
// Receiver has a wake lock for ~10 seconds // Receiver has a wake lock for ~10 seconds
EntityLog.log(context, account.name + " keep alive"); EntityLog.log(context, account.name + " keep alive");
state.thread.interrupt(); state.thread.interrupt();
try {
// Give interrupted thread some time to acquire wake lock
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
yieldWakelock();
} }
}; };
@ -2017,7 +2014,7 @@ public class ServiceSynchronize extends LifecycleService {
EntityLog.log(ServiceSynchronize.this, "Main started"); EntityLog.log(ServiceSynchronize.this, "Main started");
try { try {
Thread.sleep(1000);
yieldWakelock();
wl.release(); wl.release();
Thread.sleep(Long.MAX_VALUE); Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
@ -2050,11 +2047,7 @@ public class ServiceSynchronize extends LifecycleService {
}, "sync.main"); }, "sync.main");
state.thread.setPriority(THREAD_PRIORITY_BACKGROUND); // will be inherited state.thread.setPriority(THREAD_PRIORITY_BACKGROUND); // will be inherited
state.thread.start(); state.thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
yieldWakelock();
} }
private void stop() { private void stop() {
@ -2140,6 +2133,14 @@ public class ServiceSynchronize extends LifecycleService {
} }
} }
private void yieldWakelock() {
try {
// Give interrupted thread some time to acquire wake lock
Thread.sleep(500L);
} catch (InterruptedException ignored) {
}
}
public static void start(Context context) { public static void start(Context context) {
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class)); ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class));
} }


Loading…
Cancel
Save