EmojiCompat
@AnyThread
public class EmojiCompat
Main class to keep Android devices up to date with the newest emojis by adding EmojiSpans to a given CharSequence. It is a singleton class that can be configured using a EmojiCompat.Config instance.
init function before it can process a CharSequence.
EmojiCompat.init(/* a config instance */);
EmojiCompat.Config for more configuration parameters. Once init is called a singleton instance will be created. Any call after that will not create a new instance and will return immediately. During initialization information about emojis is loaded on a background thread. Before the EmojiCompat instance is initialized, calls to functions such as process will throw an exception. You can use the InitCallback class to be informed about the state of initialization. After initialization the get function can be used to get the configured instance and the process function can be used to update a CharSequence with emoji EmojiSpans.
CharSequence processedSequence = EmojiCompat.get().process("some string")Summary
Nested types |
|---|
public abstract class EmojiCompat.ConfigConfiguration class for EmojiCompat. |
public interface EmojiCompat.GlyphCheckerInterface to check if a given emoji exists on the system. |
public abstract class EmojiCompat.InitCallbackListener class for the initialization of the EmojiCompat. |
public interface EmojiCompat.MetadataRepoLoaderInterface to load emoji metadata. |
public abstract class EmojiCompat.MetadataRepoLoaderCallbackCallback to inform EmojiCompat about the state of the metadata load. |
Constants |
|
|---|---|
static final String |
EDITOR_INFO_METAVERSION_KEY = "android.support.text.emoji.emojiCompat_metadataVersion"Key in |
static final String |
EDITOR_INFO_REPLACE_ALL_KEY = "android.support.text.emoji.emojiCompat_replaceAll"Key in |
static final int |
EmojiCompat instance is constructed, however the initialization did not start yet. |
static final int |
An unrecoverable error occurred during initialization of EmojiCompat. |
static final int |
EmojiCompat is initializing. |
static final int |
EmojiCompat successfully initialized. |
static final int |
|
static final int |
|
static final int |
Replace strategy to add |
static final int |
Replace strategy that uses the value given in |
static final int |
Replace strategy to add |
Public methods |
|
|---|---|
static EmojiCompat |
get()Return singleton EmojiCompat instance. |
@NonNull String |
Returns signature for the currently loaded emoji assets. |
int |
Returns loading state of the EmojiCompat instance. |
static boolean |
handleDeleteSurroundingText(Handles deleteSurroundingText commands from |
static boolean |
handleOnKeyDown(@NonNull Editable editable, int keyCode, KeyEvent event)Handles onKeyDown commands from a |
boolean |
hasEmojiGlyph(@NonNull CharSequence sequence)Returns |
boolean |
hasEmojiGlyph(Returns |
static EmojiCompat |
init(@NonNull EmojiCompat.Config config)Initialize the singleton instance with a configuration. |
void |
load()When |
CharSequence |
process(@NonNull CharSequence charSequence)Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found. |
CharSequence |
process(Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found. |
CharSequence |
process(Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found. |
CharSequence |
process(Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found. |
void |
registerInitCallback(@NonNull EmojiCompat.InitCallback initCallback)Registers an initialization callback. |
void |
unregisterInitCallback(@NonNull EmojiCompat.InitCallback initCallback)Unregisters a callback that was added before. |
Constants
EDITOR_INFO_METAVERSION_KEY
public static final String EDITOR_INFO_METAVERSION_KEY = "android.support.text.emoji.emojiCompat_metadataVersion"
Key in extras that represents the emoji metadata version used by the widget. The existence of the value means that the widget is using EmojiCompat.
int and can be used to query EmojiCompat to see whether the widget has the ability to display a certain emoji using hasEmojiGlyph.EDITOR_INFO_REPLACE_ALL_KEY
public static final String EDITOR_INFO_REPLACE_ALL_KEY = "android.support.text.emoji.emojiCompat_replaceAll"
Key in extras that represents setReplaceAll configuration parameter. The key is added only if EmojiCompat is used by the widget. If exists, the value is a boolean.
LOAD_STATE_DEFAULT
public static final int LOAD_STATE_DEFAULT = 3
EmojiCompat instance is constructed, however the initialization did not start yet.
| See also | |
|---|---|
getLoadState |
LOAD_STATE_FAILED
public static final int LOAD_STATE_FAILED = 2
An unrecoverable error occurred during initialization of EmojiCompat. Calls to functions such as process will fail.
| See also | |
|---|---|
getLoadState |
LOAD_STATE_LOADING
public static final int LOAD_STATE_LOADING = 0
EmojiCompat is initializing.
| See also | |
|---|---|
getLoadState |
LOAD_STATE_SUCCEEDED
public static final int LOAD_STATE_SUCCEEDED = 1
EmojiCompat successfully initialized.
| See also | |
|---|---|
getLoadState |
LOAD_STRATEGY_DEFAULT
public static final int LOAD_STRATEGY_DEFAULT = 0
EmojiCompat will start loading metadata when init is called.
| See also | |
|---|---|
setMetadataLoadStrategy |
LOAD_STRATEGY_MANUAL
public static final int LOAD_STRATEGY_MANUAL = 1
EmojiCompat will wait for load to be called by developer in order to start loading metadata.
| See also | |
|---|---|
setMetadataLoadStrategy |
REPLACE_STRATEGY_ALL
public static final int REPLACE_STRATEGY_ALL = 1
Replace strategy to add EmojiSpans for all emoji that were found.
| See also | |
|---|---|
process |
REPLACE_STRATEGY_DEFAULT
public static final int REPLACE_STRATEGY_DEFAULT = 0
Replace strategy that uses the value given in EmojiCompat.Config.
| See also | |
|---|---|
process |
REPLACE_STRATEGY_NON_EXISTENT
public static final int REPLACE_STRATEGY_NON_EXISTENT = 2
Replace strategy to add EmojiSpans only for emoji that do not exist in the system.
Public methods
get
public static EmojiCompat get()
Return singleton EmojiCompat instance. Should be called after init is called to initialize the singleton instance.
| Returns | |
|---|---|
EmojiCompat |
EmojiCompat instance |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if called before |
getAssetSignature
public @NonNull String getAssetSignature()
Returns signature for the currently loaded emoji assets. The signature is a SHA that is constructed using emoji assets. Can be used to detect if currently loaded asset is different then previous executions. When used on devices running API 18 or below, returns empty string.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
getLoadState
public int getLoadState()
Returns loading state of the EmojiCompat instance. When used on devices running API 18 or below always returns LOAD_STATE_SUCCEEDED.
| Returns | |
|---|---|
int |
one of |
handleDeleteSurroundingText
public static boolean handleDeleteSurroundingText(
@NonNull InputConnection inputConnection,
@NonNull Editable editable,
@IntRange(from = 0) int beforeLength,
@IntRange(from = 0) int afterLength,
boolean inCodePoints
)
Handles deleteSurroundingText commands from InputConnection and tries to delete an EmojiSpan from an Editable. Returns true if an EmojiSpan is deleted.
false.
| Parameters | |
|---|---|
@NonNull InputConnection inputConnection |
InputConnection instance |
@NonNull Editable editable |
TextView.Editable instance |
@IntRange(from = 0) int beforeLength |
the number of characters before the cursor to be deleted |
@IntRange(from = 0) int afterLength |
the number of characters after the cursor to be deleted |
boolean inCodePoints |
|
| Returns | |
|---|---|
boolean |
|
handleOnKeyDown
public static boolean handleOnKeyDown(@NonNull Editable editable, int keyCode, KeyEvent event)
Handles onKeyDown commands from a KeyListener and if keyCode is one of KEYCODE_DEL or KEYCODE_FORWARD_DEL it tries to delete an EmojiSpan from an Editable. Returns true if an EmojiSpan is deleted with the characters it covers.
false.
| Parameters | |
|---|---|
@NonNull Editable editable |
Editable instance passed to |
int keyCode |
keyCode passed to |
KeyEvent event |
KeyEvent passed to |
| Returns | |
|---|---|
boolean |
|
hasEmojiGlyph
public boolean hasEmojiGlyph(@NonNull CharSequence sequence)
Returns true if EmojiCompat is capable of rendering an emoji. When used on devices running API 18 or below, always returns false.
| Parameters | |
|---|---|
@NonNull CharSequence sequence |
CharSequence representing the emoji |
| Returns | |
|---|---|
boolean |
|
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
hasEmojiGlyph
public boolean hasEmojiGlyph(
@NonNull CharSequence sequence,
@IntRange(from = 0) int metadataVersion
)
Returns true if EmojiCompat is capable of rendering an emoji at the given metadata version. When used on devices running API 18 or below, always returns false.
| Parameters | |
|---|---|
@NonNull CharSequence sequence |
CharSequence representing the emoji |
@IntRange(from = 0) int metadataVersion |
the metadata version to check against, should be greater than or equal to |
| Returns | |
|---|---|
boolean |
|
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
init
public static EmojiCompat init(@NonNull EmojiCompat.Config config)
Initialize the singleton instance with a configuration. When used on devices running API 18 or below, the singleton instance is immediately moved into LOAD_STATE_SUCCEEDED state without loading any metadata. When called for the first time, the library will create the singleton instance and any call after that will not create a new instance and return immediately.
| See also | |
|---|---|
EmojiCompat.Config |
load
public void load()
When setMetadataLoadStrategy is set to LOAD_STRATEGY_MANUAL, this function starts loading the metadata. Calling the function when setMetadataLoadStrategy is not set to LOAD_STRATEGY_MANUAL will throw an exception. The load will not start if:
- the metadata is already loaded successfully and
getLoadStateisLOAD_STATE_SUCCEEDED. - a previous load attempt is not finished yet and
getLoadStateisLOAD_STATE_LOADING.
| Throws | |
|---|---|
java.lang.IllegalStateException |
when |
process
public CharSequence process(@NonNull CharSequence charSequence)
Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found. When used on devices running API 18 or below, returns the given charSequence without processing it.
| Parameters | |
|---|---|
@NonNull CharSequence charSequence |
CharSequence to add the EmojiSpans |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
| See also | |
|---|---|
process |
process
public CharSequence process(
@NonNull CharSequence charSequence,
@IntRange(from = 0) int start,
@IntRange(from = 0) int end
)
Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found.
- If no emojis are found,
charSequencegiven as the input is returned without any changes. i.e. charSequence is a String, and no emojis are found, the same String is returned. - If the given input is not a Spannable (such as String), and at least one emoji is found a new
android.text.Spannableinstance is returned. - If the given input is a Spannable, the same instance is returned.
charSequence without processing it.
| Parameters | |
|---|---|
@NonNull CharSequence charSequence |
CharSequence to add the EmojiSpans, cannot be |
@IntRange(from = 0) int start |
start index in the charSequence to look for emojis, should be greater than or equal to |
@IntRange(from = 0) int end |
end index in the charSequence to look for emojis, should be greater than or equal to |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
java.lang.IllegalArgumentException |
in the following cases: |
process
public CharSequence process(
@NonNull CharSequence charSequence,
@IntRange(from = 0) int start,
@IntRange(from = 0) int end,
@IntRange(from = 0) int maxEmojiCount
)
Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found.
- If no emojis are found,
charSequencegiven as the input is returned without any changes. i.e. charSequence is a String, and no emojis are found, the same String is returned. - If the given input is not a Spannable (such as String), and at least one emoji is found a new
android.text.Spannableinstance is returned. - If the given input is a Spannable, the same instance is returned.
charSequence without processing it.
| Parameters | |
|---|---|
@NonNull CharSequence charSequence |
CharSequence to add the EmojiSpans, cannot be |
@IntRange(from = 0) int start |
start index in the charSequence to look for emojis, should be greater than or equal to |
@IntRange(from = 0) int end |
end index in the charSequence to look for emojis, should be greater than or equal to |
@IntRange(from = 0) int maxEmojiCount |
maximum number of emojis in the |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
java.lang.IllegalArgumentException |
in the following cases: |
process
public CharSequence process(
@NonNull CharSequence charSequence,
@IntRange(from = 0) int start,
@IntRange(from = 0) int end,
@IntRange(from = 0) int maxEmojiCount,
int replaceStrategy
)
Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found.
- If no emojis are found,
charSequencegiven as the input is returned without any changes. i.e. charSequence is a String, and no emojis are found, the same String is returned. - If the given input is not a Spannable (such as String), and at least one emoji is found a new
android.text.Spannableinstance is returned. - If the given input is a Spannable, the same instance is returned.
charSequence without processing it.
| Parameters | |
|---|---|
@NonNull CharSequence charSequence |
CharSequence to add the EmojiSpans, cannot be |
@IntRange(from = 0) int start |
start index in the charSequence to look for emojis, should be greater than or equal to |
@IntRange(from = 0) int end |
end index in the charSequence to look for emojis, should be greater than or equal to |
@IntRange(from = 0) int maxEmojiCount |
maximum number of emojis in the |
int replaceStrategy |
whether to replace all emoji with |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
java.lang.IllegalArgumentException |
in the following cases: |
registerInitCallback
public void registerInitCallback(@NonNull EmojiCompat.InitCallback initCallback)
Registers an initialization callback. If the initialization is already completed by the time the listener is added, the callback functions are called immediately. Callbacks are called on the main looper.
When used on devices running API 18 or below,onInitialized is called without loading any metadata. In such cases onFailed is never called.
| Parameters | |
|---|---|
@NonNull EmojiCompat.InitCallback initCallback |
the initialization callback to register, cannot be |
| See also | |
|---|---|
unregisterInitCallback |
unregisterInitCallback
public void unregisterInitCallback(@NonNull EmojiCompat.InitCallback initCallback)
Unregisters a callback that was added before.
| Parameters | |
|---|---|
@NonNull EmojiCompat.InitCallback initCallback |
the callback to be removed, cannot be |