mirror of
https://github.com/googlefonts/noto-emoji.git
synced 2025-07-08 13:36:40 +00:00
Add the ability to rename the fallback font in FileEmojiCompatConfig
This commit is contained in:
parent
2bd81dce8a
commit
8081cce1cb
2 changed files with 49 additions and 24 deletions
|
@ -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.6'
|
libraryVersion = '1.0.7'
|
||||||
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 6
|
versionCode 7
|
||||||
versionName "1.0.6"
|
versionName "1.0.7"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,10 +31,6 @@ import android.util.Log;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple implementation of EmojiCompat.Config using typeface files.
|
* A simple implementation of EmojiCompat.Config using typeface files.
|
||||||
|
@ -54,6 +50,10 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
* Indicates whether all emojis should be replaced when the fallback font is used.
|
* Indicates whether all emojis should be replaced when the fallback font is used.
|
||||||
*/
|
*/
|
||||||
private boolean replaceAllOnFallback = false;
|
private boolean replaceAllOnFallback = false;
|
||||||
|
/**
|
||||||
|
* The default name of the fallback font
|
||||||
|
*/
|
||||||
|
private static final String FONT_FALLBACK = "NoEmojiCompat.ttf";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new configuration for this EmojiCompat
|
* Create a new configuration for this EmojiCompat
|
||||||
|
@ -64,7 +64,21 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
// NEW
|
// NEW
|
||||||
@NonNull String path) {
|
@NonNull String path) {
|
||||||
// This one is obviously new
|
// This one is obviously new
|
||||||
this(context, new File(path));
|
this(context, path, FONT_FALLBACK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new configuration for this EmojiCompat
|
||||||
|
* @param path The file name/path of the requested font
|
||||||
|
* @param context Context instance
|
||||||
|
* @param fallbackFont The asset path of the fallback font
|
||||||
|
*/
|
||||||
|
public FileEmojiCompatConfig(@NonNull Context context,
|
||||||
|
// NEW
|
||||||
|
@NonNull String path,
|
||||||
|
@Nullable String fallbackFont) {
|
||||||
|
// This one is obviously new
|
||||||
|
this(context, new File(path), fallbackFont);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -75,7 +89,22 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
public FileEmojiCompatConfig(@NonNull Context context,
|
public FileEmojiCompatConfig(@NonNull Context context,
|
||||||
// NEW
|
// NEW
|
||||||
@Nullable File fontFile) {
|
@Nullable File fontFile) {
|
||||||
super(new FileMetadataLoader(context, fontFile));
|
this(context, fontFile, FONT_FALLBACK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new configuration for this EmojiCompat based on a file
|
||||||
|
* @param context Context instance
|
||||||
|
* @param fontFile The file containing the EmojiCompat font
|
||||||
|
* @param fallbackFont The asset path of the fallback font
|
||||||
|
*/
|
||||||
|
public FileEmojiCompatConfig(@NonNull Context context,
|
||||||
|
// NEW
|
||||||
|
@Nullable File fontFile,
|
||||||
|
@Nullable String fallbackFont) {
|
||||||
|
super(new FileMetadataLoader(context,
|
||||||
|
fontFile,
|
||||||
|
fallbackFont != null ? fallbackFont : FONT_FALLBACK));
|
||||||
if(fontFile != null && fontFile.exists() && fontFile.canRead()) {
|
if(fontFile != null && fontFile.exists() && fontFile.canRead()) {
|
||||||
try {
|
try {
|
||||||
// Is it a font?
|
// Is it a font?
|
||||||
|
@ -137,12 +166,6 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onFailed() {
|
|
||||||
Log.d(TAG, "onFailed: Could not load font");
|
|
||||||
fallback = true;
|
|
||||||
super.setReplaceAll(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the MetadataLoader. Derived from BundledMetadataLoader but with
|
* This is the MetadataLoader. Derived from BundledMetadataLoader but with
|
||||||
* the addition of a custom file name.
|
* the addition of a custom file name.
|
||||||
|
@ -151,13 +174,16 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
// NEW
|
// NEW
|
||||||
private final File fontFile;
|
private final File fontFile;
|
||||||
|
private final String fallbackFont;
|
||||||
|
|
||||||
private FileMetadataLoader(@NonNull Context context,
|
private FileMetadataLoader(@NonNull Context context,
|
||||||
// NEW
|
// NEW
|
||||||
@Nullable File fontFile) {
|
@Nullable File fontFile,
|
||||||
|
@NonNull String fallbackFont) {
|
||||||
this.mContext = context.getApplicationContext();
|
this.mContext = context.getApplicationContext();
|
||||||
// NEW
|
// NEW
|
||||||
this.fontFile = fontFile;
|
this.fontFile = fontFile;
|
||||||
|
this.fallbackFont = fallbackFont;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -166,7 +192,7 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
@RequiresApi(19)
|
@RequiresApi(19)
|
||||||
public void load(@NonNull EmojiCompat.MetadataRepoLoaderCallback loaderCallback) {
|
public void load(@NonNull EmojiCompat.MetadataRepoLoaderCallback loaderCallback) {
|
||||||
//Preconditions.checkNotNull(loaderCallback, "loaderCallback cannot be null");
|
//Preconditions.checkNotNull(loaderCallback, "loaderCallback cannot be null");
|
||||||
final InitRunnable runnable = new InitRunnable(mContext, loaderCallback, fontFile);
|
final InitRunnable runnable = new InitRunnable(mContext, loaderCallback, fontFile, fallbackFont);
|
||||||
final Thread thread = new Thread(runnable);
|
final Thread thread = new Thread(runnable);
|
||||||
thread.setDaemon(false);
|
thread.setDaemon(false);
|
||||||
thread.start();
|
thread.start();
|
||||||
|
@ -175,8 +201,9 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
|
|
||||||
@RequiresApi(19)
|
@RequiresApi(19)
|
||||||
private static class InitRunnable implements Runnable {
|
private static class InitRunnable implements Runnable {
|
||||||
// The font name is assigned in the constructor.
|
// The font names are assigned in the constructor.
|
||||||
private final File FONT_FILE;
|
private final File FONT_FILE;
|
||||||
|
private final String FONT_FALLBACK;
|
||||||
// Slightly different variable names
|
// Slightly different variable names
|
||||||
private final EmojiCompat.MetadataRepoLoaderCallback loaderCallback;
|
private final EmojiCompat.MetadataRepoLoaderCallback loaderCallback;
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
@ -184,11 +211,13 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
private InitRunnable(final Context context,
|
private InitRunnable(final Context context,
|
||||||
final EmojiCompat.MetadataRepoLoaderCallback loaderCallback,
|
final EmojiCompat.MetadataRepoLoaderCallback loaderCallback,
|
||||||
// NEW parameter
|
// NEW parameter
|
||||||
final File FONT_FILE) {
|
final File FONT_FILE,
|
||||||
|
final String FONT_FALLBACK) {
|
||||||
// This has been changed a bit in order to get some consistency
|
// This has been changed a bit in order to get some consistency
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.loaderCallback = loaderCallback;
|
this.loaderCallback = loaderCallback;
|
||||||
this.FONT_FILE = FONT_FILE;
|
this.FONT_FILE = FONT_FILE;
|
||||||
|
this.FONT_FALLBACK = FONT_FALLBACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -206,16 +235,12 @@ public class FileEmojiCompatConfig extends EmojiCompat.Config {
|
||||||
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 =
|
||||||
MetadataRepo.create(assetManager, "NoEmojiCompat.ttf");
|
MetadataRepo.create(assetManager, FONT_FALLBACK);
|
||||||
loaderCallback.onLoaded(resourceIndex);
|
loaderCallback.onLoaded(resourceIndex);
|
||||||
} catch (Throwable t2) {
|
} catch (Throwable t2) {
|
||||||
loaderCallback.onFailed(t);
|
loaderCallback.onFailed(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EmojiFontFailListener {
|
|
||||||
void onFailed();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue