Search in sources :

Example 1 with FutureTaskListener

use of org.thoughtcrime.securesms.util.FutureTaskListener in project Signal-Android by signalapp.

the class EmojiProvider method getEmojiDrawable.

/**
 * Gets an EmojiDrawable from the Page Cache
 *
 * @param context         Context object used in reading and writing from disk
 * @param drawInfo        Information about the emoji being displayed
 * @param onEmojiLoaded   Runnable which will trigger when an emoji is loaded from disk
 */
@Nullable
private static Drawable getEmojiDrawable(@NonNull Context context, @Nullable EmojiDrawInfo drawInfo, @Nullable Runnable onEmojiLoaded, boolean jumboEmoji) {
    if (drawInfo == null) {
        return null;
    }
    final int lowMemoryDecodeScale = DeviceProperties.isLowMemoryDevice(context) ? 2 : 1;
    final EmojiSource source = EmojiSource.getLatest();
    final EmojiDrawable drawable = new EmojiDrawable(source, drawInfo, lowMemoryDecodeScale);
    final AtomicBoolean jumboLoaded = new AtomicBoolean(false);
    EmojiPageCache.LoadResult loadResult = EmojiPageCache.INSTANCE.load(context, drawInfo.getPage(), lowMemoryDecodeScale);
    if (loadResult instanceof EmojiPageCache.LoadResult.Immediate) {
        ThreadUtil.runOnMain(() -> drawable.setBitmap(((EmojiPageCache.LoadResult.Immediate) loadResult).getBitmap()));
    } else if (loadResult instanceof EmojiPageCache.LoadResult.Async) {
        ((EmojiPageCache.LoadResult.Async) loadResult).getTask().addListener(new FutureTaskListener<Bitmap>() {

            @Override
            public void onSuccess(Bitmap result) {
                ThreadUtil.runOnMain(() -> {
                    if (!jumboLoaded.get()) {
                        drawable.setBitmap(result);
                        if (onEmojiLoaded != null) {
                            onEmojiLoaded.run();
                        }
                    }
                });
            }

            @Override
            public void onFailure(ExecutionException exception) {
                Log.d(TAG, "Failed to load emoji bitmap resource", exception);
            }
        });
    } else {
        throw new IllegalStateException("Unexpected subclass " + loadResult.getClass());
    }
    if (jumboEmoji && drawInfo.getJumboSheet() != null) {
        JumboEmoji.LoadResult result = JumboEmoji.loadJumboEmoji(context, drawInfo);
        if (result instanceof JumboEmoji.LoadResult.Immediate) {
            ThreadUtil.runOnMain(() -> {
                jumboLoaded.set(true);
                drawable.setSingleBitmap(((JumboEmoji.LoadResult.Immediate) result).getBitmap());
            });
        } else if (result instanceof JumboEmoji.LoadResult.Async) {
            ((JumboEmoji.LoadResult.Async) result).getTask().addListener(new FutureTaskListener<Bitmap>() {

                @Override
                public void onSuccess(Bitmap result) {
                    ThreadUtil.runOnMain(() -> {
                        jumboLoaded.set(true);
                        drawable.setSingleBitmap(result);
                        if (onEmojiLoaded != null) {
                            onEmojiLoaded.run();
                        }
                    });
                }

                @Override
                public void onFailure(ExecutionException exception) {
                    if (exception.getCause() instanceof JumboEmoji.CannotAutoDownload) {
                        Log.i(TAG, "Download restrictions are preventing jumbomoji use");
                    } else {
                        Log.d(TAG, "Failed to load jumbo emoji bitmap resource", exception);
                    }
                }
            });
        }
        return drawable;
    }
    return drawable;
}
Also used : FutureTaskListener(org.thoughtcrime.securesms.util.FutureTaskListener) Paint(android.graphics.Paint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Bitmap(android.graphics.Bitmap) EmojiSource(org.thoughtcrime.securesms.emoji.EmojiSource) JumboEmoji(org.thoughtcrime.securesms.emoji.JumboEmoji) EmojiPageCache(org.thoughtcrime.securesms.emoji.EmojiPageCache) ExecutionException(java.util.concurrent.ExecutionException) Nullable(androidx.annotation.Nullable)

Example 2 with FutureTaskListener

use of org.thoughtcrime.securesms.util.FutureTaskListener in project Signal-Android by WhisperSystems.

the class EmojiProvider method getEmojiDrawable.

/**
 * Gets an EmojiDrawable from the Page Cache
 *
 * @param context         Context object used in reading and writing from disk
 * @param drawInfo        Information about the emoji being displayed
 * @param onEmojiLoaded   Runnable which will trigger when an emoji is loaded from disk
 */
@Nullable
private static Drawable getEmojiDrawable(@NonNull Context context, @Nullable EmojiDrawInfo drawInfo, @Nullable Runnable onEmojiLoaded, boolean jumboEmoji) {
    if (drawInfo == null) {
        return null;
    }
    final int lowMemoryDecodeScale = DeviceProperties.isLowMemoryDevice(context) ? 2 : 1;
    final EmojiSource source = EmojiSource.getLatest();
    final EmojiDrawable drawable = new EmojiDrawable(source, drawInfo, lowMemoryDecodeScale);
    final AtomicBoolean jumboLoaded = new AtomicBoolean(false);
    EmojiPageCache.LoadResult loadResult = EmojiPageCache.INSTANCE.load(context, drawInfo.getPage(), lowMemoryDecodeScale);
    if (loadResult instanceof EmojiPageCache.LoadResult.Immediate) {
        ThreadUtil.runOnMain(() -> drawable.setBitmap(((EmojiPageCache.LoadResult.Immediate) loadResult).getBitmap()));
    } else if (loadResult instanceof EmojiPageCache.LoadResult.Async) {
        ((EmojiPageCache.LoadResult.Async) loadResult).getTask().addListener(new FutureTaskListener<Bitmap>() {

            @Override
            public void onSuccess(Bitmap result) {
                ThreadUtil.runOnMain(() -> {
                    if (!jumboLoaded.get()) {
                        drawable.setBitmap(result);
                        if (onEmojiLoaded != null) {
                            onEmojiLoaded.run();
                        }
                    }
                });
            }

            @Override
            public void onFailure(ExecutionException exception) {
                Log.d(TAG, "Failed to load emoji bitmap resource", exception);
            }
        });
    } else {
        throw new IllegalStateException("Unexpected subclass " + loadResult.getClass());
    }
    if (jumboEmoji && drawInfo.getJumboSheet() != null) {
        JumboEmoji.LoadResult result = JumboEmoji.loadJumboEmoji(context, drawInfo);
        if (result instanceof JumboEmoji.LoadResult.Immediate) {
            ThreadUtil.runOnMain(() -> {
                jumboLoaded.set(true);
                drawable.setSingleBitmap(((JumboEmoji.LoadResult.Immediate) result).getBitmap());
            });
        } else if (result instanceof JumboEmoji.LoadResult.Async) {
            ((JumboEmoji.LoadResult.Async) result).getTask().addListener(new FutureTaskListener<Bitmap>() {

                @Override
                public void onSuccess(Bitmap result) {
                    ThreadUtil.runOnMain(() -> {
                        jumboLoaded.set(true);
                        drawable.setSingleBitmap(result);
                        if (onEmojiLoaded != null) {
                            onEmojiLoaded.run();
                        }
                    });
                }

                @Override
                public void onFailure(ExecutionException exception) {
                    if (exception.getCause() instanceof JumboEmoji.CannotAutoDownload) {
                        Log.i(TAG, "Download restrictions are preventing jumbomoji use");
                    } else {
                        Log.d(TAG, "Failed to load jumbo emoji bitmap resource", exception);
                    }
                }
            });
        }
        return drawable;
    }
    return drawable;
}
Also used : FutureTaskListener(org.thoughtcrime.securesms.util.FutureTaskListener) Paint(android.graphics.Paint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Bitmap(android.graphics.Bitmap) EmojiSource(org.thoughtcrime.securesms.emoji.EmojiSource) JumboEmoji(org.thoughtcrime.securesms.emoji.JumboEmoji) EmojiPageCache(org.thoughtcrime.securesms.emoji.EmojiPageCache) ExecutionException(java.util.concurrent.ExecutionException) Nullable(androidx.annotation.Nullable)

Aggregations

Bitmap (android.graphics.Bitmap)2 Paint (android.graphics.Paint)2 Nullable (androidx.annotation.Nullable)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 EmojiPageCache (org.thoughtcrime.securesms.emoji.EmojiPageCache)2 EmojiSource (org.thoughtcrime.securesms.emoji.EmojiSource)2 JumboEmoji (org.thoughtcrime.securesms.emoji.JumboEmoji)2 FutureTaskListener (org.thoughtcrime.securesms.util.FutureTaskListener)2