2015-11-05 18:41:43 +00:00
|
|
|
package org.thoughtcrime.securesms.groups;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
|
|
|
|
import com.google.protobuf.ByteString;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.UriAttachment;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-11-05 18:41:43 +00:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
|
|
|
|
import org.thoughtcrime.securesms.providers.SingleUseBlobProvider;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.sms.MessageSender;
|
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2017-05-08 22:32:59 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
2015-11-05 18:41:43 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.signalservice.api.util.InvalidNumberException;
|
|
|
|
import org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext;
|
2015-11-05 18:41:43 +00:00
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
2017-07-26 16:59:15 +00:00
|
|
|
import java.util.List;
|
2015-11-05 18:41:43 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class GroupManager {
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2015-11-05 18:41:43 +00:00
|
|
|
public static @NonNull GroupActionResult createGroup(@NonNull Context context,
|
|
|
|
@NonNull MasterSecret masterSecret,
|
|
|
|
@NonNull Set<Recipient> members,
|
|
|
|
@Nullable Bitmap avatar,
|
|
|
|
@Nullable String name)
|
|
|
|
throws InvalidNumberException
|
|
|
|
{
|
2017-07-26 16:59:15 +00:00
|
|
|
final byte[] avatarBytes = BitmapUtil.toByteArray(avatar);
|
|
|
|
final GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
|
|
|
final byte[] groupId = groupDatabase.allocateGroupId();
|
|
|
|
final Set<Address> memberAddresses = getMemberAddresses(context, members);
|
2015-11-05 18:41:43 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
memberAddresses.add(Address.fromSerialized(TextSecurePreferences.getLocalNumber(context)));
|
|
|
|
groupDatabase.create(groupId, name, new LinkedList<>(memberAddresses), null, null);
|
2015-11-05 18:41:43 +00:00
|
|
|
groupDatabase.updateAvatar(groupId, avatarBytes);
|
2017-07-26 16:59:15 +00:00
|
|
|
return sendGroupUpdate(context, masterSecret, groupId, memberAddresses, name, avatarBytes);
|
2015-11-05 18:41:43 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
private static Set<Address> getMemberAddresses(Context context, Collection<Recipient> recipients)
|
2015-11-05 18:41:43 +00:00
|
|
|
throws InvalidNumberException
|
|
|
|
{
|
2017-07-26 16:59:15 +00:00
|
|
|
final Set<Address> results = new HashSet<>();
|
2015-11-05 18:41:43 +00:00
|
|
|
for (Recipient recipient : recipients) {
|
2017-07-26 16:59:15 +00:00
|
|
|
results.add(recipient.getAddress());
|
2015-11-05 18:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static GroupActionResult updateGroup(@NonNull Context context,
|
|
|
|
@NonNull MasterSecret masterSecret,
|
|
|
|
@NonNull byte[] groupId,
|
|
|
|
@NonNull Set<Recipient> members,
|
|
|
|
@Nullable Bitmap avatar,
|
|
|
|
@Nullable String name)
|
|
|
|
throws InvalidNumberException
|
|
|
|
{
|
2017-07-26 16:59:15 +00:00
|
|
|
final GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
|
|
|
final Set<Address> memberAddresses = getMemberAddresses(context, members);
|
|
|
|
final byte[] avatarBytes = BitmapUtil.toByteArray(avatar);
|
2015-11-05 18:41:43 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
memberAddresses.add(Address.fromSerialized(TextSecurePreferences.getLocalNumber(context)));
|
|
|
|
groupDatabase.updateMembers(groupId, new LinkedList<>(memberAddresses));
|
2015-11-05 18:41:43 +00:00
|
|
|
groupDatabase.updateTitle(groupId, name);
|
|
|
|
groupDatabase.updateAvatar(groupId, avatarBytes);
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
return sendGroupUpdate(context, masterSecret, groupId, memberAddresses, name, avatarBytes);
|
2015-11-05 18:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static GroupActionResult sendGroupUpdate(@NonNull Context context,
|
|
|
|
@NonNull MasterSecret masterSecret,
|
|
|
|
@NonNull byte[] groupId,
|
2017-07-26 16:59:15 +00:00
|
|
|
@NonNull Set<Address> members,
|
2015-11-05 18:41:43 +00:00
|
|
|
@Nullable String groupName,
|
|
|
|
@Nullable byte[] avatar)
|
|
|
|
{
|
|
|
|
Attachment avatarAttachment = null;
|
2017-07-26 16:59:15 +00:00
|
|
|
Address groupAddress = Address.fromSerialized(GroupUtil.getEncodedId(groupId));
|
|
|
|
Recipients groupRecipient = RecipientFactory.getRecipientsFor(context, new Address[]{groupAddress}, false);
|
|
|
|
|
|
|
|
List<String> numbers = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Address member : members) {
|
|
|
|
numbers.add(member.serialize());
|
|
|
|
}
|
2015-11-05 18:41:43 +00:00
|
|
|
|
|
|
|
GroupContext.Builder groupContextBuilder = GroupContext.newBuilder()
|
|
|
|
.setId(ByteString.copyFrom(groupId))
|
|
|
|
.setType(GroupContext.Type.UPDATE)
|
2017-07-26 16:59:15 +00:00
|
|
|
.addAllMembers(numbers);
|
2015-11-05 18:41:43 +00:00
|
|
|
if (groupName != null) groupContextBuilder.setName(groupName);
|
|
|
|
GroupContext groupContext = groupContextBuilder.build();
|
|
|
|
|
|
|
|
if (avatar != null) {
|
|
|
|
Uri avatarUri = SingleUseBlobProvider.getInstance().createUri(avatar);
|
2017-05-12 05:46:35 +00:00
|
|
|
avatarAttachment = new UriAttachment(avatarUri, MediaUtil.IMAGE_PNG, AttachmentDatabase.TRANSFER_PROGRESS_DONE, avatar.length, null, false);
|
2015-11-05 18:41:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
OutgoingGroupMediaMessage outgoingMessage = new OutgoingGroupMediaMessage(groupRecipient, groupContext, avatarAttachment, System.currentTimeMillis(), 0);
|
2017-04-22 23:29:26 +00:00
|
|
|
long threadId = MessageSender.send(context, masterSecret, outgoingMessage, -1, false, null);
|
2015-11-05 18:41:43 +00:00
|
|
|
|
|
|
|
return new GroupActionResult(groupRecipient, threadId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class GroupActionResult {
|
|
|
|
private Recipients groupRecipient;
|
|
|
|
private long threadId;
|
|
|
|
|
|
|
|
public GroupActionResult(Recipients groupRecipient, long threadId) {
|
|
|
|
this.groupRecipient = groupRecipient;
|
|
|
|
this.threadId = threadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipients getGroupRecipient() {
|
|
|
|
return groupRecipient;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getThreadId() {
|
|
|
|
return threadId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|