2014-02-03 03:38:06 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2014-12-15 14:44:41 +00:00
|
|
|
import android.content.Context;
|
2015-09-17 00:31:24 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2014-02-17 19:42:51 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2015-09-17 00:31:24 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2014-02-17 19:42:51 +00:00
|
|
|
|
2014-02-03 03:38:06 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2015-05-29 23:23:47 +00:00
|
|
|
import static org.whispersystems.textsecure.internal.push.TextSecureProtos.GroupContext;
|
2014-02-14 23:59:57 +00:00
|
|
|
|
2014-02-03 03:38:06 +00:00
|
|
|
public class GroupUtil {
|
|
|
|
|
|
|
|
private static final String ENCODED_GROUP_PREFIX = "__textsecure_group__!";
|
2015-09-17 00:31:24 +00:00
|
|
|
private static final String TAG = GroupUtil.class.getSimpleName();
|
2014-02-03 03:38:06 +00:00
|
|
|
|
|
|
|
public static String getEncodedId(byte[] groupId) {
|
|
|
|
return ENCODED_GROUP_PREFIX + Hex.toStringCondensed(groupId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] getDecodedId(String groupId) throws IOException {
|
|
|
|
if (!isEncodedGroup(groupId)) {
|
|
|
|
throw new IOException("Invalid encoding");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Hex.fromStringCondensed(groupId.split("!", 2)[1]);
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static boolean isEncodedGroup(@NonNull String groupId) {
|
2014-02-03 03:38:06 +00:00
|
|
|
return groupId.startsWith(ENCODED_GROUP_PREFIX);
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:31:24 +00:00
|
|
|
public static @NonNull GroupDescription getDescription(@NonNull Context context, @Nullable String encodedGroup) {
|
2014-02-22 01:51:25 +00:00
|
|
|
if (encodedGroup == null) {
|
2015-09-17 00:31:24 +00:00
|
|
|
return new GroupDescription(context, null);
|
2014-02-22 01:51:25 +00:00
|
|
|
}
|
2014-02-18 06:20:43 +00:00
|
|
|
|
2014-02-22 01:51:25 +00:00
|
|
|
try {
|
2014-12-29 22:32:29 +00:00
|
|
|
GroupContext groupContext = GroupContext.parseFrom(Base64.decode(encodedGroup));
|
2015-09-17 00:31:24 +00:00
|
|
|
return new GroupDescription(context, groupContext);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return new GroupDescription(context, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class GroupDescription {
|
|
|
|
|
|
|
|
@NonNull private final Context context;
|
|
|
|
@Nullable private final GroupContext groupContext;
|
|
|
|
@Nullable private final Recipients members;
|
|
|
|
|
|
|
|
public GroupDescription(@NonNull Context context, @Nullable GroupContext groupContext) {
|
|
|
|
this.context = context.getApplicationContext();
|
|
|
|
this.groupContext = groupContext;
|
|
|
|
|
|
|
|
if (groupContext == null || groupContext.getMembersList().isEmpty()) {
|
|
|
|
this.members = null;
|
|
|
|
} else {
|
|
|
|
this.members = RecipientFactory.getRecipientsFromString(context, Util.join(groupContext.getMembersList(), ", "), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
if (groupContext == null) {
|
|
|
|
return context.getString(R.string.GroupUtil_group_updated);
|
|
|
|
}
|
2014-02-17 19:42:51 +00:00
|
|
|
|
2015-09-17 00:31:24 +00:00
|
|
|
StringBuilder description = new StringBuilder();
|
|
|
|
String title = groupContext.getName();
|
|
|
|
|
|
|
|
if (members != null) {
|
|
|
|
description.append(context.getString(R.string.GroupUtil_joined_the_group, members.toShortString()));
|
2014-02-22 01:51:25 +00:00
|
|
|
}
|
2014-02-17 19:42:51 +00:00
|
|
|
|
2014-02-22 01:51:25 +00:00
|
|
|
if (title != null && !title.trim().isEmpty()) {
|
2014-12-29 22:32:29 +00:00
|
|
|
if (description.length() > 0) description.append(" ");
|
2015-06-13 21:16:02 +00:00
|
|
|
description.append(context.getString(R.string.GroupUtil_group_name_is_now, title));
|
2014-02-22 01:51:25 +00:00
|
|
|
}
|
2014-02-14 23:59:57 +00:00
|
|
|
|
2015-02-06 03:37:11 +00:00
|
|
|
if (description.length() > 0) {
|
|
|
|
return description.toString();
|
|
|
|
} else {
|
|
|
|
return context.getString(R.string.GroupUtil_group_updated);
|
|
|
|
}
|
2015-09-17 00:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addListener(Recipients.RecipientsModifiedListener listener) {
|
|
|
|
if (this.members != null) {
|
|
|
|
this.members.addListener(listener);
|
|
|
|
}
|
2014-02-17 19:42:51 +00:00
|
|
|
}
|
2014-02-14 23:59:57 +00:00
|
|
|
}
|
2014-02-03 03:38:06 +00:00
|
|
|
}
|