Simplify operations watcher

pull/152/head
M66B 6 years ago
parent 4b7caeb219
commit 80c28636e6

@ -80,7 +80,6 @@ import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
@ -493,7 +492,7 @@ public class ServiceSynchronize extends LifecycleService {
final Map<EntityFolder, Folder> folders = new HashMap<>();
List<Thread> idlers = new ArrayList<>();
List<Handler> handlers = new ArrayList<>();
List<TwoStateOwner> owners = new ArrayList<>();
try {
// Listen for store events
istore.addStoreListener(new StoreListener() {
@ -815,29 +814,14 @@ public class ServiceSynchronize extends LifecycleService {
} else
folders.put(folder, null);
// Observe operations
Handler handler = new Handler(getMainLooper()) {
private List<Long> waiting = new ArrayList<>();
private List<Long> handling = new ArrayList<>();
private LiveData<List<EntityOperation>> liveOperations;
final TwoStateOwner owner = new TwoStateOwner();
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void handleMessage(android.os.Message msg) {
Log.i(account.name + "/" + folder.name + " observe=" + msg.what);
try {
if (msg.what == 0) {
liveOperations.removeObserver(observer);
handling.clear();
} else {
liveOperations = db.operation().liveOperations(folder.id);
liveOperations.observe(ServiceSynchronize.this, observer);
}
} catch (Throwable ex) {
Log.e(ex);
}
}
private Observer<List<EntityOperation>> observer = new Observer<List<EntityOperation>>() {
public void run() {
db.operation().liveOperations(folder.id).observe(owner, new Observer<List<EntityOperation>>() {
private List<Long> waiting = new ArrayList<>();
private List<Long> handling = new ArrayList<>();
private final ExecutorService folderExecutor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
private final PowerManager.WakeLock wlFolder = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":folder." + folder.id);
@ -927,12 +911,12 @@ public class ServiceSynchronize extends LifecycleService {
});
}
}
};
};
});
}
});
// Start watching for operations
handler.sendEmptyMessage(1);
handlers.add(handler);
owner.start();
owners.add(owner);
}
// Keep alive alarm receiver
@ -1014,9 +998,9 @@ public class ServiceSynchronize extends LifecycleService {
db.account().setAccountError(account.id, Helper.formatThrowable(ex));
} finally {
// Stop watching for operations
for (Handler handler : handlers)
handler.sendEmptyMessage(0);
handlers.clear();
for (TwoStateOwner owner : owners)
owner.stop();
owners.clear();
EntityLog.log(this, account.name + " closing");
db.account().setAccountState(account.id, "closing");

@ -0,0 +1,28 @@
package eu.faircode.email;
import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
public class TwoStateOwner implements LifecycleOwner {
private LifecycleRegistry registry;
TwoStateOwner() {
registry = new LifecycleRegistry(this);
}
void start() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_START);
}
void stop() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return registry;
}
}
Loading…
Cancel
Save