use of org.whispersystems.signalservice.api.SignalServiceMessageReceiver in project Signal-Android by signalapp.
the class AvatarGroupsV2DownloadJob method downloadGroupAvatarBytes.
@Nullable
public static byte[] downloadGroupAvatarBytes(@NonNull Context context, @NonNull GroupMasterKey groupMasterKey, @NonNull String cdnKey) throws IOException {
if (cdnKey.length() == 0) {
return null;
}
GroupSecretParams groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupMasterKey);
File attachment = File.createTempFile("avatar", "gv2", context.getCacheDir());
attachment.deleteOnExit();
SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
byte[] encryptedData;
try (FileInputStream inputStream = receiver.retrieveGroupsV2ProfileAvatar(cdnKey, attachment, AVATAR_DOWNLOAD_FAIL_SAFE_MAX_SIZE)) {
encryptedData = new byte[(int) attachment.length()];
StreamUtil.readFully(inputStream, encryptedData);
GroupsV2Operations operations = ApplicationDependencies.getGroupsV2Operations();
GroupsV2Operations.GroupOperations groupOperations = operations.forGroup(groupSecretParams);
return groupOperations.decryptAvatar(encryptedData);
} finally {
if (attachment.exists())
if (!attachment.delete()) {
Log.w(TAG, "Unable to delete temp avatar file");
}
}
}
use of org.whispersystems.signalservice.api.SignalServiceMessageReceiver in project Signal-Android by signalapp.
the class AvatarGroupsV1DownloadJob method onRun.
@Override
public void onRun() throws IOException {
GroupDatabase database = SignalDatabase.groups();
Optional<GroupRecord> record = database.getGroup(groupId);
File attachment = null;
try {
if (record.isPresent()) {
long avatarId = record.get().getAvatarId();
String contentType = record.get().getAvatarContentType();
byte[] key = record.get().getAvatarKey();
String relay = record.get().getRelay();
Optional<byte[]> digest = Optional.fromNullable(record.get().getAvatarDigest());
Optional<String> fileName = Optional.absent();
if (avatarId == -1 || key == null) {
return;
}
if (digest.isPresent()) {
Log.i(TAG, "Downloading group avatar with digest: " + Hex.toString(digest.get()));
}
attachment = File.createTempFile("avatar", "tmp", context.getCacheDir());
attachment.deleteOnExit();
SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
SignalServiceAttachmentPointer pointer = new SignalServiceAttachmentPointer(0, new SignalServiceAttachmentRemoteId(avatarId), contentType, key, Optional.of(0), Optional.absent(), 0, 0, digest, fileName, false, false, false, Optional.absent(), Optional.absent(), System.currentTimeMillis());
InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, AvatarHelper.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE);
AvatarHelper.setAvatar(context, record.get().getRecipientId(), inputStream);
SignalDatabase.groups().onAvatarUpdated(groupId, true);
inputStream.close();
}
} catch (NonSuccessfulResponseCodeException | InvalidMessageException | MissingConfigurationException e) {
Log.w(TAG, e);
} finally {
if (attachment != null)
attachment.delete();
}
}
use of org.whispersystems.signalservice.api.SignalServiceMessageReceiver in project Signal-Android by WhisperSystems.
the class StickerDownloadJob method onRun.
@Override
protected void onRun() throws Exception {
StickerDatabase db = SignalDatabase.stickers();
StickerRecord stickerRecord = db.getSticker(sticker.getPackId(), sticker.getStickerId(), sticker.isCover());
if (stickerRecord != null) {
try (InputStream stream = PartAuthority.getAttachmentStream(context, stickerRecord.getUri())) {
if (stream != null) {
Log.w(TAG, "Sticker already downloaded.");
return;
}
} catch (FileNotFoundException e) {
Log.w(TAG, "Sticker file no longer exists, downloading again.");
}
}
if (!db.isPackInstalled(sticker.getPackId()) && !sticker.isCover()) {
Log.w(TAG, "Pack is no longer installed.");
return;
}
SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
byte[] packIdBytes = Hex.fromStringCondensed(sticker.getPackId());
byte[] packKeyBytes = Hex.fromStringCondensed(sticker.getPackKey());
InputStream stream = receiver.retrieveSticker(packIdBytes, packKeyBytes, sticker.getStickerId());
db.insertSticker(sticker, stream, notify);
}
use of org.whispersystems.signalservice.api.SignalServiceMessageReceiver in project Signal-Android by WhisperSystems.
the class LinkPreviewRepository method fetchStickerPackLinkPreview.
private static RequestController fetchStickerPackLinkPreview(@NonNull Context context, @NonNull String packUrl, @NonNull Callback callback) {
SignalExecutors.UNBOUNDED.execute(() -> {
try {
Pair<String, String> stickerParams = StickerUrl.parseShareLink(packUrl).or(new Pair<>("", ""));
String packIdString = stickerParams.first();
String packKeyString = stickerParams.second();
byte[] packIdBytes = Hex.fromStringCondensed(packIdString);
byte[] packKeyBytes = Hex.fromStringCondensed(packKeyString);
SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
SignalServiceStickerManifest manifest = receiver.retrieveStickerManifest(packIdBytes, packKeyBytes);
String title = manifest.getTitle().or(manifest.getAuthor()).or("");
Optional<StickerInfo> firstSticker = Optional.fromNullable(manifest.getStickers().size() > 0 ? manifest.getStickers().get(0) : null);
Optional<StickerInfo> cover = manifest.getCover().or(firstSticker);
if (cover.isPresent()) {
Bitmap bitmap = GlideApp.with(context).asBitmap().load(new StickerRemoteUri(packIdString, packKeyString, cover.get().getId())).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).centerInside().submit(512, 512).get();
Optional<Attachment> thumbnail = bitmapToAttachment(bitmap, Bitmap.CompressFormat.WEBP, MediaUtil.IMAGE_WEBP);
callback.onSuccess(new LinkPreview(packUrl, title, "", 0, thumbnail));
} else {
callback.onError(Error.PREVIEW_NOT_AVAILABLE);
}
} catch (IOException | InvalidMessageException | ExecutionException | InterruptedException e) {
Log.w(TAG, "Failed to fetch sticker pack link preview.");
callback.onError(Error.PREVIEW_NOT_AVAILABLE);
}
});
return () -> Log.i(TAG, "Cancelled sticker pack link preview fetch -- no effect.");
}
use of org.whispersystems.signalservice.api.SignalServiceMessageReceiver in project Signal-Android by WhisperSystems.
the class RetrieveProfileAvatarJob method onRun.
@Override
public void onRun() throws IOException {
RecipientDatabase database = SignalDatabase.recipients();
ProfileKey profileKey = ProfileKeyUtil.profileKeyOrNull(recipient.resolve().getProfileKey());
if (profileKey == null) {
Log.w(TAG, "Recipient profile key is gone!");
return;
}
if (Util.equals(profileAvatar, recipient.resolve().getProfileAvatar())) {
Log.w(TAG, "Already retrieved profile avatar: " + profileAvatar);
return;
}
if (TextUtils.isEmpty(profileAvatar)) {
Log.w(TAG, "Removing profile avatar (no url) for: " + recipient.getId().serialize());
AvatarHelper.delete(context, recipient.getId());
database.setProfileAvatar(recipient.getId(), profileAvatar);
return;
}
File downloadDestination = File.createTempFile("avatar", "jpg", context.getCacheDir());
try {
SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
InputStream avatarStream = receiver.retrieveProfileAvatar(profileAvatar, downloadDestination, profileKey, AvatarHelper.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE);
try {
AvatarHelper.setAvatar(context, recipient.getId(), avatarStream);
if (recipient.isSelf()) {
SignalStore.misc().markHasEverHadAnAvatar();
}
} catch (AssertionError e) {
throw new IOException("Failed to copy stream. Likely a Conscrypt issue.", e);
}
} catch (PushNetworkException e) {
if (e.getCause() instanceof NonSuccessfulResponseCodeException) {
Log.w(TAG, "Removing profile avatar (no image available) for: " + recipient.getId().serialize());
AvatarHelper.delete(context, recipient.getId());
} else {
throw e;
}
} finally {
if (downloadDestination != null)
downloadDestination.delete();
}
database.setProfileAvatar(recipient.getId(), profileAvatar);
}
Aggregations