Skip to content
Snippets Groups Projects
Commit 37e68d5e authored by akwizgran's avatar akwizgran
Browse files

Register the DatabaseUiExecutor for shutdown by the LifecycleManager.

See issue #3612607.
parent 1692e5a6
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@ import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.android.ReferenceManager;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.lifecycle.LifecycleManager;
import net.sf.briar.api.lifecycle.ShutdownManager;
import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.duplex.DuplexPluginConfig;
......@@ -34,22 +35,29 @@ import com.google.inject.Singleton;
public class AndroidModule extends AbstractModule {
protected void configure() {
bind(AndroidExecutor.class).to(AndroidExecutorImpl.class);
bind(ReferenceManager.class).to(ReferenceManagerImpl.class).in(
Singleton.class);
private final ExecutorService databaseUiExecutor;
public AndroidModule() {
// The queue is unbounded, so tasks can be dependent
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
// Discard tasks that are submitted during shutdown
RejectedExecutionHandler policy =
new ThreadPoolExecutor.DiscardPolicy();
// Use a single thread so DB accesses from the UI don't overlap
ExecutorService e = new ThreadPoolExecutor(1, 1, 60, SECONDS, queue,
databaseUiExecutor = new ThreadPoolExecutor(1, 1, 60, SECONDS, queue,
policy);
bind(Executor.class).annotatedWith(
DatabaseUiExecutor.class).toInstance(e);
bind(ExecutorService.class).annotatedWith(
DatabaseUiExecutor.class).toInstance(e);
}
protected void configure() {
bind(AndroidExecutor.class).to(AndroidExecutorImpl.class);
bind(ReferenceManager.class).to(ReferenceManagerImpl.class).in(
Singleton.class);
}
@Provides @Singleton @DatabaseUiExecutor
Executor getDatabaseUiExecutor(LifecycleManager lifecycleManager) {
lifecycleManager.registerForShutdown(databaseUiExecutor);
return databaseUiExecutor;
}
@Provides
......
......@@ -5,12 +5,10 @@ import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static java.util.logging.Level.INFO;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;
import net.sf.briar.R;
import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.lifecycle.LifecycleManager;
import roboguice.service.RoboService;
import android.app.PendingIntent;
......@@ -33,7 +31,6 @@ public class BriarService extends RoboService {
// Fields that are accessed from background threads must be volatile
@Inject private volatile LifecycleManager lifecycleManager;
@Inject private volatile AndroidExecutor androidExecutor;
@Inject @DatabaseUiExecutor private volatile ExecutorService dbUiExecutor;
@Override
public void onCreate() {
......@@ -81,10 +78,7 @@ public class BriarService extends RoboService {
new Thread() {
@Override
public void run() {
// FIXME: This is ugly - executors should register themselves
// with the lifecycle manager
androidExecutor.shutdown();
dbUiExecutor.shutdown();
lifecycleManager.stopServices();
}
}.start();
......
package net.sf.briar.api.android;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
......@@ -13,6 +14,6 @@ import com.google.inject.BindingAnnotation;
* Annotation for injecting the executor for accessing the database from the UI.
*/
@BindingAnnotation
@Target({ FIELD, PARAMETER })
@Target({ FIELD, METHOD, PARAMETER })
@Retention(RUNTIME)
public @interface DatabaseUiExecutor {}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment