From 7fd25abea2b8c9a95eeabb8c4de7233789e9a1f4 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 12 Apr 2018 08:55:50 +0200 Subject: [PATCH] code cleanup in AxolotlService.fetchDeviceIds() --- .../crypto/axolotl/AxolotlService.java | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java index 573d17e69..e8a47e447 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java @@ -978,7 +978,8 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { fetchDeviceIds(jid, null); } - public void fetchDeviceIds(final Jid jid, OnDeviceIdsFetched callback) { + private void fetchDeviceIds(final Jid jid, OnDeviceIdsFetched callback) { + IqPacket packet; synchronized (this.fetchDeviceIdsMap) { List callbacks = this.fetchDeviceIdsMap.get(jid); if (callbacks != null) { @@ -986,6 +987,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { callbacks.add(callback); } Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching device ids for " + jid + " already running. adding callback"); + packet = null; } else { callbacks = new ArrayList<>(); if (callback != null) { @@ -993,35 +995,37 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { } this.fetchDeviceIdsMap.put(jid, callbacks); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching device ids for " + jid); - IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(jid); - mXmppConnectionService.sendIqPacket(account, packet, (account, response) -> { - synchronized (fetchDeviceIdsMap) { - List callbacks1 = fetchDeviceIdsMap.remove(jid); - if (response.getType() == IqPacket.TYPE.RESULT) { - fetchDeviceListStatus.put(jid, true); - Element item = mXmppConnectionService.getIqParser().getItem(response); - Set deviceIds = mXmppConnectionService.getIqParser().deviceIds(item); - registerDevices(jid, deviceIds); - if (callbacks1 != null) { - for (OnDeviceIdsFetched callback1 : callbacks1) { - callback1.fetched(jid, deviceIds); - } + packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(jid); + } + } + if (packet != null) { + mXmppConnectionService.sendIqPacket(account, packet, (account, response) -> { + synchronized (fetchDeviceIdsMap) { + List callbacks = fetchDeviceIdsMap.remove(jid); + if (response.getType() == IqPacket.TYPE.RESULT) { + fetchDeviceListStatus.put(jid, true); + Element item = mXmppConnectionService.getIqParser().getItem(response); + Set deviceIds = mXmppConnectionService.getIqParser().deviceIds(item); + registerDevices(jid, deviceIds); + if (callbacks != null) { + for (OnDeviceIdsFetched c : callbacks) { + c.fetched(jid, deviceIds); } + } + } else { + if (response.getType() == IqPacket.TYPE.TIMEOUT) { + fetchDeviceListStatus.remove(jid); } else { - if (response.getType() == IqPacket.TYPE.TIMEOUT) { - fetchDeviceListStatus.remove(jid); - } else { - fetchDeviceListStatus.put(jid, false); - } - if (callbacks1 != null) { - for (OnDeviceIdsFetched callback1 : callbacks1) { - callback1.fetched(jid, null); - } + fetchDeviceListStatus.put(jid, false); + } + if (callbacks != null) { + for (OnDeviceIdsFetched c : callbacks) { + c.fetched(jid, null); } } } - }); - } + } + }); } }