Code cleanup - most notably BitmessageService was cleaned up and doesn't use messaging anymore

This commit is contained in:
2016-01-19 20:50:58 +01:00
parent 6f6a2e4e6c
commit c4d76fc8ce
58 changed files with 661 additions and 468 deletions

View File

@ -1,26 +1,31 @@
/*
* Copyright 2016 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.apps.abit.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Binder;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import ch.dissem.apps.abit.R;
import ch.dissem.apps.abit.notification.NetworkNotification;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import static ch.dissem.apps.abit.notification.NetworkNotification.ONGOING_NOTIFICATION_ID;
@ -32,19 +37,6 @@ import static ch.dissem.apps.abit.notification.NetworkNotification.ONGOING_NOTIF
public class BitmessageService extends Service {
public static final Logger LOG = LoggerFactory.getLogger(BitmessageService.class);
public static final int MSG_CREATE_IDENTITY = 10;
public static final int MSG_SUBSCRIBE = 20;
public static final int MSG_ADD_CONTACT = 21;
public static final int MSG_SEND_MESSAGE = 30;
public static final int MSG_SEND_BROADCAST = 31;
public static final int MSG_START_NODE = 100;
public static final int MSG_STOP_NODE = 101;
public static final String DATA_FIELD_IDENTITY = "identity";
public static final String DATA_FIELD_ADDRESS = "address";
public static final String DATA_FIELD_SUBJECT = "subject";
public static final String DATA_FIELD_MESSAGE = "message";
// Object to use as a thread-safe lock
private static final Object lock = new Object();
@ -53,8 +45,6 @@ public class BitmessageService extends Service {
private static volatile boolean running = false;
private static Messenger messenger;
public static boolean isRunning() {
return running && bmc.isRunning();
}
@ -65,7 +55,6 @@ public class BitmessageService extends Service {
if (bmc == null) {
bmc = Singleton.getBitmessageContext(this);
notification = new NetworkNotification(this, bmc);
messenger = new Messenger(new IncomingHandler(this));
}
}
}
@ -81,101 +70,34 @@ public class BitmessageService extends Service {
running = false;
}
/**
* Return an object that allows the system to invoke
* the sync adapter.
*/
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
return new BitmessageBinder();
}
private static class IncomingHandler extends Handler {
private WeakReference<BitmessageService> service;
private IncomingHandler(BitmessageService service) {
this.service = new WeakReference<>(service);
public class BitmessageBinder extends Binder {
public void startupNode() {
startService(new Intent(BitmessageService.this, BitmessageService.class));
running = true;
startForeground(ONGOING_NOTIFICATION_ID, notification.getNotification());
if (!bmc.isRunning()) {
bmc.startup();
}
notification.show();
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_CREATE_IDENTITY: {
BitmessageAddress identity = bmc.createIdentity(false);
if (msg.replyTo != null) {
try {
Message message = Message.obtain(this, MSG_CREATE_IDENTITY);
Bundle bundle = new Bundle();
bundle.putSerializable(DATA_FIELD_IDENTITY, identity);
message.setData(bundle);
msg.replyTo.send(message);
} catch (RemoteException e) {
LOG.debug(e.getMessage(), e);
}
}
break;
}
case MSG_SUBSCRIBE: {
Serializable data = msg.getData().getSerializable(DATA_FIELD_ADDRESS);
if (data instanceof BitmessageAddress) {
bmc.addSubscribtion((BitmessageAddress) data);
}
break;
}
case MSG_ADD_CONTACT: {
Serializable data = msg.getData().getSerializable(DATA_FIELD_ADDRESS);
if (data instanceof BitmessageAddress) {
bmc.addContact((BitmessageAddress) data);
}
break;
}
case MSG_SEND_MESSAGE: {
Serializable identity = msg.getData().getSerializable(DATA_FIELD_IDENTITY);
Serializable address = msg.getData().getSerializable(DATA_FIELD_ADDRESS);
if (identity instanceof BitmessageAddress
&& address instanceof BitmessageAddress) {
String subject = msg.getData().getString(DATA_FIELD_SUBJECT);
String message = msg.getData().getString(DATA_FIELD_MESSAGE);
bmc.send((BitmessageAddress) identity, (BitmessageAddress) address,
subject, message);
} else {
Context ctx = service.get();
Toast.makeText(ctx, "Could not send", Toast.LENGTH_LONG);
}
break;
}
case MSG_SEND_BROADCAST: {
Serializable data = msg.getData().getSerializable(DATA_FIELD_IDENTITY);
if (data instanceof BitmessageAddress) {
String subject = msg.getData().getString(DATA_FIELD_SUBJECT);
String message = msg.getData().getString(DATA_FIELD_MESSAGE);
bmc.broadcast((BitmessageAddress) data, subject, message);
}
break;
}
case MSG_START_NODE:
// TODO: warn user, option to restrict to WiFi
// (I'm not quite sure this can be done here, though)
service.get().startService(new Intent(service.get(), BitmessageService.class));
running = true;
service.get().startForeground(ONGOING_NOTIFICATION_ID, notification
.getNotification());
if (!bmc.isRunning()) {
bmc.startup();
}
notification.show();
break;
case MSG_STOP_NODE:
if (bmc.isRunning()) {
bmc.shutdown();
}
running = false;
service.get().stopForeground(false);
service.get().stopSelf();
break;
default:
super.handleMessage(msg);
public void shutdownNode() {
if (bmc.isRunning()) {
bmc.shutdown();
}
running = false;
stopForeground(false);
stopSelf();
}
}
}

View File

@ -1,3 +1,19 @@
/*
* Copyright 2016 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.apps.abit.service;
import android.app.Service;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2016 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.apps.abit.service;
import android.content.ComponentName;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2016 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.apps.abit.service;
import android.content.Context;