Fixed the 'full node' switch, activated the jack tools to support some Java 8 features, and fixed some lint issues

This commit is contained in:
2016-10-16 23:16:38 +02:00
parent a5b3c33394
commit 2b1fb436a9
30 changed files with 367 additions and 436 deletions

View File

@ -21,11 +21,9 @@ import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.dissem.apps.abit.notification.NetworkNotification;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.utils.Property;
import static ch.dissem.apps.abit.notification.NetworkNotification.ONGOING_NOTIFICATION_ID;
@ -35,12 +33,7 @@ import static ch.dissem.apps.abit.notification.NetworkNotification.ONGOING_NOTIF
* onPerformSync().
*/
public class BitmessageService extends Service {
public static final Logger LOG = LoggerFactory.getLogger(BitmessageService.class);
// Object to use as a thread-safe lock
private static final Object lock = new Object();
private static NetworkNotification notification = null;
private NetworkNotification notification = null;
private static BitmessageContext bmc = null;
private static volatile boolean running = false;
@ -51,11 +44,11 @@ public class BitmessageService extends Service {
@Override
public void onCreate() {
synchronized (lock) {
synchronized (BitmessageService.class) {
if (bmc == null) {
bmc = Singleton.getBitmessageContext(this);
notification = new NetworkNotification(this, bmc);
}
notification = new NetworkNotification(this);
}
}
@ -70,7 +63,6 @@ public class BitmessageService extends Service {
running = false;
}
/**
* Return an object that allows the system to invoke
* the sync adapter.
@ -84,6 +76,7 @@ public class BitmessageService extends Service {
public void startupNode() {
startService(new Intent(BitmessageService.this, BitmessageService.class));
running = true;
notification.connecting();
startForeground(ONGOING_NOTIFICATION_ID, notification.getNotification());
if (!bmc.isRunning()) {
bmc.startup();
@ -96,8 +89,17 @@ public class BitmessageService extends Service {
bmc.shutdown();
}
running = false;
stopForeground(false);
stopForeground(true);
notification.show();
stopSelf();
}
}
}
public static Property getStatus() {
if (bmc != null) {
return bmc.status();
} else {
return new Property("bitmessage context", null);
}
}
}

View File

@ -40,7 +40,7 @@ public class ProofOfWorkService extends Service {
private static ProofOfWorkEngine engine = new MultiThreadedPOWEngine();
private static boolean calculating;
private static final Queue<PowItem> queue = new LinkedList<>();
private static ProofOfWorkNotification notification;
private ProofOfWorkNotification notification;
@Override
public void onCreate() {
@ -55,16 +55,18 @@ public class ProofOfWorkService extends Service {
public static class PowBinder extends Binder {
private final ProofOfWorkService service;
private final ProofOfWorkNotification notification;
private PowBinder(ProofOfWorkService service) {
this.service = service;
this.notification = service.notification;
}
public void process(PowItem item) {
void process(PowItem item) {
synchronized (queue) {
service.startService(new Intent(service, ProofOfWorkService.class));
service.startForeground(ONGOING_NOTIFICATION_ID,
notification.getNotification());
notification.getNotification());
if (!calculating) {
calculating = true;
service.calculateNonce(item);
@ -90,28 +92,25 @@ public class ProofOfWorkService extends Service {
}
private void calculateNonce(final PowItem item) {
engine.calculateNonce(item.initialHash, item.targetValue, new ProofOfWorkEngine.Callback() {
@Override
public void onNonceCalculated(byte[] initialHash, byte[] nonce) {
try {
item.callback.onNonceCalculated(initialHash, nonce);
} finally {
PowItem item;
synchronized (queue) {
item = queue.poll();
if (item == null) {
calculating = false;
stopForeground(true);
stopSelf();
} else {
notification.update(queue.size()).show();
}
}
if (item != null) {
calculateNonce(item);
engine.calculateNonce(item.initialHash, item.targetValue, (initialHash, nonce) -> {
try {
item.callback.onNonceCalculated(initialHash, nonce);
} finally {
PowItem next;
synchronized (queue) {
next = queue.poll();
if (next == null) {
calculating = false;
stopForeground(true);
stopSelf();
} else {
notification.update(queue.size()).show();
}
}
if (next != null) {
calculateNonce(next);
}
}
});
}
}
}

View File

@ -46,7 +46,6 @@ import static ch.dissem.bitmessage.utils.UnixTime.DAY;
* Provides singleton objects across the application.
*/
public class Singleton {
public static final Object lock = new Object();
private static BitmessageContext bitmessageContext;
private static MessageListener messageListener;
private static BitmessageAddress identity;
@ -54,28 +53,28 @@ public class Singleton {
public static BitmessageContext getBitmessageContext(Context context) {
if (bitmessageContext == null) {
synchronized (lock) {
synchronized (Singleton.class) {
if (bitmessageContext == null) {
final Context ctx = context.getApplicationContext();
SqlHelper sqlHelper = new SqlHelper(ctx);
powRepo = new AndroidProofOfWorkRepository(sqlHelper);
TTL.pubkey(2 * DAY);
bitmessageContext = new BitmessageContext.Builder()
.proofOfWorkEngine(new SwitchingProofOfWorkEngine(
ctx, Constants.PREFERENCE_SERVER_POW,
new ServerPowEngine(ctx),
new ServicePowEngine(ctx)
))
.cryptography(new AndroidCryptography())
.nodeRegistry(new AndroidNodeRegistry(sqlHelper))
.inventory(new AndroidInventory(sqlHelper))
.addressRepo(new AndroidAddressRepository(sqlHelper))
.messageRepo(new AndroidMessageRepository(sqlHelper, ctx))
.powRepo(powRepo)
.networkHandler(new NioNetworkHandler())
.listener(getMessageListener(ctx))
.doNotSendPubkeyOnIdentityCreation()
.build();
.proofOfWorkEngine(new SwitchingProofOfWorkEngine(
ctx, Constants.PREFERENCE_SERVER_POW,
new ServerPowEngine(ctx),
new ServicePowEngine(ctx)
))
.cryptography(new AndroidCryptography())
.nodeRegistry(new AndroidNodeRegistry(sqlHelper))
.inventory(new AndroidInventory(sqlHelper))
.addressRepo(new AndroidAddressRepository(sqlHelper))
.messageRepo(new AndroidMessageRepository(sqlHelper, ctx))
.powRepo(powRepo)
.networkHandler(new NioNetworkHandler())
.listener(getMessageListener(ctx))
.doNotSendPubkeyOnIdentityCreation()
.build();
}
}
}
@ -108,11 +107,12 @@ public class Singleton {
public static BitmessageAddress getIdentity(Context ctx) {
if (identity == null) {
BitmessageContext bmc = getBitmessageContext(ctx);
synchronized (Singleton.class) {
if (identity == null) {
BitmessageContext bmc = getBitmessageContext(ctx);
// FIXME: this may block the UI, there must be a better way!
List<BitmessageAddress> identities = bmc.addresses()
.getIdentities();
.getIdentities();
if (identities.size() > 0) {
identity = identities.get(0);
} else {