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