1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.ui.util;
31
32import android.app.Activity;
33import android.content.Context;
34import androidx.appcompat.app.AlertDialog;
35import android.util.Pair;
36
37import java.util.Collections;
38import java.util.Map;
39import java.util.concurrent.atomic.AtomicInteger;
40
41import eu.siacs.conversations.R;
42import eu.siacs.conversations.entities.Contact;
43import eu.siacs.conversations.entities.Conversation;
44import eu.siacs.conversations.entities.Presences;
45import eu.siacs.conversations.utils.CryptoHelper;
46import eu.siacs.conversations.xmpp.Jid;
47import eu.siacs.conversations.xmpp.jingle.RtpCapability;
48
49public class PresenceSelector {
50
51 public static void showPresenceSelectionDialog(Activity activity, final Conversation conversation, final OnPresenceSelected listener) {
52 final Contact contact = conversation.getContact();
53 final String[] resourceArray = contact.getPresences().toResourceArray();
54 showPresenceSelectionDialog(activity, contact, resourceArray, fullJid -> {
55 conversation.setNextCounterpart(fullJid);
56 listener.onPresenceSelected();
57 });
58 }
59
60 public static void selectFullJidForDirectRtpConnection(final Activity activity, final Contact contact, final RtpCapability.Capability required, final OnFullJidSelected onFullJidSelected) {
61 final String[] resources = RtpCapability.filterPresences(contact, required);
62 if (resources.length == 1) {
63 onFullJidSelected.onFullJidSelected(contact.getJid().withResource(resources[0]));
64 } else {
65 showPresenceSelectionDialog(activity, contact, resources, onFullJidSelected);
66 }
67 }
68
69 private static void showPresenceSelectionDialog(final Activity activity, final Contact contact, final String[] resourceArray, final OnFullJidSelected onFullJidSelected) {
70 final Presences presences = contact.getPresences();
71 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
72 builder.setTitle(activity.getString(R.string.choose_presence));
73 Pair<Map<String, String>, Map<String, String>> typeAndName = presences.toTypeAndNameMap();
74 final Map<String, String> resourceTypeMap = typeAndName.first;
75 final Map<String, String> resourceNameMap = typeAndName.second;
76 final String[] readableIdentities = new String[resourceArray.length];
77 final AtomicInteger selectedResource = new AtomicInteger(0);
78 for (int i = 0; i < resourceArray.length; ++i) {
79 String resource = resourceArray[i];
80 if (resource.equals(contact.getLastResource())) {
81 selectedResource.set(i);
82 }
83 String type = resourceTypeMap.get(resource);
84 String name = resourceNameMap.get(resource);
85 if (type != null) {
86 if (Collections.frequency(resourceTypeMap.values(), type) == 1) {
87 readableIdentities[i] = translateType(activity, type);
88 } else if (name != null) {
89 if (Collections.frequency(resourceNameMap.values(), name) == 1
90 || CryptoHelper.UUID_PATTERN.matcher(resource).matches()) {
91 readableIdentities[i] = translateType(activity, type) + " (" + name + ")";
92 } else {
93 readableIdentities[i] = translateType(activity, type) + " (" + name + " / " + resource + ")";
94 }
95 } else {
96 readableIdentities[i] = translateType(activity, type) + " (" + resource + ")";
97 }
98 } else {
99 readableIdentities[i] = resource;
100 }
101 }
102 builder.setSingleChoiceItems(readableIdentities,
103 selectedResource.get(),
104 (dialog, which) -> selectedResource.set(which));
105 builder.setNegativeButton(R.string.cancel, null);
106 builder.setPositiveButton(
107 R.string.ok,
108 (dialog, which) -> onFullJidSelected.onFullJidSelected(
109 getNextCounterpart(contact, resourceArray[selectedResource.get()])
110 )
111 );
112 builder.create().show();
113 }
114
115 public static Jid getNextCounterpart(final Contact contact, final String resource) {
116 return getNextCounterpart(contact.getJid(), resource);
117 }
118
119 public static Jid getNextCounterpart(final Jid jid, final String resource) {
120 if (resource.isEmpty()) {
121 return jid.asBareJid();
122 } else {
123 return jid.withResource(resource);
124 }
125 }
126
127 public static void warnMutualPresenceSubscription(Activity activity, final Conversation conversation, final OnPresenceSelected listener) {
128 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
129 builder.setTitle(conversation.getContact().getJid().toString());
130 builder.setMessage(R.string.without_mutual_presence_updates);
131 builder.setNegativeButton(R.string.cancel, null);
132 builder.setPositiveButton(R.string.ignore, (dialog, which) -> {
133 conversation.setNextCounterpart(null);
134 if (listener != null) {
135 listener.onPresenceSelected();
136 }
137 });
138 builder.create().show();
139 }
140
141 private static String translateType(Context context, String type) {
142 switch (type.toLowerCase()) {
143 case "pc":
144 return context.getString(R.string.type_pc);
145 case "phone":
146 return context.getString(R.string.type_phone);
147 case "tablet":
148 return context.getString(R.string.type_tablet);
149 case "web":
150 return context.getString(R.string.type_web);
151 case "console":
152 return context.getString(R.string.type_console);
153 default:
154 return type;
155 }
156 }
157
158 public interface OnPresenceSelected {
159 void onPresenceSelected();
160 }
161
162 public interface OnFullJidSelected {
163 void onFullJidSelected(Jid jid);
164 }
165}