Add contact and key sync message receive support.

This commit is contained in:
Cody Henthorne
2022-01-18 11:10:23 -05:00
committed by Greyson Parrelli
parent c5028720e3
commit c548816daa
17 changed files with 542 additions and 143 deletions

View File

@@ -12,6 +12,7 @@ import org.thoughtcrime.securesms.crypto.AttachmentSecret;
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider;
import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream;
import org.thoughtcrime.securesms.crypto.ModernEncryptingPartOutputStream;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.util.ByteUnit;
@@ -129,7 +130,24 @@ public class AvatarHelper {
OutputStream outputStream = null;
try {
outputStream = getOutputStream(context, recipientId);
outputStream = getOutputStream(context, recipientId, false);
StreamUtil.copy(inputStream, outputStream);
} finally {
StreamUtil.close(outputStream);
}
}
public static void setSyncAvatar(@NonNull Context context, @NonNull RecipientId recipientId, @Nullable InputStream inputStream)
throws IOException
{
if (inputStream == null) {
delete(context, recipientId);
return;
}
OutputStream outputStream = null;
try {
outputStream = getOutputStream(context, recipientId, true);
StreamUtil.copy(inputStream, outputStream);
} finally {
StreamUtil.close(outputStream);
@@ -140,9 +158,9 @@ public class AvatarHelper {
* Retrieves an output stream you can write to that will be saved as the avatar for the specified
* recipient. Only intended to be used for backup. Otherwise, use {@link #setAvatar(Context, RecipientId, InputStream)}.
*/
public static @NonNull OutputStream getOutputStream(@NonNull Context context, @NonNull RecipientId recipientId) throws IOException {
public static @NonNull OutputStream getOutputStream(@NonNull Context context, @NonNull RecipientId recipientId, boolean isSyncAvatar) throws IOException {
AttachmentSecret attachmentSecret = AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret();
File targetFile = getAvatarFile(context, recipientId);
File targetFile = getAvatarFile(context, recipientId, isSyncAvatar);
return ModernEncryptingPartOutputStream.createFor(attachmentSecret, targetFile, true).second;
}
@@ -179,8 +197,25 @@ public class AvatarHelper {
}
private static @NonNull File getAvatarFile(@NonNull Context context, @NonNull RecipientId recipientId) {
File profileAvatar = getAvatarFile(context, recipientId, false);
boolean profileAvatarExists = profileAvatar.exists() && profileAvatar.length() > 0;
File syncAvatar = getAvatarFile(context, recipientId, true);
boolean syncAvatarExists = syncAvatar.exists() && syncAvatar.length() > 0;
if (SignalStore.settings().isPreferSystemContactPhotos() && syncAvatarExists) {
return syncAvatar;
} else if (profileAvatarExists) {
return profileAvatar;
} else if (syncAvatarExists) {
return syncAvatar;
}
return profileAvatar;
}
private static @NonNull File getAvatarFile(@NonNull Context context, @NonNull RecipientId recipientId, boolean isSyncAvatar) {
File directory = context.getDir(AVATAR_DIRECTORY, Context.MODE_PRIVATE);
return new File(directory, recipientId.serialize());
return new File(directory, recipientId.serialize() + (isSyncAvatar ? "-sync" : ""));
}
public static class Avatar {