Change summary
src/main/java/eu/siacs/conversations/entities/Contact.java | 7
src/main/java/eu/siacs/conversations/entities/Conversation.java | 9
src/main/java/eu/siacs/conversations/entities/Presences.java | 12
src/main/java/eu/siacs/conversations/ui/ConversationFragment.java | 41
4 files changed, 45 insertions(+), 24 deletions(-)
Detailed changes
@@ -292,6 +292,13 @@ public class Contact implements ListItem, Blockable {
return this.presences.getShownStatus();
}
+ public Jid resourceWhichSupport(final String namespace) {
+ final String resource = getPresences().firstWhichSupport(namespace);
+ if (resource == null) return null;
+
+ return resource.equals("") ? getJid() : getJid().withResource(resource);
+ }
+
public boolean setPhotoUri(String uri) {
if (uri != null && !uri.equals(this.photoUri)) {
this.photoUri = uri;
@@ -30,6 +30,7 @@ import eu.siacs.conversations.services.QuickConversationsService;
import eu.siacs.conversations.utils.JidHelper;
import eu.siacs.conversations.utils.MessageUtils;
import eu.siacs.conversations.utils.UIHelper;
+import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.Jid;
import eu.siacs.conversations.xmpp.chatstate.ChatState;
import eu.siacs.conversations.xmpp.mam.MamReference;
@@ -1109,6 +1110,14 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
return getName().toString();
}
+ public int getCurrentTab() {
+ if (!isRead() || getContact().resourceWhichSupport(Namespace.COMMANDS) == null) {
+ return 0;
+ }
+
+ return 1;
+ }
+
public interface OnMessageFound {
void onMessageFound(final Message message);
}
@@ -149,6 +149,18 @@ public class Presences {
return false;
}
+ public String firstWhichSupport(final String namespace) {
+ for (Map.Entry<String, Presence> entry : this.presences.entrySet()) {
+ String resource = entry.getKey();
+ Presence presence = entry.getValue();
+ if (presence.getServiceDiscoveryResult().getFeatures().contains(namespace)) {
+ return resource;
+ }
+ }
+
+ return null;
+ }
+
public boolean anyIdentity(final String category, final String type) {
synchronized (this.presences) {
if (this.presences.size() == 0) {
@@ -1247,33 +1247,26 @@ public class ConversationFragment extends XmppFragment
binding.conversationViewPager
));
binding.tabLayout.setupWithViewPager(binding.conversationViewPager);
+ binding.conversationViewPager.setCurrentItem(conversation.getCurrentTab());
commandAdapter = new CommandAdapter((XmppActivity) getActivity());
binding.commandsView.setAdapter(commandAdapter);
- Presences presences = conversation.getContact().getPresences();
- for (Map.Entry<String, Presence> entry : presences.getPresencesMap().entrySet()) {
- String resource = entry.getKey();
- Presence presence = entry.getValue();
- if (presence.getServiceDiscoveryResult().getFeatures().contains("http://jabber.org/protocol/commands")) {
- binding.tabLayout.setVisibility(View.VISIBLE);
- binding.conversationViewPager.setCurrentItem(1);
- Jid jid = conversation.getContact().getJid();
- if (resource != null && !resource.equals("")) jid = jid.withResource(resource);
- activity.xmppConnectionService.fetchCommands(conversation.getAccount(), jid, (a, iq) -> {
- if (iq.getType() == IqPacket.TYPE.RESULT) {
- activity.runOnUiThread(() -> {
- for (Element child : iq.query().getChildren()) {
- if (!"item".equals(child.getName()) || !Namespace.DISCO_ITEMS.equals(child.getNamespace())) continue;
- commandAdapter.add(child);
- }
- });
- } else {
- binding.tabLayout.setVisibility(View.GONE);
- binding.conversationViewPager.setCurrentItem(0);
- }
- });
- break;
- }
+ Jid commandJid = conversation.getContact().resourceWhichSupport(Namespace.COMMANDS);
+ if (commandJid != null) {
+ binding.tabLayout.setVisibility(View.VISIBLE);
+ activity.xmppConnectionService.fetchCommands(conversation.getAccount(), commandJid, (a, iq) -> {
+ if (iq.getType() == IqPacket.TYPE.RESULT) {
+ activity.runOnUiThread(() -> {
+ for (Element child : iq.query().getChildren()) {
+ if (!"item".equals(child.getName()) || !Namespace.DISCO_ITEMS.equals(child.getNamespace())) continue;
+ commandAdapter.add(child);
+ }
+ });
+ } else {
+ binding.tabLayout.setVisibility(View.GONE);
+ binding.conversationViewPager.setCurrentItem(0);
+ }
+ });
}
return binding.getRoot();