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