Moving Bitmessage context into a foreground service (work in progress)
This commit is contained in:
@ -2,6 +2,7 @@ package ch.dissem.apps.abit.service;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import ch.dissem.apps.abit.MessageListActivity;
|
||||
import ch.dissem.apps.abit.listener.MessageListener;
|
||||
import ch.dissem.apps.abit.repository.AndroidAddressRepository;
|
||||
import ch.dissem.apps.abit.repository.AndroidInventory;
|
||||
@ -9,35 +10,24 @@ import ch.dissem.apps.abit.repository.AndroidMessageRepository;
|
||||
import ch.dissem.apps.abit.repository.SqlHelper;
|
||||
import ch.dissem.bitmessage.BitmessageContext;
|
||||
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
|
||||
import ch.dissem.bitmessage.ports.AddressRepository;
|
||||
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
||||
import ch.dissem.bitmessage.ports.MessageRepository;
|
||||
import ch.dissem.bitmessage.ports.Security;
|
||||
import ch.dissem.bitmessage.security.sc.SpongySecurity;
|
||||
|
||||
/**
|
||||
* Provides singleton objects across the application.
|
||||
*/
|
||||
public class Singleton {
|
||||
private static BitmessageContext bitmessageContext;
|
||||
private static SqlHelper sqlHelper;
|
||||
private static Security security;
|
||||
private static MessageRepository messageRepository;
|
||||
private static MessageListener messageListener;
|
||||
private static AddressRepository addressRepository;
|
||||
|
||||
public static BitmessageContext getBitmessageContext(Context context) {
|
||||
if (bitmessageContext == null) {
|
||||
synchronized (Singleton.class) {
|
||||
if (bitmessageContext == null) {
|
||||
final Context ctx = context.getApplicationContext();
|
||||
SqlHelper sqlHelper = new SqlHelper(ctx);
|
||||
bitmessageContext = new BitmessageContext.Builder()
|
||||
.security(new SpongySecurity())
|
||||
.nodeRegistry(new MemoryNodeRegistry())
|
||||
.inventory(new AndroidInventory(sqlHelper))
|
||||
.addressRepo(new AndroidAddressRepository(sqlHelper))
|
||||
.messageRepo(new AndroidMessageRepository(sqlHelper, ctx))
|
||||
.networkHandler(new DefaultNetworkHandler())
|
||||
.listener(getMessageListener(ctx))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
return bitmessageContext;
|
||||
static {
|
||||
ch.dissem.bitmessage.utils.Singleton.initialize(new SpongySecurity());
|
||||
}
|
||||
|
||||
public static MessageListener getMessageListener(Context ctx) {
|
||||
@ -50,4 +40,41 @@ public class Singleton {
|
||||
}
|
||||
return messageListener;
|
||||
}
|
||||
|
||||
public static SqlHelper getSqlHelper(Context ctx) {
|
||||
if (sqlHelper == null) {
|
||||
synchronized (Singleton.class) {
|
||||
if (sqlHelper == null) {
|
||||
sqlHelper = new SqlHelper(ctx.getApplicationContext());
|
||||
}
|
||||
}
|
||||
}
|
||||
return sqlHelper;
|
||||
}
|
||||
|
||||
public static MessageRepository getMessageRepository(Context ctx) {
|
||||
if (messageRepository == null) {
|
||||
ctx = ctx.getApplicationContext();
|
||||
getSqlHelper(ctx);
|
||||
synchronized (Singleton.class) {
|
||||
if (messageRepository == null) {
|
||||
messageRepository = new AndroidMessageRepository(sqlHelper, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
return messageRepository;
|
||||
}
|
||||
|
||||
public static AddressRepository getAddressRepository(Context ctx) {
|
||||
if (addressRepository == null) {
|
||||
ctx = ctx.getApplicationContext();
|
||||
getSqlHelper(ctx);
|
||||
synchronized (Singleton.class) {
|
||||
if (addressRepository == null) {
|
||||
addressRepository = new AndroidAddressRepository(sqlHelper);
|
||||
}
|
||||
}
|
||||
}
|
||||
return addressRepository;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user