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 17 package android.support.text.emoji.bundled; 18 19 import android.content.Context; 20 import android.content.res.AssetManager; 21 import android.support.annotation.NonNull; 22 import android.support.annotation.RequiresApi; 23 import android.support.text.emoji.EmojiCompat; 24 import android.support.text.emoji.MetadataRepo; 25 import 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 */ 35 public class BundledEmojiCompatConfig extends EmojiCompat.Config { 36 37 /** 38 * Default constructor. 39 * 40 * @param context Context instance 41 */ BundledEmojiCompatConfig(@onNull Context context)42 public BundledEmojiCompatConfig(@NonNull Context context) { 43 super(new BundledMetadataLoader(context)); 44 } 45 46 private static class BundledMetadataLoader implements EmojiCompat.MetadataLoader { 47 private final Context mContext; 48 BundledMetadataLoader(@onNull Context context)49 private BundledMetadataLoader(@NonNull Context context) { 50 mContext = context.getApplicationContext(); 51 } 52 53 @Override 54 @RequiresApi(19) load(@onNull EmojiCompat.LoaderCallback loaderCallback)55 public void load(@NonNull EmojiCompat.LoaderCallback 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.LoaderCallback mLoaderCallback; 68 private final Context mContext; 69 InitRunnable(final Context context, final EmojiCompat.LoaderCallback loaderCallback)70 private InitRunnable(final Context context, 71 final EmojiCompat.LoaderCallback loaderCallback) { 72 mContext = context; 73 mLoaderCallback = loaderCallback; 74 } 75 76 @Override run()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