2014-02-02 19:38:06 -08:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2014-02-17 11:42:51 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.google.protobuf.ByteString;
|
|
|
|
|
|
|
|
import org.whispersystems.textsecure.util.Base64;
|
2014-02-02 19:38:06 -08:00
|
|
|
import org.whispersystems.textsecure.util.Hex;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-02-17 11:42:51 -08:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2014-02-02 19:38:06 -08:00
|
|
|
|
2014-02-14 15:59:57 -08:00
|
|
|
import static org.whispersystems.textsecure.push.PushMessageProtos.PushMessageContent.GroupContext;
|
|
|
|
|
2014-02-02 19:38:06 -08: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-02-17 22:20:43 -08:00
|
|
|
public static boolean isMetaGroupAction(int groupAction) {
|
|
|
|
return groupAction > 0
|
|
|
|
&& groupAction != GroupContext.Type.DELIVER_VALUE;
|
|
|
|
}
|
|
|
|
|
2014-02-17 11:42:51 -08:00
|
|
|
public static String serializeArguments(byte[] id, String name, List<String> members) {
|
|
|
|
return Base64.encodeBytes(GroupContext.newBuilder()
|
|
|
|
.setId(ByteString.copyFrom(id))
|
|
|
|
.setName(name)
|
|
|
|
.addAllMembers(members)
|
|
|
|
.build().toByteArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String serializeArguments(GroupContext context) {
|
|
|
|
return Base64.encodeBytes(context.toByteArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> getSerializedArgumentMembers(String serialized) {
|
|
|
|
if (serialized == null) {
|
|
|
|
return new LinkedList<String>();
|
2014-02-14 15:59:57 -08:00
|
|
|
}
|
|
|
|
|
2014-02-17 11:42:51 -08:00
|
|
|
try {
|
|
|
|
GroupContext context = GroupContext.parseFrom(Base64.decode(serialized));
|
|
|
|
return context.getMembersList();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("GroupUtil", e);
|
|
|
|
return new LinkedList<String>();
|
|
|
|
}
|
2014-02-14 15:59:57 -08:00
|
|
|
}
|
2014-02-02 19:38:06 -08:00
|
|
|
}
|