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;
|
2014-02-17 19:42:51 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2014-02-22 01:51:25 +00:00
|
|
|
import com.google.protobuf.InvalidProtocolBufferException;
|
2014-02-17 19:42:51 +00:00
|
|
|
|
2014-02-03 03:38:06 +00:00
|
|
|
import java.io.IOException;
|
2014-02-17 19:42:51 +00:00
|
|
|
import java.util.List;
|
2014-02-03 03:38:06 +00:00
|
|
|
|
2014-12-15 14:44:41 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2014-11-12 19:35:54 +00:00
|
|
|
import static org.whispersystems.textsecure.internal.push.PushMessageProtos.PushMessageContent.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__!";
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-15 14:44:41 +00:00
|
|
|
public static String getDescription(Context context, String encodedGroup) {
|
2014-02-22 01:51:25 +00:00
|
|
|
if (encodedGroup == null) {
|
2014-12-15 14:44:41 +00:00
|
|
|
return context.getString(R.string.GroupUtil_group_updated);
|
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
|
|
|
StringBuilder description = new StringBuilder();
|
|
|
|
GroupContext groupContext = GroupContext.parseFrom(Base64.decode(encodedGroup));
|
|
|
|
List<String> members = groupContext.getMembersList();
|
|
|
|
String title = groupContext.getName();
|
2014-02-17 19:42:51 +00:00
|
|
|
|
2014-02-22 01:51:25 +00:00
|
|
|
if (!members.isEmpty()) {
|
2014-12-29 22:32:29 +00:00
|
|
|
description.append(context.getString(R.string.GroupUtil_joined_the_group, Util.join(members, ", ")));
|
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(" ");
|
|
|
|
description.append(context.getString(R.string.GroupUtil_title_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);
|
|
|
|
}
|
2014-02-22 01:51:25 +00:00
|
|
|
} catch (InvalidProtocolBufferException e) {
|
|
|
|
Log.w("GroupUtil", e);
|
2014-12-15 14:44:41 +00:00
|
|
|
return context.getString(R.string.GroupUtil_group_updated);
|
2014-02-17 19:42:51 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("GroupUtil", e);
|
2014-12-15 14:44:41 +00:00
|
|
|
return context.getString(R.string.GroupUtil_group_updated);
|
2014-02-17 19:42:51 +00:00
|
|
|
}
|
2014-02-14 23:59:57 +00:00
|
|
|
}
|
2014-02-03 03:38:06 +00:00
|
|
|
}
|