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.support.v7.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;
 47
 48public class PresenceSelector {
 49
 50	public static void showPresenceSelectionDialog(Activity activity, final Conversation conversation, final OnPresenceSelected listener) {
 51		final Contact contact = conversation.getContact();
 52		final Presences presences = contact.getPresences();
 53		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
 54		builder.setTitle(activity.getString(R.string.choose_presence));
 55		final String[] resourceArray = presences.toResourceArray();
 56		Pair<Map<String, String>, Map<String, String>> typeAndName = presences.toTypeAndNameMap();
 57		final Map<String, String> resourceTypeMap = typeAndName.first;
 58		final Map<String, String> resourceNameMap = typeAndName.second;
 59		final String[] readableIdentities = new String[resourceArray.length];
 60		final AtomicInteger selectedResource = new AtomicInteger(0);
 61		for (int i = 0; i < resourceArray.length; ++i) {
 62			String resource = resourceArray[i];
 63			if (resource.equals(contact.getLastResource())) {
 64				selectedResource.set(i);
 65			}
 66			String type = resourceTypeMap.get(resource);
 67			String name = resourceNameMap.get(resource);
 68			if (type != null) {
 69				if (Collections.frequency(resourceTypeMap.values(), type) == 1) {
 70					readableIdentities[i] = translateType(activity, type);
 71				} else if (name != null) {
 72					if (Collections.frequency(resourceNameMap.values(), name) == 1
 73							|| CryptoHelper.UUID_PATTERN.matcher(resource).matches()) {
 74						readableIdentities[i] = translateType(activity, type) + "  (" + name + ")";
 75					} else {
 76						readableIdentities[i] = translateType(activity, type) + " (" + name + " / " + resource + ")";
 77					}
 78				} else {
 79					readableIdentities[i] = translateType(activity, type) + " (" + resource + ")";
 80				}
 81			} else {
 82				readableIdentities[i] = resource;
 83			}
 84		}
 85		builder.setSingleChoiceItems(readableIdentities,
 86				selectedResource.get(),
 87				(dialog, which) -> selectedResource.set(which));
 88		builder.setNegativeButton(R.string.cancel, null);
 89		builder.setPositiveButton(R.string.ok, (dialog, which) -> {
 90			try {
 91				Jid next = Jid.of(contact.getJid().getLocal(), contact.getJid().getDomain(), resourceArray[selectedResource.get()]);
 92				conversation.setNextCounterpart(next);
 93			} catch (IllegalArgumentException e) {
 94				conversation.setNextCounterpart(null);
 95			}
 96			listener.onPresenceSelected();
 97		});
 98		builder.create().show();
 99	}
100
101	public static void warnMutualPresenceSubscription(Activity activity, final Conversation conversation, final OnPresenceSelected listener) {
102		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
103		builder.setTitle(conversation.getContact().getJid().toString());
104		builder.setMessage(R.string.without_mutual_presence_updates);
105		builder.setNegativeButton(R.string.cancel, null);
106		builder.setPositiveButton(R.string.ignore, (dialog, which) -> {
107			conversation.setNextCounterpart(null);
108			if (listener != null) {
109				listener.onPresenceSelected();
110			}
111		});
112		builder.create().show();
113	}
114
115	private static String translateType(Context context, String type) {
116		switch (type.toLowerCase()) {
117			case "pc":
118				return context.getString(R.string.type_pc);
119			case "phone":
120				return context.getString(R.string.type_phone);
121			case "tablet":
122				return context.getString(R.string.type_tablet);
123			case "web":
124				return context.getString(R.string.type_web);
125			case "console":
126				return context.getString(R.string.type_console);
127			default:
128				return type;
129		}
130	}
131
132	public interface OnPresenceSelected {
133		void onPresenceSelected();
134	}
135}