Implementing Word Prediction and Completion on Android IME
This thread is giving an lightly explanation of Android Input Method Framework (IMF), life-cycle of Input Method (IME) and the word prediction/completion in composing.
There are three primary parties involved in the IMF architecture:
- The Input method manager as expressed by this class (InputMethodManager) is the central point of the system that manages interaction between all other parts. It is expressed as the client-side API here which exists in each application context and communicates with a global system service that manages the interaction across all processes.
- An Input method (IME) service implements a particular interaction model allowing the user to generate text. The system binds to the current input method that is use, causing it to be created and run, and tells it when to hide and show its UI. Only one IME is running at a time.
- Multiple client applications arbitrate with the input method manager for input focus and control over the state of the IME. Only one such client is ever active (working with the IME) at a time.
II. InputMethod (IME) Overview
- An input method (IME) is implemented as a Service, typically deriving from InputMethodService? . It must provide the core InputMethod? interface, though this is normally handled by InputMethodService? and implementors will only need to deal with the higher-level API there.
- The InputMethodService? provides a basic framework for standard UI elements (input view, candidates view, and running in fullscreen mode), but it is up to a particular implementor to decide how to use them. For example, one input method could implement an input area with a keyboard, another could allow the user to draw text, while a third could have no input area (and thus not be visible to the user) but instead listen to audio and perform text to speech conversion.
- The normal Service lifecycle methods, the InputMethodService? class introduces some new specific callbacks that most subclasses will want to make use of:
- onInitializeInterface() for user-interface initialization, in particular to deal with configuration changes while the service is running.
- onBindInput() to find out about switching to a new client.
- onStartInput(EditorInfo? , boolean) to deal with an input session starting with the client.
- onCreateInputView(), onCreateCandidatesView(), and onCreateExtractTextView() for non-demand generation of the UI.
- onStartInputView(EditorInfo? , boolean) to deal with input starting within the input area of the IME.
android.inputmethodservice.InputMethodService. This API provides much of the basic implementation for an input method, in terms of managing the state and visibility of the input method and communicating with the currently visible activity. The typical life-cycle of an
InputMethodServicelooks like this:
CandidateView is the place where potential word corrections or completions are presented to the user for selection. Again, this may or may not be relevant to your input method and you can return
nullfrom calls to
InputMethodService.onCreateCandidatesView(), which is the default behavior. By creating a layout for candidates and implement
onCreateCandidatesView()method, developer can show the word prediction/completions when user is typing on the virtual keyboard.
The DFD below in basically show the most popular implemented in Android's IME applications (Android's IME, Latin IME, ZhuYin IME ...)
plz click to enlarge
And here is the architecture for a new prediction/completion method on IME:
(not yet complete...)