mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Switched AppBus for ResultHandler, Controller for Helper. Added the basics for LifecycleControllers and implemented it for BriarActivity and NavDrawerActivity
This commit is contained in:
@@ -8,6 +8,8 @@ import android.os.IBinder;
|
||||
|
||||
import org.briarproject.android.BriarService.BriarBinder;
|
||||
import org.briarproject.android.BriarService.BriarServiceConnection;
|
||||
import org.briarproject.android.controller.BriarController;
|
||||
import org.briarproject.android.controller.ResultHandler;
|
||||
import org.briarproject.android.panic.ExitActivity;
|
||||
import org.briarproject.api.db.DatabaseConfig;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
@@ -37,12 +39,12 @@ public abstract class BriarActivity extends BaseActivity {
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BriarActivity.class.getName());
|
||||
|
||||
/*
|
||||
private final BriarServiceConnection serviceConnection =
|
||||
new BriarServiceConnection();
|
||||
|
||||
@Inject
|
||||
protected DatabaseConfig databaseConfig;
|
||||
|
||||
DatabaseConfig databaseConfig;
|
||||
private boolean bound = false;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@@ -51,19 +53,23 @@ public abstract class BriarActivity extends BaseActivity {
|
||||
protected volatile Executor dbExecutor;
|
||||
@Inject
|
||||
protected volatile LifecycleManager lifecycleManager;
|
||||
*/
|
||||
@Inject
|
||||
protected BriarController briarController;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
|
||||
if (databaseConfig.getEncryptionKey() != null) startAndBindService();
|
||||
briarController.startAndBindService();
|
||||
// if (databaseConfig.getEncryptionKey() != null) startAndBindService();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int request, int result, Intent data) {
|
||||
super.onActivityResult(request, result, data);
|
||||
if (request == REQUEST_PASSWORD) {
|
||||
if (result == RESULT_OK) startAndBindService();
|
||||
if (result == RESULT_OK) briarController.startAndBindService();
|
||||
else finish();
|
||||
}
|
||||
}
|
||||
@@ -71,7 +77,7 @@ public abstract class BriarActivity extends BaseActivity {
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (databaseConfig.getEncryptionKey() == null && !isFinishing()) {
|
||||
if (!briarController.encryptionKey() && !isFinishing()) {
|
||||
Intent i = new Intent(this, PasswordActivity.class);
|
||||
i.setFlags(FLAG_ACTIVITY_NO_ANIMATION | FLAG_ACTIVITY_SINGLE_TOP);
|
||||
startActivityForResult(i, REQUEST_PASSWORD);
|
||||
@@ -81,41 +87,22 @@ public abstract class BriarActivity extends BaseActivity {
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unbindService();
|
||||
}
|
||||
|
||||
private void startAndBindService() {
|
||||
startService(new Intent(this, BriarService.class));
|
||||
bound = bindService(new Intent(this, BriarService.class),
|
||||
serviceConnection, 0);
|
||||
}
|
||||
|
||||
private void unbindService() {
|
||||
if (bound) unbindService(serviceConnection);
|
||||
briarController.unbindService();
|
||||
}
|
||||
|
||||
protected void signOut(final boolean removeFromRecentApps) {
|
||||
// Use a new thread to avoid deadlock with executor tasks
|
||||
new Thread() {
|
||||
briarController.signOut(new ResultHandler<Void, RuntimeException>() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// Wait for the service to finish starting up
|
||||
IBinder binder = serviceConnection.waitForBinder();
|
||||
BriarService service = ((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");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
public void onResult(Void result) {
|
||||
if (removeFromRecentApps) startExitActivity();
|
||||
else finishAndExit();
|
||||
}
|
||||
}.start();
|
||||
|
||||
@Override
|
||||
public void onException(RuntimeException exception) {
|
||||
// TODO ?
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void signOut() {
|
||||
@@ -123,44 +110,25 @@ public abstract class BriarActivity extends BaseActivity {
|
||||
}
|
||||
|
||||
private void startExitActivity() {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Intent intent = new Intent(BriarActivity.this,
|
||||
ExitActivity.class);
|
||||
intent.addFlags(FLAG_ACTIVITY_NEW_TASK
|
||||
| FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
|
||||
| FLAG_ACTIVITY_NO_ANIMATION);
|
||||
if (Build.VERSION.SDK_INT >= 11)
|
||||
intent.addFlags(FLAG_ACTIVITY_CLEAR_TASK);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
Intent intent = new Intent(BriarActivity.this,
|
||||
ExitActivity.class);
|
||||
intent.addFlags(FLAG_ACTIVITY_NEW_TASK
|
||||
| FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
|
||||
| FLAG_ACTIVITY_NO_ANIMATION);
|
||||
if (Build.VERSION.SDK_INT >= 11)
|
||||
intent.addFlags(FLAG_ACTIVITY_CLEAR_TASK);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
private void finishAndExit() {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if (Build.VERSION.SDK_INT >= 21) finishAndRemoveTask();
|
||||
else finish();
|
||||
LOG.info("Exiting");
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
if (Build.VERSION.SDK_INT >= 21) finishAndRemoveTask();
|
||||
else finish();
|
||||
LOG.info("Exiting");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public void runOnDbThread(final Runnable task) {
|
||||
dbExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
lifecycleManager.waitForDatabase();
|
||||
task.run();
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warning("Interrupted while waiting for database");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
briarController.runOnDbThread(task);
|
||||
}
|
||||
|
||||
protected void finishOnUiThread() {
|
||||
|
||||
Reference in New Issue
Block a user