Android 技術專題系列之七 -- 輸入法 框架

原文地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=2715&highlight=Android%2B%E6%8A%80%E6%9C%AF%E4%B8%93%E9%A2%98

 

Android的輸入法框架比價複雜。從進程的角度來講,相關功能主要分佈在下面三個位置:
[url=http://photo.blog.sina.com.cn/showpic.html#blogid=5e71b14f0100bu3f&url=http://s16.sinaimg.cn/orignal/5e71b14ft7070f9a7963f][/url]

客戶端應用是一個包含有圖形界面的應用,如地址本。圖形界面上包含有能夠接收輸入的編輯框,如TextView。
輸入法模塊提供軟鍵盤,將用戶在軟鍵盤上的按鍵輸入根據某種算法(如Zi, T9, 國筆等)轉換成單詞,然後傳遞給客戶端應用。目錄development/samples/SoftKeyboard下提供了一個輸入法模塊實例。如果想要實現一箇中文輸入法,可參考這個實例。
平臺部分實現一些管理功能,負責裝載某個輸入法模塊,啓動,終止該模塊等。
相關代碼主要位於下面幾個位置。其中,位於3,5,6,7目錄下的代碼最值得關注。
1. frameworks/base/core/java/com/android/internal/view
這個目錄下定義了幾個重要的idl 接口。
IInputMethod.aidl 定義了IInputMethod idl 接口,用於客戶端跨進程操作InputMethod接口。
IInputMethodSession.aidl 定義了IInputMethodSession接口,是IInputMethod的輔助接口。用於客戶端跨進程操作InputMethodSession接口。

IInputMethodCallback.aidl定義了一個helper 接口,由客戶端實現。IInputMethod.aidl和IInputMethodSession.aidl實例可以分別調用該接口中的不同方法

IInputMethodManager.aidl 定義了Input Method Manager的service接口。客戶端通過InputMethodManager interface調用這個service。
InputMethodManagerService.java實現了IInputMethodManager.aidl接口

IInputMethodClient.aidl定義接口,標識一個Input Method Manager 的客戶。這個service在客戶端實現,提供給server端調用。

IInputContext.aidl定義了一個接口,由客戶端提供InputMethod使用。InputMethod可以與客戶端交互,調用客戶端提供的callback。
IInputConnectionWrapper.java 實現了IInputContext接口。
IInputContextCallback.aidl定義了一個接口,定義了一組callback函數給IInputContext.aidl實例調用,從客戶端返回信息給InputMethod。
InputConnectionWrapper.java實現了IInputContextCallback接口。

2. frameworks/base/services/java/com/android/server
InputMethodManagerService.java實現了IInputMethodManager.aidl接口

3. frameworks/base/core/java/android/view/inputmethod
這個目錄下定義了幾個重要的interface和類。
InputMethodManager.java實現了InputMethodManager 類。此類調用IInputMethodManager.aidl接口功能,而IInputMethodManager.aidl接口功能由InputMethodManagerService.java實現,並運行在不同於客戶端進程的server進程中。

InputConnection.java定義了InputConnection interface。InputConnection 接口在輸入法和客戶端之間建立了一個連接,輸入法可以使用該連接獲取或發送信息給客戶端。InputConnection實例由客戶端創建之後傳遞給輸入法使用。BaseInputConnection.java 實現了InputConnection接口的一個基類: BaseInputConnection。 EditableInputConnection.java實現了一個派生類

InputBinding.java 定義了類InputBinding,這個類實現了parcelable 接口。這個類的成員變量包含了客戶端傳向server的信息。

InputMethod.java定義了InputMethod interface。文件InputMethodService.java中類InputMethodImpl實現了這個接口。這個接口定義了一套操縱一個輸入法的方法。如,createSession,startInput等。要編寫一個具體輸入法的話,就需要派生這個接口。

InputMethodSession.java定義了InputMethodSession接口。文件InputMethodService.java中類InputMethodSessionImpl實現了這個接口。InputMethodSession是InputMethod的輔助接口,用於具體和某個輸入法客戶端交互。

CompletionInfo.java 類描述一個text completion.
EditorInfo.java類描述一個接收輸入的view的屬性,如內容屬性(text, digit, etc)。
ExtractedText.java類描述從view中提取的傳遞給輸入法的文本屬性。


4. frameworks/base/core/java/com/android/internal/widget
EditableInputConnection.java實現了BaseInputConnection的一個派生類。

5. frameworks/base/core/java/android/inputmethodservice
這個目錄下的代碼提供了實現一個具體輸入法的框架類。從這些類派生,就可以定製一個輸入法。
SoftInputWindow.java中的SoftInputWindow類是一個Dialog子類。它代表一個輸入法的頂級窗口(由窗口管理器管理),這個窗口由上到下,包含extractArea, candidatesArea, 和 inputArea。

Keyboard.java 中的Keyboard類裝載並解析一個描述虛擬鍵盤(Soft Keyboard)的xml文件(如development/samples/SoftKeyboard/res/xml),並存儲該鍵盤的屬性,如該虛擬鍵盤包含多上行,每行有哪些鍵等。
KeyboardView.java 中的KeyboardView類是一個View子類。它根據Keyboard數據結構真正的在screen上畫出一個虛擬鍵盤。這個虛擬鍵盤就是SoftInputWindow中的inputArea。

AbstractInputMethodService是Service的派生類,並實現了KeyEvent.Callback 接口。實現了InputMethod 和 InputMethodSession的基類。dispatchKeyEvent 函數將收到的key event傳給相應的key 處理函數(在派生類中實現)。當這個service被客戶端綁定時,其onBind()函數給客戶端返回了一個IInputMethodWrapper實例,這個實例實現了IInputMethod idl接口。客戶端可以使用該接口的相關功能。

IInputMethodWrapper.java 實現了IInputMethod idl 接口。這個類收到客戶端的跨進程命令後,調用InputMethod完成相應功能。
IInputMethodSessionWrapper.java 實現了IInputMethodSession idl接口。這個類收到客戶端的跨進程命令後,調用InputMethodSession完成相應功能。


6. frameworks/base/core/res/res/layout
這個目錄下存放着一些系統資源。其中,
input_method.xml描述了一個輸入法的窗口(即SoftInputWindow)佈局,從上往下,依次排列extractArea, candidatesArea 和 inputArea。
input_method_extract_view.xml。

7. development/samples/SoftKeyboard
這個目錄下代碼實現了一個的輸入法實例--軟鍵盤英文/數字輸入法。這裏面實現的類大都是從frameworks/base/core/java/android/inputmethodservice 中的類派生而來。
AndroidManifest.xml:描述這個.apk提供的service以及關於這個輸入法的一些信息。
res/xml/目錄下存儲着幾個描述不同虛擬鍵盤的xml文件。
LatinKeyboard.java中的LatinKeyboard類是Keyboard的子類。
LatinKeyboardView.java中的LatinKeyboardView類是KeyboardView的子類。

8. frameworks/base/core/java/android/widget
在這裏TextView.java是使用Input Method Framework (IMF)的客戶端。TextView創建了一個InputMethodManager的實例並調用其restartInput 函數。
InputMethodManager::restartInput函數創建了一個InputConnection 實例並調用IInputMethodManager::startInput。
IInputMethodManager::startInput 函數使用mContext.bindService啓動一個InputMethod service, 如 Sample Soft Keyboard。

9. frameworks/base/core/java/com/android/internal/widget
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章