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 android.util.Pair;
35import android.widget.Toast;
36
37import androidx.appcompat.app.AlertDialog;
38
39import com.google.android.material.dialog.MaterialAlertDialogBuilder;
40
41import java.util.Collections;
42import java.util.Map;
43import java.util.concurrent.atomic.AtomicInteger;
44
45import eu.siacs.conversations.R;
46import eu.siacs.conversations.entities.Contact;
47import eu.siacs.conversations.entities.Conversation;
48import eu.siacs.conversations.entities.Presences;
49import eu.siacs.conversations.utils.CryptoHelper;
50import eu.siacs.conversations.xmpp.Jid;
51import eu.siacs.conversations.xmpp.jingle.RtpCapability;
52
53public class PresenceSelector {
54
55 public static void showPresenceSelectionDialog(Activity activity, final Conversation conversation, final OnPresenceSelected listener) {
56 final Contact contact = conversation.getContact();
57 final String[] resourceArray = contact.getPresences().toResourceArray();
58 showPresenceSelectionDialog(activity, contact, resourceArray, fullJid -> {
59 conversation.setNextCounterpart(fullJid);
60 listener.onPresenceSelected();
61 });
62 }
63
64 public static void selectFullJidForDirectRtpConnection(final Activity activity, final Contact contact, final RtpCapability.Capability required, final OnFullJidSelected onFullJidSelected) {
65 final String[] resources = RtpCapability.filterPresences(contact, required);
66 if (resources.length < 1) {
67 Toast.makeText(activity,R.string.rtp_state_contact_offline,Toast.LENGTH_LONG).show();
68 } else if (resources.length == 1) {
69 onFullJidSelected.onFullJidSelected(contact.getJid().withResource(resources[0]));
70 } else {
71 showPresenceSelectionDialog(activity, contact, resources, onFullJidSelected);
72 }
73 }
74
75 private static void showPresenceSelectionDialog(final Activity activity, final Contact contact, final String[] resourceArray, final OnFullJidSelected onFullJidSelected) {
76 final Presences presences = contact.getPresences();
77 final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(activity);
78 builder.setTitle(activity.getString(R.string.choose_presence));
79 Pair<Map<String, String>, Map<String, String>> typeAndName = presences.toTypeAndNameMap();
80 final Map<String, String> resourceTypeMap = typeAndName.first;
81 final Map<String, String> resourceNameMap = typeAndName.second;
82 final String[] readableIdentities = new String[resourceArray.length];
83 final AtomicInteger selectedResource = new AtomicInteger(0);
84 for (int i = 0; i < resourceArray.length; ++i) {
85 String resource = resourceArray[i];
86 if (resource.equals(contact.getLastResource())) {
87 selectedResource.set(i);
88 }
89 String type = resourceTypeMap.get(resource);
90 String name = resourceNameMap.get(resource);
91 if (type != null) {
92 if (Collections.frequency(resourceTypeMap.values(), type) == 1) {
93 readableIdentities[i] = translateType(activity, type);
94 } else if (name != null) {
95 if (Collections.frequency(resourceNameMap.values(), name) == 1
96 || CryptoHelper.UUID_PATTERN.matcher(resource).matches()) {
97 readableIdentities[i] = translateType(activity, type) + " (" + name + ")";
98 } else {
99 readableIdentities[i] = translateType(activity, type) + " (" + name + " / " + resource + ")";
100 }
101 } else {
102 readableIdentities[i] = translateType(activity, type) + " (" + resource + ")";
103 }
104 } else {
105 readableIdentities[i] = resource;
106 }
107 }
108 builder.setSingleChoiceItems(readableIdentities,
109 selectedResource.get(),
110 (dialog, which) -> selectedResource.set(which));
111 builder.setNegativeButton(R.string.cancel, null);
112 builder.setPositiveButton(
113 R.string.ok,
114 (dialog, which) -> onFullJidSelected.onFullJidSelected(
115 getNextCounterpart(contact, resourceArray[selectedResource.get()])
116 )
117 );
118 builder.create().show();
119 }
120
121 public static Jid getNextCounterpart(final Contact contact, final String resource) {
122 return getNextCounterpart(contact.getJid(), resource);
123 }
124
125 public static Jid getNextCounterpart(final Jid jid, final String resource) {
126 if (resource.isEmpty()) {
127 return jid.asBareJid();
128 } else {
129 return jid.withResource(resource);
130 }
131 }
132
133 public static void warnMutualPresenceSubscription(final Activity activity, final Conversation conversation, final OnPresenceSelected listener) {
134 final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(activity);
135 builder.setTitle(conversation.getContact().getJid().toString());
136 builder.setMessage(R.string.without_mutual_presence_updates);
137 builder.setNegativeButton(R.string.cancel, null);
138 builder.setPositiveButton(R.string.ignore, (dialog, which) -> {
139 conversation.setNextCounterpart(null);
140 if (listener != null) {
141 listener.onPresenceSelected();
142 }
143 });
144 builder.create().show();
145 }
146
147 private static String translateType(Context context, String type) {
148 switch (type.toLowerCase()) {
149 case "pc":
150 return context.getString(R.string.type_pc);
151 case "phone":
152 return context.getString(R.string.type_phone);
153 case "tablet":
154 return context.getString(R.string.type_tablet);
155 case "web":
156 return context.getString(R.string.type_web);
157 case "console":
158 return context.getString(R.string.type_console);
159 default:
160 return type;
161 }
162 }
163
164 public interface OnPresenceSelected {
165 void onPresenceSelected();
166 }
167
168 public interface OnFullJidSelected {
169 void onFullJidSelected(Jid jid);
170 }
171}