70 lines
2.0 KiB
Java
70 lines
2.0 KiB
Java
|
package org.thoughtcrime.securesms.contacts.avatars;
|
||
|
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.net.Uri;
|
||
|
import androidx.annotation.NonNull;
|
||
|
import androidx.annotation.Nullable;
|
||
|
|
||
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||
|
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;
|
||
|
|
||
|
public class GroupRecordContactPhoto implements ContactPhoto {
|
||
|
|
||
|
private final String groupId;
|
||
|
private final long avatarId;
|
||
|
|
||
|
public GroupRecordContactPhoto(@NonNull String groupId, long avatarId) {
|
||
|
this.groupId = groupId;
|
||
|
this.avatarId = avatarId;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public InputStream openInputStream(Context context) throws IOException {
|
||
|
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
||
|
Optional<GroupDatabase.GroupRecord> groupRecord = groupDatabase.getGroup(groupId);
|
||
|
|
||
|
if (groupRecord.isPresent() && groupRecord.get().getAvatar() != null) {
|
||
|
return new ByteArrayInputStream(groupRecord.get().getAvatar());
|
||
|
}
|
||
|
|
||
|
throw new IOException("Couldn't load avatar for group: " + groupId);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public @Nullable Uri getUri(@NonNull Context context) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isProfilePhoto() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
|
||
|
messageDigest.update(groupId.getBytes());
|
||
|
messageDigest.update(Conversions.longToByteArray(avatarId));
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean equals(Object other) {
|
||
|
if (other == null || !(other instanceof GroupRecordContactPhoto)) return false;
|
||
|
|
||
|
GroupRecordContactPhoto that = (GroupRecordContactPhoto)other;
|
||
|
return this.groupId.equals(that.groupId) && this.avatarId == that.avatarId;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int hashCode() {
|
||
|
return this.groupId.hashCode() ^ (int) avatarId;
|
||
|
}
|
||
|
}
|