1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.emoji.bundled;
18
19import android.content.Context;
20import android.content.res.AssetManager;
21
22import androidx.annotation.NonNull;
23import androidx.annotation.RequiresApi;
24import androidx.core.util.Preconditions;
25import androidx.emoji.text.EmojiCompat;
26import androidx.emoji.text.MetadataRepo;
27
28/**
29 * {@link EmojiCompat.Config} implementation that loads the metadata using AssetManager and
30 * bundled resources.
31 * <p/>
32 * <pre><code>EmojiCompat.init(new BundledEmojiCompatConfig(context));</code></pre>
33 *
34 * @see EmojiCompat
35 */
36public class BundledEmojiCompatConfig extends EmojiCompat.Config {
37
38    /**
39     * Default constructor.
40     *
41     * @param context Context instance
42     */
43    public BundledEmojiCompatConfig(@NonNull Context context) {
44        super(new BundledMetadataLoader(context));
45    }
46
47    private static class BundledMetadataLoader implements EmojiCompat.MetadataRepoLoader {
48        private final Context mContext;
49
50        private BundledMetadataLoader(@NonNull Context context) {
51            mContext = context.getApplicationContext();
52        }
53
54        @Override
55        @RequiresApi(19)
56        public void load(@NonNull EmojiCompat.MetadataRepoLoaderCallback loaderCallback) {
57            Preconditions.checkNotNull(loaderCallback, "loaderCallback cannot be null");
58            final InitRunnable runnable = new InitRunnable(mContext, loaderCallback);
59            final Thread thread = new Thread(runnable);
60            thread.setDaemon(false);
61            thread.start();
62        }
63    }
64
65    @RequiresApi(19)
66    private static class InitRunnable implements Runnable {
67        private static final String FONT_NAME = "NotoColorEmojiCompat.ttf";
68        private final EmojiCompat.MetadataRepoLoaderCallback mLoaderCallback;
69        private final Context mContext;
70
71        private InitRunnable(final Context context,
72                final EmojiCompat.MetadataRepoLoaderCallback loaderCallback) {
73            mContext = context;
74            mLoaderCallback = loaderCallback;
75        }
76
77        @Override
78        public void run() {
79            try {
80                final AssetManager assetManager = mContext.getAssets();
81                final MetadataRepo resourceIndex = MetadataRepo.create(assetManager, FONT_NAME);
82                mLoaderCallback.onLoaded(resourceIndex);
83            } catch (Throwable t) {
84                mLoaderCallback.onFailed(t);
85            }
86        }
87    }
88}
89