aboutsummaryrefslogtreecommitdiffstats
path: root/src/standardPush/java/de/pixart/messenger/services/EmojiService.java
blob: 0e5bfadd01fbeeca92f8229d903f1d925fe3751b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package de.pixart.messenger.services;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.core.provider.FontRequest;
import androidx.emoji.text.EmojiCompat;
import androidx.emoji.text.FontRequestEmojiCompatConfig;

import de.pixart.messenger.Config;
import de.pixart.messenger.R;

public class EmojiService {

    private final EmojiCompat.InitCallback initCallback = new EmojiCompat.InitCallback() {
        @Override
        public void onInitialized() {
            super.onInitialized();
            Log.d(Config.LOGTAG, "EmojiService succeeded in loading fonts");

        }

        @Override
        public void onFailed(Throwable throwable) {
            super.onFailed(throwable);
            Log.d(Config.LOGTAG, "EmojiService failed to load fonts", throwable);
        }
    };

    private final Context context;

    public EmojiService(Context context) {
        this.context = context;
    }

    public void init(boolean useBundledEmoji) {
        final FontRequest fontRequest = new FontRequest(
                "com.google.android.gms.fonts",
                "com.google.android.gms",
                "Noto Color Emoji Compat",
                R.array.font_certs);
        FontRequestEmojiCompatConfig fontRequestEmojiCompatConfig = new FontRequestEmojiCompatConfig(context, fontRequest);
        fontRequestEmojiCompatConfig.registerInitCallback(initCallback);
        //On recent Androids we assume to have the latest emojis
        //there are some annoying bugs with emoji compat that make it a safer choice not to use it when possible
        // a) when using the ondemand emoji font (play store) flags don’t work
        // b) the text preview has annoying glitches when the cut of text contains emojis (the emoji will be half visible)
        // c) can trigger a hardware rendering bug https://issuetracker.google.com/issues/67102093
        fontRequestEmojiCompatConfig.setReplaceAll(useBundledEmoji && Build.VERSION.SDK_INT < Build.VERSION_CODES.O);
        EmojiCompat.init(fontRequestEmojiCompatConfig);
    }
}