session-android/src/org/thoughtcrime/securesms/util/GroupUtil.java

68 lines
2.2 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.util.Log;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.IOException;
import java.util.List;
import org.thoughtcrime.securesms.R;
import static org.whispersystems.textsecure.internal.push.PushMessageProtos.PushMessageContent.GroupContext;
public class GroupUtil {
private static final String ENCODED_GROUP_PREFIX = "__textsecure_group__!";
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]);
}
public static boolean isEncodedGroup(String groupId) {
return groupId.startsWith(ENCODED_GROUP_PREFIX);
}
public static String getDescription(Context context, String encodedGroup) {
if (encodedGroup == null) {
return context.getString(R.string.GroupUtil_group_updated);
}
try {
2014-12-29 22:32:29 +00:00
StringBuilder description = new StringBuilder();
GroupContext groupContext = GroupContext.parseFrom(Base64.decode(encodedGroup));
List<String> members = groupContext.getMembersList();
String title = groupContext.getName();
if (!members.isEmpty()) {
2014-12-29 22:32:29 +00:00
description.append(context.getString(R.string.GroupUtil_joined_the_group, Util.join(members, ", ")));
}
if (title != null && !title.trim().isEmpty()) {
2014-12-29 22:32:29 +00:00
if (description.length() > 0) description.append(" ");
description.append(context.getString(R.string.GroupUtil_title_is_now, title));
}
if (description.length() > 0) {
return description.toString();
} else {
return context.getString(R.string.GroupUtil_group_updated);
}
} catch (InvalidProtocolBufferException e) {
Log.w("GroupUtil", e);
return context.getString(R.string.GroupUtil_group_updated);
} catch (IOException e) {
Log.w("GroupUtil", e);
return context.getString(R.string.GroupUtil_group_updated);
}
}
}