Commit 5aec9533 authored by akwizgran's avatar akwizgran

Use RoboGuice for injection into Android activities.

parent 8279cd14
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="roboguice_modules">
<item>net.sf.briar.HelloWorldModule</item>
<item>net.sf.briar.android.AndroidModule</item>
<item>net.sf.briar.android.invitation.AndroidInvitationModule</item>
<item>net.sf.briar.clock.ClockModule</item>
<item>net.sf.briar.crypto.CryptoModule</item>
<item>net.sf.briar.db.DatabaseModule</item>
<item>net.sf.briar.lifecycle.LifecycleModule</item>
<item>net.sf.briar.plugins.PluginsModule</item>
<item>net.sf.briar.protocol.ProtocolModule</item>
<item>net.sf.briar.protocol.duplex.DuplexProtocolModule</item>
<item>net.sf.briar.protocol.simplex.SimplexProtocolModule</item>
<item>net.sf.briar.serial.SerialModule</item>
<item>net.sf.briar.transport.TransportModule</item>
</string-array>
</resources>
\ No newline at end of file
package net.sf.briar;
import static android.content.Context.MODE_PRIVATE;
import java.io.File;
import net.sf.briar.api.crypto.Password;
import net.sf.briar.api.db.DatabaseConfig;
import net.sf.briar.api.ui.UiCallback;
import android.app.Application;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
public class HelloWorldModule extends AbstractModule {
private final DatabaseConfig config;
private final UiCallback callback;
@Override
protected void configure() {
bind(UiCallback.class).toInstance(new UiCallback() {
public HelloWorldModule(final File dir) {
final Password password = new Password() {
public int showChoice(String[] options, String... message) {
return -1;
}
public char[] getPassword() {
return "foo bar".toCharArray();
public boolean showConfirmationMessage(String... message) {
return false;
}
};
config = new DatabaseConfig() {
public void showMessage(String... message) {}
});
}
@Provides @Singleton
DatabaseConfig getDatabaseConfig(final Application app) {
return new DatabaseConfig() {
public File getDataDirectory() {
return dir;
return app.getApplicationContext().getDir("db", MODE_PRIVATE);
}
public Password getPassword() {
return password;
return new Password() {
public char[] getPassword() {
return "foo bar".toCharArray();
}
};
}
public long getMaxSize() {
return Long.MAX_VALUE;
}
};
callback = new UiCallback() {
public int showChoice(String[] options, String... message) {
return -1;
}
public boolean showConfirmationMessage(String... message) {
return false;
}
public void showMessage(String... message) {}
};
}
@Override
protected void configure() {
bind(DatabaseConfig.class).toInstance(config);
bind(UiCallback.class).toInstance(callback);
}
}
......@@ -3,40 +3,27 @@ package net.sf.briar;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import net.sf.briar.android.AndroidModule;
import net.sf.briar.api.crypto.KeyManager;
import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.plugins.PluginManager;
import net.sf.briar.clock.ClockModule;
import net.sf.briar.crypto.CryptoModule;
import net.sf.briar.db.DatabaseModule;
import net.sf.briar.lifecycle.LifecycleModule;
import net.sf.briar.plugins.PluginsModule;
import net.sf.briar.protocol.ProtocolModule;
import net.sf.briar.protocol.duplex.DuplexProtocolModule;
import net.sf.briar.protocol.simplex.SimplexProtocolModule;
import net.sf.briar.serial.SerialModule;
import net.sf.briar.transport.TransportModule;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Inject;
public class HelloWorldService extends Service implements Runnable {
private static final Logger LOG =
Logger.getLogger(HelloWorldService.class.getName());
private DatabaseComponent db = null;
private KeyManager keyManager = null;
private PluginManager pluginManager = null;
@Inject private DatabaseComponent db;
@Inject private KeyManager keyManager;
@Inject private PluginManager pluginManager;
@Override
public void onCreate() {
......@@ -56,16 +43,6 @@ public class HelloWorldService extends Service implements Runnable {
}
public void run() {
File dir = getApplicationContext().getDir("db", MODE_PRIVATE);
Injector i = Guice.createInjector(new HelloWorldModule(dir),
new AndroidModule(), new ClockModule(), new CryptoModule(),
new DatabaseModule(), new LifecycleModule(),
new PluginsModule(), new ProtocolModule(),
new DuplexProtocolModule(), new SimplexProtocolModule(),
new SerialModule(), new TransportModule());
db = i.getInstance(DatabaseComponent.class);
keyManager = i.getInstance(KeyManager.class);
pluginManager = i.getInstance(PluginManager.class);
try {
// Start...
if(LOG.isLoggable(INFO)) LOG.info("Starting");
......
package net.sf.briar.android.invitation;
import javax.inject.Singleton;
import com.google.inject.AbstractModule;
public class AndroidInvitationModule extends AbstractModule {
@Override
protected void configure() {
bind(InvitationManager.class).to(InvitationManagerImpl.class).in(
Singleton.class);
}
}
......@@ -6,7 +6,7 @@ import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
import net.sf.briar.R;
import android.app.Activity;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
......@@ -15,9 +15,13 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ConfirmationCodeActivity extends Activity
import com.google.inject.Inject;
public class ConfirmationCodeActivity extends RoboActivity
implements CodeEntryListener {
@Inject private InvitationManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -49,8 +53,7 @@ implements CodeEntryListener {
TextView code = new TextView(this);
code.setGravity(CENTER_HORIZONTAL);
code.setTextSize(50);
InvitationManager im = InvitationManagerFactory.getInvitationManager();
code.setText(im.getLocalConfirmationCode());
code.setText(manager.getLocalConfirmationCode());
layout.addView(code);
CodeEntryWidget codeEntry = new CodeEntryWidget(this);
......@@ -62,9 +65,7 @@ implements CodeEntryListener {
}
public void codeEntered(String code) {
InvitationManager im = InvitationManagerFactory.getInvitationManager();
String remoteConfirmationCode = im.getRemoteConfirmationCode();
if(code.equals(String.valueOf(remoteConfirmationCode))) {
if(code.equals(manager.getRemoteConfirmationCode())) {
Intent intent = new Intent(this, WaitForContactActivity.class);
intent.putExtras(getIntent().getExtras());
startActivity(intent);
......
......@@ -6,7 +6,7 @@ import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
import net.sf.briar.R;
import android.app.Activity;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
......@@ -15,10 +15,12 @@ import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ConnectionActivity extends Activity implements ConnectionListener {
import com.google.inject.Inject;
private final InvitationManager manager =
InvitationManagerFactory.getInvitationManager();
public class ConnectionActivity extends RoboActivity
implements ConnectionListener {
@Inject private InvitationManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
......
......@@ -4,7 +4,7 @@ import static android.view.Gravity.CENTER_HORIZONTAL;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.widget.LinearLayout.VERTICAL;
import net.sf.briar.R;
import android.app.Activity;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
......@@ -12,11 +12,12 @@ import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class InvitationCodeActivity extends Activity
import com.google.inject.Inject;
public class InvitationCodeActivity extends RoboActivity
implements CodeEntryListener {
private final InvitationManager manager =
InvitationManagerFactory.getInvitationManager();
@Inject private InvitationManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
......
package net.sf.briar.android.invitation;
class InvitationManagerFactory {
private static final Object LOCK = new Object();
private static InvitationManager instance = null; // Locking: lock
static InvitationManager getInvitationManager() {
synchronized(LOCK) {
if(instance == null) instance = new InvitationManagerImpl();
return instance;
}
}
}
......@@ -6,7 +6,7 @@ import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
import net.sf.briar.R;
import android.app.Activity;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
......@@ -15,9 +15,13 @@ import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
public class WaitForContactActivity extends Activity
import com.google.inject.Inject;
public class WaitForContactActivity extends RoboActivity
implements ConfirmationListener {
@Inject private InvitationManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -49,8 +53,7 @@ implements ConfirmationListener {
TextView code = new TextView(this);
code.setGravity(CENTER_HORIZONTAL);
code.setTextSize(50);
InvitationManager im = InvitationManagerFactory.getInvitationManager();
code.setText(im.getLocalConfirmationCode());
code.setText(manager.getLocalConfirmationCode());
layout.addView(code);
innerLayout = new LinearLayout(this);
......@@ -69,7 +72,7 @@ implements ConfirmationListener {
setContentView(layout);
im.startConfirmationWorker(this);
manager.startConfirmationWorker(this);
}
public void confirmationReceived() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment