Added wakelock / account monitor

pull/194/merge
M66B 3 years ago
parent 4e05fcf956
commit 3086464cbd

@ -232,6 +232,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private int lastQuitId = -1; private int lastQuitId = -1;
private List<Long> initialized = new ArrayList<>(); private List<Long> initialized = new ArrayList<>();
private List<TupleAccountNetworkState> accountStates = new ArrayList<>(); private List<TupleAccountNetworkState> accountStates = new ArrayList<>();
private PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
private PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":service");
private ExecutorService queue = Helper.getBackgroundExecutor(1, "service"); private ExecutorService queue = Helper.getBackgroundExecutor(1, "service");
@Override @Override
@ -418,30 +421,36 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
queue.submit(new Runnable() { queue.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Scheduling,
"### init " + accountNetworkState);
DB db = DB.getInstance(ServiceSynchronize.this);
try { try {
db.beginTransaction(); wl.acquire();
db.account().setAccountState(accountNetworkState.accountState.id, null); EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Scheduling,
db.account().setAccountBackoff(accountNetworkState.accountState.id, null); "### init " + accountNetworkState);
for (EntityFolder folder : db.folder().getFolders(accountNetworkState.accountState.id, false, false)) { DB db = DB.getInstance(ServiceSynchronize.this);
db.folder().setFolderState(folder.id, null); try {
if (db.operation().getOperationCount(folder.id, EntityOperation.SYNC) == 0) db.beginTransaction();
db.folder().setFolderSyncState(folder.id, null);
db.folder().setFolderPollCount(folder.id, 0);
}
db.operation().resetOperationStates(accountNetworkState.accountState.id); db.account().setAccountState(accountNetworkState.accountState.id, null);
db.account().setAccountBackoff(accountNetworkState.accountState.id, null);
db.setTransactionSuccessful(); for (EntityFolder folder : db.folder().getFolders(accountNetworkState.accountState.id, false, false)) {
} catch (Throwable ex) { db.folder().setFolderState(folder.id, null);
Log.e(ex); if (db.operation().getOperationCount(folder.id, EntityOperation.SYNC) == 0)
db.folder().setFolderSyncState(folder.id, null);
db.folder().setFolderPollCount(folder.id, 0);
}
db.operation().resetOperationStates(accountNetworkState.accountState.id);
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(ex);
} finally {
db.endTransaction();
}
} finally { } finally {
db.endTransaction(); wl.release();
} }
} }
}); });
@ -468,6 +477,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
@Override @Override
public void run() { public void run() {
try { try {
wl.acquire();
Map<String, String> crumb = new HashMap<>(); Map<String, String> crumb = new HashMap<>();
crumb.put("account", accountNetworkState.accountState.id.toString()); crumb.put("account", accountNetworkState.accountState.id.toString());
crumb.put("connected", Boolean.toString(accountNetworkState.networkState.isConnected())); crumb.put("connected", Boolean.toString(accountNetworkState.networkState.isConnected()));
@ -483,6 +494,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
"### started=" + accountNetworkState); "### started=" + accountNetworkState);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally {
wl.release();
} }
} }
}); });
@ -501,6 +514,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
@Override @Override
public void run() { public void run() {
try { try {
wl.acquire();
Map<String, String> crumb = new HashMap<>(); Map<String, String> crumb = new HashMap<>();
crumb.put("account", accountNetworkState.accountState.id.toString()); crumb.put("account", accountNetworkState.accountState.id.toString());
crumb.put("connected", Boolean.toString(accountNetworkState.networkState.isConnected())); crumb.put("connected", Boolean.toString(accountNetworkState.networkState.isConnected()));
@ -518,6 +533,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
"### stopped=" + accountNetworkState); "### stopped=" + accountNetworkState);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally {
wl.release();
} }
} }
}); });
@ -531,6 +548,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
@Override @Override
public void run() { public void run() {
try { try {
wl.acquire();
DB db = DB.getInstance(ServiceSynchronize.this); DB db = DB.getInstance(ServiceSynchronize.this);
db.account().deleteAccount(accountNetworkState.accountState.id); db.account().deleteAccount(accountNetworkState.accountState.id);
@ -540,6 +559,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally {
wl.release();
} }
} }
}); });
@ -550,6 +571,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
@Override @Override
public void run() { public void run() {
try { try {
wl.acquire();
EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Scheduling, EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Scheduling,
"### quit eventId=" + eventId); "### quit eventId=" + eventId);
@ -579,14 +602,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
// Stop service // Stop service
EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Scheduling,
"### stopping self eventId=" + eventId);
stopSelf(); stopSelf();
EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Scheduling, EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Scheduling,
"### stop self eventId=" + eventId); "### stopped self eventId=" + eventId);
WorkerCleanup.cleanupConditionally(ServiceSynchronize.this); WorkerCleanup.cleanupConditionally(ServiceSynchronize.this);
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally {
wl.release();
} }
} }
}); });
@ -599,9 +626,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
@Override @Override
public void run() { public void run() {
try { try {
wl.acquire();
MessageClassifier.save(ServiceSynchronize.this); MessageClassifier.save(ServiceSynchronize.this);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally {
wl.release();
} }
} }
}); });

Loading…
Cancel
Save