Search in sources :

Example 1 with JumboEmoji

use of org.thoughtcrime.securesms.emoji.JumboEmoji in project Signal-Android by WhisperSystems.

the class EmojiProvider method getEmojiDrawableSync.

/**
 * Gets an EmojiDrawable from the Page Cache synchronously
 *
 * @param context         Context object used in reading and writing from disk
 * @param drawInfo        Information about the emoji being displayed
 */
@Nullable
private static Drawable getEmojiDrawableSync(@NonNull Context context, @Nullable EmojiDrawInfo drawInfo, boolean jumboEmoji) {
    ThreadUtil.assertNotMainThread();
    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);
    Bitmap bitmap = null;
    if (jumboEmoji && drawInfo.getJumboSheet() != null) {
        JumboEmoji.LoadResult result = JumboEmoji.loadJumboEmoji(context, drawInfo);
        if (result instanceof JumboEmoji.LoadResult.Immediate) {
            bitmap = ((JumboEmoji.LoadResult.Immediate) result).getBitmap();
        } else if (result instanceof JumboEmoji.LoadResult.Async) {
            try {
                bitmap = ((JumboEmoji.LoadResult.Async) result).getTask().get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException 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);
                }
            }
        }
        if (bitmap != null) {
            drawable.setSingleBitmap(bitmap);
        }
    }
    if (!jumboEmoji || bitmap == null) {
        EmojiPageCache.LoadResult loadResult = EmojiPageCache.INSTANCE.load(context, drawInfo.getPage(), lowMemoryDecodeScale);
        if (loadResult instanceof EmojiPageCache.LoadResult.Immediate) {
            Log.d(TAG, "Cached emoji page: " + drawInfo.getPage().getUri().toString());
            bitmap = ((EmojiPageCache.LoadResult.Immediate) loadResult).getBitmap();
        } else if (loadResult instanceof EmojiPageCache.LoadResult.Async) {
            Log.d(TAG, "Loading emoji page: " + drawInfo.getPage().getUri().toString());
            try {
                bitmap = ((EmojiPageCache.LoadResult.Async) loadResult).getTask().get(2, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException exception) {
                Log.d(TAG, "Failed to load emoji bitmap resource", exception);
            }
        } else {
            throw new IllegalStateException("Unexpected subclass " + loadResult.getClass());
        }
        drawable.setBitmap(bitmap);
    }
    return drawable;
}
Also used : Paint(android.graphics.Paint) Bitmap(android.graphics.Bitmap) EmojiSource(org.thoughtcrime.securesms.emoji.EmojiSource) JumboEmoji(org.thoughtcrime.securesms.emoji.JumboEmoji) EmojiPageCache(org.thoughtcrime.securesms.emoji.EmojiPageCache) Nullable(androidx.annotation.Nullable)

Example 2 with JumboEmoji

use of org.thoughtcrime.securesms.emoji.JumboEmoji 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 3 with JumboEmoji

use of org.thoughtcrime.securesms.emoji.JumboEmoji in project Signal-Android by signalapp.

the class EmojiProvider method getEmojiDrawableSync.

/**
 * Gets an EmojiDrawable from the Page Cache synchronously
 *
 * @param context         Context object used in reading and writing from disk
 * @param drawInfo        Information about the emoji being displayed
 */
@Nullable
private static Drawable getEmojiDrawableSync(@NonNull Context context, @Nullable EmojiDrawInfo drawInfo, boolean jumboEmoji) {
    ThreadUtil.assertNotMainThread();
    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);
    Bitmap bitmap = null;
    if (jumboEmoji && drawInfo.getJumboSheet() != null) {
        JumboEmoji.LoadResult result = JumboEmoji.loadJumboEmoji(context, drawInfo);
        if (result instanceof JumboEmoji.LoadResult.Immediate) {
            bitmap = ((JumboEmoji.LoadResult.Immediate) result).getBitmap();
        } else if (result instanceof JumboEmoji.LoadResult.Async) {
            try {
                bitmap = ((JumboEmoji.LoadResult.Async) result).getTask().get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException 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);
                }
            }
        }
        if (bitmap != null) {
            drawable.setSingleBitmap(bitmap);
        }
    }
    if (!jumboEmoji || bitmap == null) {
        EmojiPageCache.LoadResult loadResult = EmojiPageCache.INSTANCE.load(context, drawInfo.getPage(), lowMemoryDecodeScale);
        if (loadResult instanceof EmojiPageCache.LoadResult.Immediate) {
            Log.d(TAG, "Cached emoji page: " + drawInfo.getPage().getUri().toString());
            bitmap = ((EmojiPageCache.LoadResult.Immediate) loadResult).getBitmap();
        } else if (loadResult instanceof EmojiPageCache.LoadResult.Async) {
            Log.d(TAG, "Loading emoji page: " + drawInfo.getPage().getUri().toString());
            try {
                bitmap = ((EmojiPageCache.LoadResult.Async) loadResult).getTask().get(2, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException exception) {
                Log.d(TAG, "Failed to load emoji bitmap resource", exception);
            }
        } else {
            throw new IllegalStateException("Unexpected subclass " + loadResult.getClass());
        }
        drawable.setBitmap(bitmap);
    }
    return drawable;
}
Also used : Paint(android.graphics.Paint) Bitmap(android.graphics.Bitmap) EmojiSource(org.thoughtcrime.securesms.emoji.EmojiSource) JumboEmoji(org.thoughtcrime.securesms.emoji.JumboEmoji) EmojiPageCache(org.thoughtcrime.securesms.emoji.EmojiPageCache) Nullable(androidx.annotation.Nullable)

Example 4 with JumboEmoji

use of org.thoughtcrime.securesms.emoji.JumboEmoji 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)4 Paint (android.graphics.Paint)4 Nullable (androidx.annotation.Nullable)4 EmojiPageCache (org.thoughtcrime.securesms.emoji.EmojiPageCache)4 EmojiSource (org.thoughtcrime.securesms.emoji.EmojiSource)4 JumboEmoji (org.thoughtcrime.securesms.emoji.JumboEmoji)4 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FutureTaskListener (org.thoughtcrime.securesms.util.FutureTaskListener)2