从ViewRootImpl到IME以及Activity、再到View过程:
一、Input子系统将按键分发给输入法应用
frameworks/base/core/java/android/view/ViewRootImpl.java
private void deliverKeyEvent(QueuedInputEvent q) {
if (mLastWasImTarget) {
InputMethodManager imm = InputMethodManager.peekInstance();
/*
frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
static public InputMethodManager peekInstance() {
return mInstance;
}
mInstance = new InputMethodManager(service, mainLooper);
*/
if (imm != null) {
final int seq = event.getSequenceNumber();
if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq=" + seq + " event=" + event);
imm.dispatchKeyEvent(mView.getContext(), seq, event, mInputMethodCallback);
/*
其中mInputMethodCallback为传入的回调函数
mInputMethodCallback = new InputMethodCallback(this);
*/
return;
}
}
}
frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
public void dispatchKeyEvent(Context context, int seq, KeyEvent key,
FinishedEventCallback callback) {
mCurMethod.dispatchKeyEvent(seq, key, mInputMethodCallback);
return;
}
IInputMethodSession mCurMethod;
frameworks/base/core/java/android/inputmethodservice/IInputMethodSessionWrapper.java
class IInputMethodSessionWrapper extends IInputMethodSession.Stub
implements HandlerCaller.Callback {
public void dispatchKeyEvent(int seq, KeyEvent event, IInputMethodCallback callback) {
mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_KEY_EVENT, seq,
event, callback));
}
}
mCaller = new HandlerCaller(context, this);
/*
frameworks/base/core/java/com/android/internal/os/HandlerCaller.java
public HandlerCaller(Context context, Callback callback) {
mContext = context;
mMainLooper = context.getMainLooper();
mH = new MyHandler(mMainLooper);
mCallback = callback;
}
public void executeOrSendMessage(Message msg) {
if (Looper.myLooper() == mMainLooper) {
mCallback.executeMessage(msg); //这个为IInputMethodSessionWrapper的executeMessage
msg.recycle();
return;
}
mH.sendMessage(msg);
}
*/
public void executeMessage(Message msg) {
switch (msg.what) {
case DO_DISPATCH_KEY_EVENT: {
SomeArgs args = (SomeArgs)msg.obj;
mInputMethodSession.dispatchKeyEvent(msg.arg1,
(KeyEvent)args.arg1,
new InputMethodEventCallbackWrapper(
(IInputMethodCallback)args.arg2));
args.recycle();
return;
}
}
}
InputMethodSession mInputMethodSession;
frameworks/base/core/java/android/inputmethodservice/AbstractInputMethodService.java
public abstract class AbstractInputMethodSessionImpl implements InputMethodSession {
public void dispatchKeyEvent(int seq, KeyEvent event, EventCallback callback) {
boolean handled = event.dispatch(AbstractInputMethodService.this,
mDispatcherState, this); //分发给输入法应用
if (callback != null) {
//执行回调函数,如果输入法应用没有处理、handled为false;反之为true。这将决定系统是否再次分发!!!
callback.finishedEvent(seq, handled);
}
}
}
说明:如果输入法没有处理;即onKeyDown返回false,将调用ViewRootImpl的handleImeFinishedEvent处理、即重新分发。
frameworks/base/core/java/android/view/KeyEvent.java
public final boolean dispatch(Callback receiver) {
return dispatch(receiver, null, null);
}
public final boolean dispatch(Callback receiver, DispatcherState state,
Object target) {
switch (mAction) {
case ACTION_DOWN: {
boolean res = receiver.onKeyDown(mKeyCode, this);
return res;
}
}
}
二、输入法应用的处理
packages/inputmethods/PinyinIME/src/com/android/inputmethod/pinyin/PinyinIME.java
public class PinyinIME extends InputMethodService {
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (processKey(event, 0 != event.getRepeatCount())) return true;
return super.onKeyDown(keyCode, event);
}
}
private boolean processKey(KeyEvent event, boolean realAction) {
if (processFunctionKeys(keyCode, realAction)) {
//del键将调用getCurrentInputConnection().deleteSurroundingText(1, 0);
return true;
}
commitResultText(String.valueOf((char) keyChar));
//调用getCurrentInputConnection().commitText(resultText, 1);
}
输入法与应用程序间的通信
说明:以下分析也就是上述commitText和deleteSurroundingText
1.接口类
frameworks/base/core/java/view/inputmethod/InputConnection.java
public interface InputConnection {
public boolean deleteSurroundingText(int beforeLength, int afterLength); //删除输入的字符
public boolean commitText(CharSequence text, int newCursorPosition); //输入字符
public boolean sendKeyEvent(KeyEvent event); //注入按键
}
2.基础类
frameworks/base/core/java/view/inputmethod/BaseInputConnection.java
public class BaseInputConnection implements InputConnection {
}
3.使用类
对于普通输入框是这个:
frameworks/base/core/java/com/android/internal/widget/EditableInputConnection.java
public boolean commitText(CharSequence text, int newCursorPosition) {
if (mTextView == null) {
return super.commitText(text, newCursorPosition);
}
if (text instanceof Spanned) {
Spanned spanned = ((Spanned) text);
SuggestionSpan[] spans = spanned.getSpans(0, text.length(), SuggestionSpan.class);
mIMM.registerSuggestionSpansForNotification(spans);
}
mTextView.resetErrorChangedFlag();
boolean success = super.commitText(text, newCursorPosition);
mTextView.hideErrorIfUnchanged();
return success;
}
deleteSurroundingText方法该类没有实现,找其父类BaseInputConnection.java
对于浏览器的输入框是这个:
frameworks/base/core/java/android/webkit/WebViewClassic.java
public boolean commitText(CharSequence text, int newCursorPosition) {
setComposingText(text, newCursorPosition);
finishComposingText();
return true;
}
public boolean deleteSurroundingText(int leftLength, int rightLength) {
}
三、调试解决问题
浏览器在打开输入法时;用硬件键盘输入符号,不能删除。
原理:
浏览器WebViewClassic机制决定如果字符输入不通过它,录入个数将不被增加;就会导致deleteSurroundingText时无法在删除所有(只能删除记录的个数)。我们目前问题是:符号键录入时没有通过WebViewClassic而是通过系统送给Web应用,但删除时统一使用deleteSurroundingText、不经过系统分发Web应用,这就导致有符号键输入时、不能将所有的输入删除。这部分修改输入法应用在按键录入部分添加对符号按键的支持;或做判断,当有不经过WebViewClassic录入的按键时,删除时也能交由系统处理。
另外一种修改方法就是通过修改系统WebKit。
frameworks/base/core/java/android/webkit/WebViewClassic.java
public boolean deleteSurroundingText(int leftLength, int rightLength) {
Editable content = getEditable();
//add tankai, input by keyboard , but can't delete all string ,
// eg: "asdkjhjk';/,.," , then press backspace
if(getEditable().toString().isEmpty()) {
mIsKeySentByMe = true;
sendKey(KeyEvent.KEYCODE_DEL);
mIsKeySentByMe = false;
return false;
}
//end tankai
}
四、输入法的切换(转载)
转载自:http://xiaohang.org/2011/02/android%E8%BE%93%E5%85%A5%E6%B3%95%E4%B9%8B%E2%80%94%E2%80%94%E5%A6%82%E4%BD%95%E5%9C%A8%E4%BB%A3%E7%A0%81%E4%B8%AD%E5%BC%BA%E5%88%B6%E5%88%87%E6%8D%A2%E8%BE%93%E5%85%A5%E6%B3%95/
由于工作需要,追踪Android输入法Framework中了关于输入法切换的实现过程或者说是API调用的部分。
找到如下两个API:
1.InputMethodManager.setInputMethod (IBinder token, String id)
public void setInputMethod (IBinder token, String id)
Force switch to a new input method component. This can only be called from the currently active input method, as validated by the given token.
Parameters
token Supplies the identifying token given to an input method when it was started, which allows it to perform this operation on itself.
id The unique identifier for the new input method to be switched to.
此函数,当然是可以实现输入法的强制切换的,但是,IBinder的获得却需要一定的权限,调用起来需要比较深入,哥们能力有限专研的一阵子未果。日后,再做深入探讨吧。哪位仁兄有解,欢迎留言,不胜感激!!
2.InputMethodService.switchInputMethod(String id)
public void switchInputMethod (String id)
Since: API Level 3 Force switch to a new input method, as identified by id. This input method will be destroyed, and the requested one started on the current input field.
Parameters
id Unique identifier of the new input method ot start.
相对第一个API来说,这个却简单的多。最直观来看的话,就是这里不用获得相应的IBinder!我们几乎已经胜利了,但是,还是需要点破这层纸的。
首先,关于该API在何处调用?
该API属于类InputMethodService,而每个输入应用的入口类又都是继承于该类的,所以,在输入法入口类调用该API就可以了。比如,Google的SDK中给出的Google拼音输入法,就在类PinyinIME.java中调用就可以了!
再者,关于参数id?
从定义来看,它是一个String类型,看Google的解释也未免笼统。经小可仔细专研,原来,是这样的,还是用例子来说吧,依然选择上述例子。 Google拼音输入法的id就是:com.android.inputmethod.pinyin/.PinyinIME。完整的调用方法就 是:switchInputMethod(“com.android.inputmethod.pinyin/.PinyinIME”);
那么,如果不在入口类中如何调用呢?
在需要调用的类中定义:private PinyinIME mImeService;
需要的地方调用就可以了:mImeService.switchInputMethod(“com.android.inputmethod.pinyin/.PinyinIME”);
OK!打完收工!