diff --git a/emojicompat/FileMojiCompat/filemojicompat/build.gradle b/emojicompat/FileMojiCompat/filemojicompat/build.gradle index 28506d027..06a51ee8e 100644 --- a/emojicompat/FileMojiCompat/filemojicompat/build.gradle +++ b/emojicompat/FileMojiCompat/filemojicompat/build.gradle @@ -18,7 +18,7 @@ ext { libraryDescription = 'An EmojiCompat implementation using files from a local file or a file inside your assets directory' siteUrl = 'https://github.com/c1710/blobmoji' gitUrl = 'https://github.com/c1710/blobmoji.git' - libraryVersion = '1.0.1' + libraryVersion = '1.0.2' developerId = 'c1710' developerName = 'Constantin A.' developerEmail = 'c1710.apps@outlook.com' @@ -33,8 +33,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 27 - versionCode 2 - versionName "1.0.1" + versionCode 4 + versionName "1.0.3" } diff --git a/emojicompat/FileMojiCompat/filemojicompat/src/main/java/de/c1710/filemojicompat/FileEmojiCompatConfig.java b/emojicompat/FileMojiCompat/filemojicompat/src/main/java/de/c1710/filemojicompat/FileEmojiCompatConfig.java index 95f073e26..a2001abad 100644 --- a/emojicompat/FileMojiCompat/filemojicompat/src/main/java/de/c1710/filemojicompat/FileEmojiCompatConfig.java +++ b/emojicompat/FileMojiCompat/filemojicompat/src/main/java/de/c1710/filemojicompat/FileEmojiCompatConfig.java @@ -48,7 +48,6 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config { * This boolean indicates whether the fallback solution is used. */ private boolean fallback; - private static final HashMap listeners = new HashMap<>(1); /** * Create a new configuration for this EmojiCompat @@ -71,8 +70,38 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config { // NEW @NonNull File fontFile) { super(new FileMetadataLoader(context, fontFile)); - // The InitRunable needs to find this config if it fails. - listeners.put(fontFile, this::onFailed); + if(fontFile.exists() && fontFile.canRead()) { + try { + // Is it a font? + Typeface typeface = Typeface.createFromFile(fontFile); + // Is it an EmojiCompat font? + /* + Please note that this will possibly cause a race condition. But all in all it's + better to have a chance of detecting such a non-valid font than either having to + wait for a long time or not being able to detect it at all. + However, since this Thread is started immediately, it should be faster than + the initialization process of EmojiCompat itself... + */ + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + new Thread(() -> { + try { + MetadataRepo.create(typeface, new FileInputStream(fontFile)); + } catch (Throwable t) { + fallback = true; + setReplaceAll(false); + Log.w(TAG, "FileEmojiCompatConfig: No valid EmojiCompat font provided. Fallback enabled", t); + } + }).start(); + } + } catch (RuntimeException ex) { + fallback = true; + Log.e(TAG, "FileEmojiCompatConfig: Font file corrupt. Fallback enabled", ex); + } + } else { + // The heck, this is not even an actual _file_! + fallback = true; + } + } @Override @@ -91,8 +120,9 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config { } private void onFailed() { + Log.d(TAG, "onFailed: Could not load font"); fallback = true; - setReplaceAll(false); + super.setReplaceAll(false); } /** @@ -155,13 +185,6 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config { catch (Throwable t) { // Instead of crashing, this one will first try to load the fallback font try { - /* - This solution is very bad but it's impossible to not use such a solution since - EmojiCompat.Config is very restricted. - */ - if(listeners.containsKey(FONT_FILE)) { - listeners.get(FONT_FILE).onFailed(); - } android.util.Log.w(TAG, "Error while loading the font file.", t); final AssetManager assetManager = context.getAssets(); final MetadataRepo resourceIndex =