Reduce duplicate code

This commit is contained in:
AsamK 2016-03-27 23:34:59 +02:00
parent 083e33c4da
commit dd934f1304

View file

@ -573,73 +573,33 @@ public class Main {
} }
} }
private static class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler { private static class DbusReceiveMessageHandler extends ReceiveMessageHandler {
final Manager m;
final DBusConnection conn; final DBusConnection conn;
public DbusReceiveMessageHandler(Manager m, DBusConnection conn) { public DbusReceiveMessageHandler(Manager m, DBusConnection conn) {
this.m = m; super(m);
this.conn = conn; this.conn = conn;
} }
@Override @Override
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, GroupInfo group) { public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, GroupInfo group) {
System.out.println("Envelope from: " + envelope.getSource()); super.handleMessage(envelope, content, group);
System.out.println("Timestamp: " + envelope.getTimestamp());
if (envelope.isReceipt()) { if (!envelope.isReceipt() && content != null && content.getDataMessage().isPresent()) {
System.out.println("Got receipt.");
} else if (envelope.isSignalMessage() | envelope.isPreKeySignalMessage()) {
if (content == null) {
System.out.println("Failed to decrypt message.");
} else {
if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get(); SignalServiceDataMessage message = content.getDataMessage().get();
System.out.println("Message timestamp: " + message.getTimestamp()); if (!message.isEndSession() &&
!(message.getGroupInfo().isPresent() &&
if (message.getBody().isPresent()) { message.getGroupInfo().get().getType() != SignalServiceGroup.Type.DELIVER)) {
System.out.println("Body: " + message.getBody().get());
}
if (message.getGroupInfo().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupInfo().get();
System.out.println("Group info:");
System.out.println(" Id: " + Base64.encodeBytes(groupInfo.getGroupId()));
if (groupInfo.getName().isPresent()) {
System.out.println(" Name: " + groupInfo.getName().get());
} else if (group != null) {
System.out.println(" Name: " + group.name);
} else {
System.out.println(" Name: <Unknown group>");
}
System.out.println(" Type: " + groupInfo.getType());
if (groupInfo.getMembers().isPresent()) {
for (String member : groupInfo.getMembers().get()) {
System.out.println(" Member: " + member);
}
}
if (groupInfo.getAvatar().isPresent()) {
System.out.println(" Avatar:");
printAttachment(groupInfo.getAvatar().get());
}
}
if (message.isEndSession()) {
System.out.println("Is end session");
}
List<String> attachments = new ArrayList<>(); List<String> attachments = new ArrayList<>();
if (message.getAttachments().isPresent()) { if (message.getAttachments().isPresent()) {
System.out.println("Attachments: ");
for (SignalServiceAttachment attachment : message.getAttachments().get()) { for (SignalServiceAttachment attachment : message.getAttachments().get()) {
if (attachment.isPointer()) { if (attachment.isPointer()) {
attachments.add(m.getAttachmentFile(attachment.asPointer().getId()).getAbsolutePath()); attachments.add(m.getAttachmentFile(attachment.asPointer().getId()).getAbsolutePath());
} }
printAttachment(attachment);
} }
} }
if (!message.isEndSession() &&
!(message.getGroupInfo().isPresent() && message.getGroupInfo().get().getType() != SignalServiceGroup.Type.DELIVER)) {
try { try {
conn.sendSignal(new Signal.MessageReceived( conn.sendSignal(new Signal.MessageReceived(
SIGNAL_OBJECTPATH, SIGNAL_OBJECTPATH,
@ -652,15 +612,6 @@ public class Main {
} }
} }
} }
if (content.getSyncMessage().isPresent()) {
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
System.out.println("Received sync message");
}
}
} else {
System.out.println("Unknown message received.");
}
System.out.println();
} }
private void printAttachment(SignalServiceAttachment attachment) { private void printAttachment(SignalServiceAttachment attachment) {