Search in sources :

Example 1 with StickerRemoteUri

use of org.thoughtcrime.securesms.stickers.StickerRemoteUri 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.");
}
Also used : SignalServiceStickerManifest(org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) StickerRemoteUri(org.thoughtcrime.securesms.stickers.StickerRemoteUri) Attachment(org.thoughtcrime.securesms.attachments.Attachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) StickerInfo(org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest.StickerInfo) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with StickerRemoteUri

use of org.thoughtcrime.securesms.stickers.StickerRemoteUri in project Signal-Android by signalapp.

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.");
}
Also used : SignalServiceStickerManifest(org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) StickerRemoteUri(org.thoughtcrime.securesms.stickers.StickerRemoteUri) Attachment(org.thoughtcrime.securesms.attachments.Attachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) StickerInfo(org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest.StickerInfo) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Bitmap (android.graphics.Bitmap)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Attachment (org.thoughtcrime.securesms.attachments.Attachment)2 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)2 StickerRemoteUri (org.thoughtcrime.securesms.stickers.StickerRemoteUri)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 SignalServiceMessageReceiver (org.whispersystems.signalservice.api.SignalServiceMessageReceiver)2 SignalServiceStickerManifest (org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest)2 StickerInfo (org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest.StickerInfo)2