PresenceSelector.java

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