Yield wake lock

This commit is contained in:
M66B 2018-10-10 08:00:42 +00:00
parent 0d340d1865
commit 681a8a31e2
1 changed files with 27 additions and 26 deletions

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) {
try {
wl.acquire();
Log.i(Helper.TAG, account.name + " event: " + e.getMessage()); Log.i(Helper.TAG, account.name + " event: " + e.getMessage());
db.account().setAccountError(account.id, e.getMessage()); db.account().setAccountError(account.id, e.getMessage());
state.thread.interrupt(); 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 { yieldWakelock();
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
} 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 { yieldWakelock();
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
} 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 { yieldWakelock();
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
} 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 { yieldWakelock();
// Give interrupted thread some time to acquire wake lock
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
} }
}; };
@ -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();
yieldWakelock();
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
} }
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));
} }