TranslationManager
public
final
class
TranslationManager
extends Object
java.lang.Object | |
↳ | android.view.translation.TranslationManager |
The TranslationManager
class provides ways for apps to integrate and use the
translation framework.
The TranslationManager manages Translator
s and help bridge client calls to
the server translation service
Summary
Public methods | |
---|---|
void
|
addOnDeviceTranslationCapabilityUpdateListener(Executor executor, Consumer<TranslationCapability> capabilityListener)
Adds a |
void
|
createOnDeviceTranslator(TranslationContext translationContext, Executor executor, Consumer<Translator> callback)
Creates an on-device Translator for natural language translation. |
Set<TranslationCapability>
|
getOnDeviceTranslationCapabilities(int sourceFormat, int targetFormat)
Returns a set of |
PendingIntent
|
getOnDeviceTranslationSettingsActivityIntent()
Returns an immutable PendingIntent which can be used to launch an activity to view/edit on-device translation settings. |
void
|
removeOnDeviceTranslationCapabilityUpdateListener(Consumer<TranslationCapability> capabilityListener)
Removes a |
Inherited methods | |
---|---|
Public methods
addOnDeviceTranslationCapabilityUpdateListener
public void addOnDeviceTranslationCapabilityUpdateListener (Executor executor, Consumer<TranslationCapability> capabilityListener)
Adds a TranslationCapability
Consumer to listen for updates on states of on-device
TranslationCapability
s.
Parameters | |
---|---|
executor |
Executor : This value cannot be null .
Callback and listener events are dispatched through this
Executor , providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor() .
Otherwise, provide an Executor that dispatches to an appropriate thread. |
capabilityListener |
Consumer : a TranslationCapability Consumer to receive the updated
TranslationCapability from the on-device translation service.
This value cannot be null . |
createOnDeviceTranslator
public void createOnDeviceTranslator (TranslationContext translationContext, Executor executor, Consumer<Translator> callback)
Creates an on-device Translator for natural language translation.
In Android 12, this method provided the same cached Translator object when given the same TranslationContext object. Do not use a Translator destroyed elsewhere as this will cause an exception on Android 12.
In later versions, this method never returns a cached Translator.
Parameters | |
---|---|
translationContext |
TranslationContext : TranslationContext containing the specs for creating the
Translator.
This value cannot be null . |
executor |
Executor : Executor to run callback operations
This value cannot be null .
Callback and listener events are dispatched through this
Executor , providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor() .
Otherwise, provide an Executor that dispatches to an appropriate thread. |
callback |
Consumer : Consumer to receive the translator. A null value is returned
if the service could not create the translator. |
getOnDeviceTranslationCapabilities
public Set<TranslationCapability> getOnDeviceTranslationCapabilities (int sourceFormat, int targetFormat)
Returns a set of TranslationCapability
s describing the capabilities for on-device
Translator
s.
These translation capabilities contains a source and target TranslationSpec
representing the data expected for both ends of translation process. The capabilities
provides the information and limitations for generating a TranslationContext
.
The context object can then be used by
createOnDeviceTranslator(android.view.translation.TranslationContext, java.util.concurrent.Executor, java.util.function.Consumer)
to obtain a
Translator
for translations.
NOTE: Call on a worker thread.
This method may take several seconds to complete, so it should
only be called from a worker thread.
Parameters | |
---|---|
sourceFormat |
int : data format for the input data to be translated.
Value is TranslationSpec.DATA_FORMAT_TEXT |
targetFormat |
int : data format for the expected translated output data.
Value is TranslationSpec.DATA_FORMAT_TEXT |
Returns | |
---|---|
Set<TranslationCapability> |
A set of TranslationCapability s.
This value cannot be null . |
getOnDeviceTranslationSettingsActivityIntent
public PendingIntent getOnDeviceTranslationSettingsActivityIntent ()
Returns an immutable PendingIntent which can be used to launch an activity to view/edit on-device translation settings.
Returns | |
---|---|
PendingIntent |
An immutable PendingIntent or null if one of reason met:
|
removeOnDeviceTranslationCapabilityUpdateListener
public void removeOnDeviceTranslationCapabilityUpdateListener (Consumer<TranslationCapability> capabilityListener)
Removes a TranslationCapability
Consumer to listen for updates on states of
on-device TranslationCapability
s.
Parameters | |
---|---|
capabilityListener |
Consumer : the TranslationCapability Consumer to unregister
This value cannot be null . |