EmojiCompat
@AnyThread
class EmojiCompat
Main class to keep Android devices up to date with the newest emojis by adding EmojiSpans to a given CharSequence.
EmojiCompatInitializer, which performs deferred font loading to avoid potential app startup delays. The default behavior is to load the font shortly after the first Activity resumes. EmojiCompatInitializer will configure EmojiCompat to use the system emoji font provider via DefaultEmojiCompatConfig and always creates a new background thread for font loading. EmojiCompat will only allow one instance to be initialized and any calls to init after the first one will have no effect. As a result, configuration options may not be provided when using EmojiCompatInitializer. To provide a custom configuration, disable EmojiCompatInitializer in the manifest with:
<provider android:name="androidx.startup.InitializationProvider" android:authorities="${applicationId}.androidx-startup" android:exported="false" tools:node="merge"> <meta-data android:name="androidx.emoji2.text.EmojiCompatInitializer" tools:node="remove" /> </provider>
init. It is recommended to make the initialization as early as possible in your app, such as from onCreate. init is fast and may be called from the main thread on the path to displaying the first activity. However, loading the emoji font takes significant resources on a background thread, so it is suggested to use LOAD_STRATEGY_MANUAL in all manual configurations to defer font loading until after the first screen displays. Font loading may be started by calling load}. See the implementation EmojiCompatInitializer for ideas when building a manual configuration. 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")
During loading information about emojis is not available. Before the
EmojiCompat instance has finished loading, calls to functions such as process will throw an exception. It is safe to call process when
getLoadState returns LOAD_STATE_SUCCEEDED. To register a callback when
loading completes use InitCallback.
Summary
Nested types |
|---|
abstract class EmojiCompat.ConfigConfiguration class for EmojiCompat. |
interface EmojiCompat.GlyphCheckerInterface to check if a given emoji exists on the system. |
abstract class EmojiCompat.InitCallbackListener class for the initialization of the EmojiCompat. |
interface EmojiCompat.MetadataRepoLoaderInterface to load emoji metadata. |
abstract class EmojiCompat.MetadataRepoLoaderCallbackCallback to inform EmojiCompat about the state of the metadata load. |
interface EmojiCompat.SpanFactoryFactory class that creates the EmojiSpans. |
Constants |
|
|---|---|
const String! |
EDITOR_INFO_METAVERSION_KEY = "android.support.text.emoji.emojiCompat_metadataVersion"Key in |
const String! |
EDITOR_INFO_REPLACE_ALL_KEY = "android.support.text.emoji.emojiCompat_replaceAll"Key in |
const Int |
EMOJI_FALLBACK = 2Result of |
const Int |
EMOJI_SUPPORTED = 1Result of |
const Int |
Result of |
const Int |
EmojiCompat instance is constructed, however the initialization did not start yet. |
const Int |
An unrecoverable error occurred during initialization of EmojiCompat. |
const Int |
EmojiCompat is initializing. |
const Int |
EmojiCompat successfully initialized. |
const Int |
|
const Int |
|
const Int |
Replace strategy to add |
const Int |
Replace strategy that uses the value given in |
const Int |
Replace strategy to add |
Public functions |
|
|---|---|
java-static EmojiCompat |
get()Return singleton EmojiCompat instance. |
String |
Returns signature for the currently loaded emoji assets. |
Int |
getEmojiEnd(charSequence: CharSequence, offset: @IntRange(from = 0) Int)see |
Int |
getEmojiMatch(Attempts to lookup the entire sequence at the specified metadata version and returns what the runtime match behavior would be. |
Int |
getEmojiStart(charSequence: CharSequence, offset: @IntRange(from = 0) Int)Together with |
Int |
Returns loading state of the EmojiCompat instance. |
java-static Boolean |
handleDeleteSurroundingText(Handles deleteSurroundingText commands from |
java-static Boolean |
handleOnKeyDown(editable: Editable, keyCode: Int, event: KeyEvent)Handles onKeyDown commands from a |
Boolean |
This function is deprecated. use getEmojiMatch which returns more accurate lookup information. |
Boolean |
This function is deprecated. use getEmojiMatch which returns more accurate lookup information. |
java-static EmojiCompat |
init(config: EmojiCompat.Config)Initialize the singleton instance with a configuration. |
java-static EmojiCompat? |
Initialize the singleton instance with the default system-provided configuration. |
java-static Boolean |
Return true if EmojiCompat has been configured by a successful call to |
Unit |
load()When |
CharSequence? |
process(charSequence: CharSequence?)Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found. |
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. |
Unit |
registerInitCallback(initCallback: EmojiCompat.InitCallback)Registers an initialization callback. |
Unit |
registerInitCallback(Registers an initialization callback. |
Unit |
unregisterInitCallback(initCallback: EmojiCompat.InitCallback)Unregisters a callback that was added before. |
Unit |
updateEditorInfo(outAttrs: EditorInfo)Updates the EditorInfo attributes in order to communicate information to Keyboards. |
Constants
EDITOR_INFO_METAVERSION_KEY
const val EDITOR_INFO_METAVERSION_KEY = "android.support.text.emoji.emojiCompat_metadataVersion": String!
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
const val EDITOR_INFO_REPLACE_ALL_KEY = "android.support.text.emoji.emojiCompat_replaceAll": String!
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.
EMOJI_FALLBACK
const val EMOJI_FALLBACK = 2: Int
Result of getEmojiMatch that means the full codepoint sequence is not known to emojicompat, but at least one subsequence is an emoji that is known at this metadata level. Keyboards may decide that this emoji is not supported by the application when this result is returned, with no further processing. This return value implies: - EmojiCompat will decompose this ZWJ sequence into multiple glyphs when replaceAll=true - EmojiCompat MAY defer to platform when replaceAll=false - System emoji font may or may not support this emoji - This application MAY render this emoji This return value is only ever returned for ZWJ sequences. To understand this result consider when it may be returned for the multi-skin-tone handshake introduced in emoji 14.
U+1FAF1 // unknown @ requested metadata level U+1F3FB // metadata level 1 U+200D // not displayed (ZWJ) U+1FAF2 // unknown @ requested metadata level U+1F3FD // metadata level 1
REPLACE_STRATEGY_ALL true, the color emoji are matched and replaced with EmojiSpan. The system emoji font, even if it supports this ZWJ sequence, is never queried and the added EmojiSpans force fallback rendering for the ZWJ sequence. The glyph will only display correctly for this application if ALL of the following requirements are met: - REPLACE_STRATEGY_ALL is false - hasGlyph returns true for each emoji subsequence known at this metadata level - hasGlyph returns true for the full sequence Given this return value for the multi-skin-tone handshake above, if REPLACE_STRATEGY_ALL is false then the emoji will display if the entire emoji sequence is matched by hasGlyph because U+1F3FB and U+1F3FD are both in the system emoji font. Keyboards that wish to determine if the glyph will display correctly by the application in response to this return value should consider building an internal lookup for new ZWJ sequences instead of repeatedly calling hasGlyph for each emoji subsequence.EMOJI_SUPPORTED
const val EMOJI_SUPPORTED = 1: Int
Result of getEmojiMatch that means this codepoint can be drawn by an EmojiSpan at this metadata level. No further checks are required by keyboards for this result. The emoji is always supported by this application. This return value implies: - EmojiCompat can draw this emoji - System emoji font may or may not support this emoji - This application WILL render this emoji This result implies that EmojiCompat can successfully display this emoji. The system emoji font is not checked by this method, and this result may be returned even if the platform also supports the emoji sequence. If the application passes REPLACE_STRATEGY_ALL of true, then an EmojiSpan will always be generated for this emoji. If the application passes REPLACE_STRATEGY_ALL of false, then an EmojiSpan will only be generated if hasGlyph returns false for this emoji.
EMOJI_UNSUPPORTED
const val EMOJI_UNSUPPORTED = 0: Int
Result of getEmojiMatch that means no part of this codepoint sequence will ever generate an EmojiSpan at the requested metadata level. This return value implies: - EmojiCompat will always defer to system emoji font - System emoji font may or may not support this emoji - This application MAY render this emoji This can be used by keyboards to learn that EmojiCompat does not support this codepoint sequence at this metadata version. The system emoji font is not checked by this method, and this result will be returned even if the system emoji font supports the emoji. This may happen if the application is using an older version of the emoji compat font than the system emoji font. Keyboards may optionally determine that the system emoji font will support the emoji, for example by building a internal lookup table or calling hasGlyph to query the system emoji font. Keyboards may use a lookup table to optimize this check, however they should be aware that OEMs may add or remove emoji from the system emoji font. Keyboards may finally decide: - If the system emoji font DOES NOT support the emoji, then the emoji IS NOT supported by this application. - If the system emoji font DOES support the emoji, then the emoji IS supported by this application. - If system emoji font is support is UNKNOWN, then assume the emoji IS NOT supported by this application.
LOAD_STATE_DEFAULT
const val LOAD_STATE_DEFAULT = 3: Int
EmojiCompat instance is constructed, however the initialization did not start yet.
| See also | |
|---|---|
getLoadState |
LOAD_STATE_FAILED
const val LOAD_STATE_FAILED = 2: Int
An unrecoverable error occurred during initialization of EmojiCompat. Calls to functions such as process will fail.
| See also | |
|---|---|
getLoadState |
LOAD_STATE_LOADING
const val LOAD_STATE_LOADING = 0: Int
EmojiCompat is initializing.
| See also | |
|---|---|
getLoadState |
LOAD_STATE_SUCCEEDED
const val LOAD_STATE_SUCCEEDED = 1: Int
EmojiCompat successfully initialized.
| See also | |
|---|---|
getLoadState |
LOAD_STRATEGY_DEFAULT
const val LOAD_STRATEGY_DEFAULT = 0: Int
EmojiCompat will start loading metadata when init is called.
| See also | |
|---|---|
setMetadataLoadStrategy |
LOAD_STRATEGY_MANUAL
const val LOAD_STRATEGY_MANUAL = 1: Int
EmojiCompat will wait for load to be called by developer in order to start loading metadata.
| See also | |
|---|---|
setMetadataLoadStrategy |
REPLACE_STRATEGY_ALL
const val REPLACE_STRATEGY_ALL = 1: Int
Replace strategy to add EmojiSpans for all emoji that were found.
| See also | |
|---|---|
process |
REPLACE_STRATEGY_DEFAULT
const val REPLACE_STRATEGY_DEFAULT = 0: Int
Replace strategy that uses the value given in EmojiCompat.Config.
| See also | |
|---|---|
process |
REPLACE_STRATEGY_NON_EXISTENT
const val REPLACE_STRATEGY_NON_EXISTENT = 2: Int
Replace strategy to add EmojiSpans only for emoji that do not exist in the system.
Public functions
get
java-static fun get(): EmojiCompat
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
fun getAssetSignature(): String
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 |
getEmojiEnd
fun getEmojiEnd(charSequence: CharSequence, offset: @IntRange(from = 0) Int): Int
see getEmojiStart.
getEmojiMatch
fun getEmojiMatch(
sequence: CharSequence,
metadataVersion: @IntRange(from = 0) Int
): Int
Attempts to lookup the entire sequence at the specified metadata version and returns what the runtime match behavior would be. To be used by keyboards to show or hide emoji in response to specific metadata support.
| Parameters | |
|---|---|
sequence: CharSequence |
CharSequence representing an emoji |
metadataVersion: @IntRange(from = 0) Int |
the metada version to check against, should be greater than or equal to |
| Returns | |
|---|---|
Int |
A match result, or decomposes if replaceAll would cause partial subsequence matches. |
getEmojiStart
fun getEmojiStart(charSequence: CharSequence, offset: @IntRange(from = 0) Int): Int
Together with getEmojiEnd, if the character at offset is part of an emoji, returns the index range of that emoji, start index inclusively/end index exclusively so that charSequence.subSequence(start, end) will return that emoji. E.g., getEmojiStart/End("AB😀", 1) will return (-1,-1) since 'B' is not part an emoji; getEmojiStart/End("AB😀", 3) will return [2,4), note that "😀" contains 2 Chars. Returns -1 otherwise.
| Parameters | |
|---|---|
charSequence: CharSequence |
the whole sequence |
offset: @IntRange(from = 0) Int |
index of the emoji to look up |
| Returns | |
|---|---|
Int |
the start index inclusively/end index exclusively |
getLoadState
fun getLoadState(): Int
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
java-static fun handleDeleteSurroundingText(
inputConnection: InputConnection,
editable: Editable,
beforeLength: @IntRange(from = 0) Int,
afterLength: @IntRange(from = 0) Int,
inCodePoints: Boolean
): Boolean
Handles deleteSurroundingText commands from InputConnection and tries to delete an EmojiSpan from an Editable. Returns true if an EmojiSpan is deleted.
false.
| Parameters | |
|---|---|
inputConnection: InputConnection |
InputConnection instance |
editable: Editable |
TextView.Editable instance |
beforeLength: @IntRange(from = 0) Int |
the number of characters before the cursor to be deleted |
afterLength: @IntRange(from = 0) Int |
the number of characters after the cursor to be deleted |
inCodePoints: Boolean |
|
handleOnKeyDown
java-static fun handleOnKeyDown(editable: Editable, keyCode: Int, event: KeyEvent): Boolean
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.
funhasEmojiGlyph(sequence: CharSequence): Boolean
Returns true if EmojiCompat is capable of rendering an emoji. When used on devices running API 18 or below, always returns false.
| Parameters | |
|---|---|
sequence: CharSequence |
CharSequence representing the emoji |
| Returns | |
|---|---|
Boolean |
|
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
funhasEmojiGlyph(
sequence: CharSequence,
metadataVersion: @IntRange(from = 0) Int
): Boolean
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 | |
|---|---|
sequence: CharSequence |
CharSequence representing the emoji |
metadataVersion: @IntRange(from = 0) Int |
the metadata version to check against, should be greater than or equal to |
| Returns | |
|---|---|
Boolean |
|
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
init
java-static fun init(config: EmojiCompat.Config): EmojiCompat
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 |
init
java-static fun init(context: Context): EmojiCompat?
Initialize the singleton instance with the default system-provided configuration.
This is the recommended configuration for most applications. For more details see DefaultEmojiCompatConfig.
This call will use DefaultEmojiCompatConfig to lookup the default emoji font provider installed on the system and use that, if present. If there is no default font provider onthe system, this call will have no effect.
Note: EmojiCompat may only be initialized once, and will return the same instance afterwords.
| Returns | |
|---|---|
EmojiCompat? |
Default EmojiCompat for this device, or null if there is no provider on the system. |
isConfigured
java-static fun isConfigured(): Boolean
Return true if EmojiCompat has been configured by a successful call to init. You can use this to check if get will return a valid EmojiCompat instance. This function does not check the getLoadState and will return true even if the font is still loading, or has failed to load.
| Returns | |
|---|---|
Boolean |
true if EmojiCompat has been successfully initialized. |
load
fun load(): Unit
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
fun process(charSequence: 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 | |
|---|---|
charSequence: CharSequence? |
CharSequence to add the EmojiSpans |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
| See also | |
|---|---|
process |
process
fun process(
charSequence: CharSequence?,
start: @IntRange(from = 0) Int,
end: @IntRange(from = 0) Int
): CharSequence?
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 | |
|---|---|
charSequence: CharSequence? |
CharSequence to add the EmojiSpans, cannot be |
start: @IntRange(from = 0) Int |
start index in the charSequence to look for emojis, should be greater than or equal to |
end: @IntRange(from = 0) Int |
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
fun process(
charSequence: CharSequence?,
start: @IntRange(from = 0) Int,
end: @IntRange(from = 0) Int,
maxEmojiCount: @IntRange(from = 0) Int
): CharSequence?
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 | |
|---|---|
charSequence: CharSequence? |
CharSequence to add the EmojiSpans, cannot be |
start: @IntRange(from = 0) Int |
start index in the charSequence to look for emojis, should be greater than or equal to |
end: @IntRange(from = 0) Int |
end index in the charSequence to look for emojis, should be greater than or equal to |
maxEmojiCount: @IntRange(from = 0) Int |
maximum number of emojis in the |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
java.lang.IllegalArgumentException |
in the following cases: |
process
fun process(
charSequence: CharSequence?,
start: @IntRange(from = 0) Int,
end: @IntRange(from = 0) Int,
maxEmojiCount: @IntRange(from = 0) Int,
replaceStrategy: Int
): CharSequence?
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 | |
|---|---|
charSequence: CharSequence? |
CharSequence to add the EmojiSpans, cannot be |
start: @IntRange(from = 0) Int |
start index in the charSequence to look for emojis, should be greater than or equal to |
end: @IntRange(from = 0) Int |
end index in the charSequence to look for emojis, should be greater than or equal to |
maxEmojiCount: @IntRange(from = 0) Int |
maximum number of emojis in the |
replaceStrategy: Int |
whether to replace all emoji with |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if not initialized yet |
java.lang.IllegalArgumentException |
in the following cases: |
registerInitCallback
fun registerInitCallback(initCallback: EmojiCompat.InitCallback): Unit
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 | |
|---|---|
initCallback: EmojiCompat.InitCallback |
the initialization callback to register, cannot be |
| See also | |
|---|---|
unregisterInitCallback |
registerInitCallback
fun registerInitCallback(
executor: Executor,
initCallback: EmojiCompat.InitCallback
): Unit
Registers an initialization callback. If the initialization is already completed by the time the listener is added, the callback functions are called immediately.
When used on devices running API 18 or below,onInitialized is called without loading any metadata. In such cases onFailed is never called.
| Parameters | |
|---|---|
executor: Executor |
executor to dispatch callback on |
initCallback: EmojiCompat.InitCallback |
the initialization callback to register, cannot be |
| See also | |
|---|---|
unregisterInitCallback |
unregisterInitCallback
fun unregisterInitCallback(initCallback: EmojiCompat.InitCallback): Unit
Unregisters a callback that was added before.
| Parameters | |
|---|---|
initCallback: EmojiCompat.InitCallback |
the callback to be removed, cannot be |
updateEditorInfo
fun updateEditorInfo(outAttrs: EditorInfo): Unit
Updates the EditorInfo attributes in order to communicate information to Keyboards. When used on devices running API 18 or below, does not update EditorInfo attributes. This is called from EditText integrations that use EmojiEditTextHelper. Custom widgets that allow IME not subclassing EditText should call this method when creating an input connection. When EmojiCompat is not in LOAD_STATE_SUCCEEDED, this method has no effect. Calling this method on API levels below API 19 will have no effect, as EmojiCompat may never be configured. However, it is always safe to call, even on older API levels.
| Parameters | |
|---|---|
outAttrs: EditorInfo |
EditorInfo instance passed to |