Skip to content
Snippets Groups Projects
BriarControllerImpl.java 2.33 KiB
Newer Older
package org.briarproject.android.controller;

import android.app.Activity;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.CallSuper;

import org.briarproject.android.BriarService;
import org.briarproject.android.BriarService.BriarServiceConnection;
Ernir Erlingsson's avatar
Ernir Erlingsson committed
import org.briarproject.android.controller.handler.ResultHandler;
import org.briarproject.api.db.DatabaseConfig;

import java.util.logging.Logger;

import javax.inject.Inject;

public class BriarControllerImpl implements BriarController {
			Logger.getLogger(BriarControllerImpl.class.getName());

	@Inject
	protected BriarServiceConnection serviceConnection;
	@Inject
	protected DatabaseConfig databaseConfig;
akwizgran's avatar
akwizgran committed
	@Inject
	protected Activity activity;

	public BriarControllerImpl() {

	}

	@Override
	@CallSuper
	public void onActivityCreate() {
		if (databaseConfig.getEncryptionKey() != null) startAndBindService();
	}

	@Override
	@CallSuper
	public void onActivityResume() {
	}

	@Override
	@CallSuper
	public void onActivityPause() {
	}

	@Override
	@CallSuper
	public void onActivityDestroy() {
		unbindService();
	}

akwizgran's avatar
akwizgran committed
	@Override
	public void startAndBindService() {
		activity.startService(new Intent(activity, BriarService.class));
		bound = activity.bindService(new Intent(activity, BriarService.class),
				serviceConnection, 0);
		LOG.info("Briar service started " + bound);
	public boolean hasEncryptionKey() {
		return databaseConfig.getEncryptionKey() != null;
	}

	@Override
Ernir Erlingsson's avatar
Ernir Erlingsson committed
	public void signOut(final ResultHandler<Void> eventHandler) {
		new Thread() {
			@Override
			public void run() {
				try {
					// Wait for the service to finish starting up
					IBinder binder = serviceConnection.waitForBinder();
akwizgran's avatar
akwizgran committed
					BriarService service =
							((BriarService.BriarBinder) binder).getService();
					service.waitForStartup();
					// Shut down the service and wait for it to shut down
					LOG.info("Shutting down service");
					service.shutdown();
					service.waitForShutdown();
				} catch (InterruptedException e) {
					LOG.warning("Interrupted while waiting for service");
				}
Ernir Erlingsson's avatar
Ernir Erlingsson committed
				eventHandler.onResult(null);
	protected void unbindService() {
		LOG.info("Briar service unbind " + bound);
		if (bound) activity.unbindService(serviceConnection);
	}

}