main/emojicompat/FileMojiCompat
2018-05-05 15:19:46 +02:00
..
filemojicompat Ability added to replace all emojis when using fallback 2018-05-05 15:19:46 +02:00
gradle/wrapper Update some files for FileMojiCompat 2018-05-01 08:41:18 +02:00
build.gradle Update some files for FileMojiCompat 2018-05-01 08:41:18 +02:00
gradle.properties Update some files for FileMojiCompat 2018-05-01 08:41:18 +02:00
gradlew Add sources for FileMojiCompat library 2018-04-29 18:07:44 +02:00
gradlew.bat Update some files for FileMojiCompat 2018-05-01 08:41:18 +02:00
README.md Update README.md 2018-04-29 19:00:39 +02:00
settings.gradle Update some files for FileMojiCompat 2018-05-01 08:41:18 +02:00

FileMojiCompat

What is this?

This is a library providing an easy solution to use EmojiCompat with fonts that are either stored in the assets-folder with another name than NotoColorEmojiCompat.ttf or you can use EmojiCompat fonts which are stored anywhere on the device's local storage.

How do I get this library?

That's relatively easy: Just add the following line to your module's build.gradle inside dependencies:

implementation 'de.c1710:filemojicompat:1.0.1'

How do I use it?

There are two different methods included in this library:

  1. AssetEmojiCompatConfig

    You can use this just like you would usually use BundledEmojiCompatConfig except for the detail that you can choose the font's name as the second parameter.
    Example:
    EmojiCompat.Config config = new AssetEmojiCompatConfig(getContext(), "Blobmoji.ttf");
    
    This will create a new EmojiCompat configuration using the file provided in assets/Blobmoji.ttf.
  2. FileEmojiCOmpatConfig

    This is the more complex and interesting option.
    Instead of providing a short String containing the font's name, you can provide a File (or the String containing the full path of it).
    This will try to load the font from this path.
    In case it gets into any trouble - let's say because of missing permissions or a non-existent file, it will fallback to using no EmojiCOmpat at all.
    (Technically this is wrong as leaving EmojiCompat uninitialized would crash the components using it. There's an explanation below).
    Example:
    Context context = getContext();
    File fontFile = new File(context.getExternalFilesDir(null), "emoji/Blobmoji.ttf"),
    EmojiCompat.Config config = new FileEmojiCompatConfig(context, fontFile);
    
    In this example, your app would try to load the font file Blobmoji.ttf located at /storage/emulated/0/Android/data/[your.app.package]/files/Blobmoji.ttf. If this file is not available, EmojiCompat won't be visible.
    Hint: When accessing a file inside your private directory (i.e. the one used in the example), you don't need storage permissions.
    However, if you want to load the font from somewhere else - let's say the Download directory, you will need storage permissions.
    But don't worry (too much) - your app won't crash (probably) as this missing file is just being ignored in this case.

What happens if I don't provide the font file in FileEmojiCompatConfig?

In this case, there won't be a visible difference to not using EmojiCompat.

But what does that mean exactly?

If you take a look at EmojiCompat itself, you'll notice that it isn't build with missing fonts in mind. If anything happens, onLoadFailed is called and EmojiCompat crashes - and so do all components relying on it.
To prevent this case, FileEmojiCompatConfig includes a fallback solution - inside the assets folder, you'll find a file called NoEmojiCompat.ttf which is much smaller than most of the EmojiCompat font files (~40kiB). That's because it only includes 10 characters which are the flags for China, Germany, Spain, France, United Kingdom, Italy, Japan, South Korea, Russia, USA.
These are the flags which where originally included in Noto Emoji and they are only used to fill the font file with something.

WIll my users see these flags when the fallback font is used?

Yes, they will. But only if their device either doesn't support these flags (which is basically impossible) or if they use a device which already uses these assets as their default emojis. They won't replace any existing emojis.

But I did setReplaceAll(true)?!

This won't change anything as FileEmojiCompatConfig will detect if the font file doesn't exist (or can't be read) and it will simply ignore setReplaceAll(true). However, this is currently not the case if something else goes wrong, e.g. if the file is not an EmojiCompat font.
But even in this case only these flags are affected.