2017-10-16 22:11:42 +02:00
|
|
|
package org.thoughtcrime.securesms.contacts.avatars;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-04-27 02:03:54 +02:00
|
|
|
import android.net.Uri;
|
2020-03-26 15:00:17 +01:00
|
|
|
|
2019-06-05 21:47:14 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2017-10-16 22:11:42 +02:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
2020-03-26 15:00:17 +01:00
|
|
|
import org.thoughtcrime.securesms.groups.GroupId;
|
2020-03-26 20:38:27 +01:00
|
|
|
import org.thoughtcrime.securesms.profiles.AvatarHelper;
|
2017-10-16 22:11:42 +02:00
|
|
|
import org.thoughtcrime.securesms.util.Conversions;
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
2020-03-26 15:00:17 +01:00
|
|
|
public final class GroupRecordContactPhoto implements ContactPhoto {
|
2017-10-16 22:11:42 +02:00
|
|
|
|
2020-03-26 15:00:17 +01:00
|
|
|
private final GroupId groupId;
|
|
|
|
private final long avatarId;
|
2017-10-16 22:11:42 +02:00
|
|
|
|
2020-03-26 15:00:17 +01:00
|
|
|
public GroupRecordContactPhoto(@NonNull GroupId groupId, long avatarId) {
|
2019-09-07 05:40:06 +02:00
|
|
|
this.groupId = groupId;
|
2017-10-16 22:11:42 +02:00
|
|
|
this.avatarId = avatarId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InputStream openInputStream(Context context) throws IOException {
|
|
|
|
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
2019-09-07 05:40:06 +02:00
|
|
|
Optional<GroupDatabase.GroupRecord> groupRecord = groupDatabase.getGroup(groupId);
|
2017-10-16 22:11:42 +02:00
|
|
|
|
2020-03-26 20:38:27 +01:00
|
|
|
if (!groupRecord.isPresent() || !AvatarHelper.hasAvatar(context, groupRecord.get().getRecipientId())) {
|
|
|
|
throw new IOException("No avatar for group: " + groupId);
|
2017-10-16 22:11:42 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 20:38:27 +01:00
|
|
|
return AvatarHelper.getAvatar(context, groupRecord.get().getRecipientId());
|
2017-10-16 22:11:42 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 02:03:54 +02:00
|
|
|
@Override
|
|
|
|
public @Nullable Uri getUri(@NonNull Context context) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isProfilePhoto() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-16 22:11:42 +02:00
|
|
|
@Override
|
2019-05-22 18:51:56 +02:00
|
|
|
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
|
2020-03-26 15:00:17 +01:00
|
|
|
messageDigest.update(groupId.toString().getBytes());
|
2017-10-16 22:11:42 +02:00
|
|
|
messageDigest.update(Conversions.longToByteArray(avatarId));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object other) {
|
2020-03-26 15:00:17 +01:00
|
|
|
if (!(other instanceof GroupRecordContactPhoto)) return false;
|
2017-10-16 22:11:42 +02:00
|
|
|
|
|
|
|
GroupRecordContactPhoto that = (GroupRecordContactPhoto)other;
|
2019-09-07 05:40:06 +02:00
|
|
|
return this.groupId.equals(that.groupId) && this.avatarId == that.avatarId;
|
2017-10-16 22:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2019-09-07 05:40:06 +02:00
|
|
|
return this.groupId.hashCode() ^ (int) avatarId;
|
2017-10-16 22:11:42 +02:00
|
|
|
}
|
|
|
|
}
|