Add another bad solution to detect non-EmojiCompat-fonts

This commit is contained in:
Constantin A 2018-05-01 10:20:41 +02:00
parent 4edb536bc5
commit c05eff4eb1
2 changed files with 37 additions and 14 deletions

View file

@ -18,7 +18,7 @@ ext {
libraryDescription = 'An EmojiCompat implementation using files from a local file or a file inside your assets directory' libraryDescription = 'An EmojiCompat implementation using files from a local file or a file inside your assets directory'
siteUrl = 'https://github.com/c1710/blobmoji' siteUrl = 'https://github.com/c1710/blobmoji'
gitUrl = 'https://github.com/c1710/blobmoji.git' gitUrl = 'https://github.com/c1710/blobmoji.git'
libraryVersion = '1.0.1' libraryVersion = '1.0.2'
developerId = 'c1710' developerId = 'c1710'
developerName = 'Constantin A.' developerName = 'Constantin A.'
developerEmail = 'c1710.apps@outlook.com' developerEmail = 'c1710.apps@outlook.com'
@ -33,8 +33,8 @@ android {
defaultConfig { defaultConfig {
minSdkVersion 14 minSdkVersion 14
targetSdkVersion 27 targetSdkVersion 27
versionCode 2 versionCode 4
versionName "1.0.1" versionName "1.0.3"
} }

View file

@ -48,7 +48,6 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
* This boolean indicates whether the fallback solution is used. * This boolean indicates whether the fallback solution is used.
*/ */
private boolean fallback; private boolean fallback;
private static final HashMap<File, InitRunnable.EmojiFontFailListener> listeners = new HashMap<>(1);
/** /**
* Create a new configuration for this EmojiCompat * Create a new configuration for this EmojiCompat
@ -71,8 +70,38 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
// NEW // NEW
@NonNull File fontFile) { @NonNull File fontFile) {
super(new FileMetadataLoader(context, fontFile)); super(new FileMetadataLoader(context, fontFile));
// The InitRunable needs to find this config if it fails. if(fontFile.exists() && fontFile.canRead()) {
listeners.put(fontFile, this::onFailed); 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 @Override
@ -91,8 +120,9 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
} }
private void onFailed() { private void onFailed() {
Log.d(TAG, "onFailed: Could not load font");
fallback = true; fallback = true;
setReplaceAll(false); super.setReplaceAll(false);
} }
/** /**
@ -155,13 +185,6 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
catch (Throwable t) { catch (Throwable t) {
// Instead of crashing, this one will first try to load the fallback font // Instead of crashing, this one will first try to load the fallback font
try { 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); android.util.Log.w(TAG, "Error while loading the font file.", t);
final AssetManager assetManager = context.getAssets(); final AssetManager assetManager = context.getAssets();
final MetadataRepo resourceIndex = final MetadataRepo resourceIndex =