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 android.support.text.emoji.bundled;
18
19import android.content.Context;
20import android.content.res.AssetManager;
21import android.support.annotation.NonNull;
22import android.support.annotation.RequiresApi;
23import android.support.text.emoji.EmojiCompat;
24import android.support.text.emoji.MetadataRepo;
25import android.support.v4.util.Preconditions;
26
27/**
28 * {@link EmojiCompat.Config} implementation that loads the metadata using AssetManager and
29 * bundled resources.
30 * <p/>
31 * <pre><code>EmojiCompat.init(new BundledEmojiCompatConfig(context));</code></pre>
32 *
33 * @see EmojiCompat
34 */
35public class BundledEmojiCompatConfig extends EmojiCompat.Config {
36
37    /**
38     * Default constructor.
39     *
40     * @param context Context instance
41     */
42    public BundledEmojiCompatConfig(@NonNull Context context) {
43        super(new BundledMetadataLoader(context));
44    }
45
46    private static class BundledMetadataLoader implements EmojiCompat.MetadataRepoLoader {
47        private final Context mContext;
48
49        private BundledMetadataLoader(@NonNull Context context) {
50            mContext = context.getApplicationContext();
51        }
52
53        @Override
54        @RequiresApi(19)
55        public void load(@NonNull EmojiCompat.MetadataRepoLoaderCallback loaderCallback) {
56            Preconditions.checkNotNull(loaderCallback, "loaderCallback cannot be null");
57            final InitRunnable runnable = new InitRunnable(mContext, loaderCallback);
58            final Thread thread = new Thread(runnable);
59            thread.setDaemon(false);
60            thread.start();
61        }
62    }
63
64    @RequiresApi(19)
65    private static class InitRunnable implements Runnable {
66        private static final String FONT_NAME = "NotoColorEmojiCompat.ttf";
67        private final EmojiCompat.MetadataRepoLoaderCallback mLoaderCallback;
68        private final Context mContext;
69
70        private InitRunnable(final Context context,
71                final EmojiCompat.MetadataRepoLoaderCallback loaderCallback) {
72            mContext = context;
73            mLoaderCallback = loaderCallback;
74        }
75
76        @Override
77        public void run() {
78            try {
79                final AssetManager assetManager = mContext.getAssets();
80                final MetadataRepo resourceIndex = MetadataRepo.create(assetManager, FONT_NAME);
81                mLoaderCallback.onLoaded(resourceIndex);
82            } catch (Throwable t) {
83                mLoaderCallback.onFailed(t);
84            }
85        }
86    }
87}
88