InputManagerService的createInputChannel方法用于创建输入Channel,代码如下:
//frameworks/base/services/core/java/android/server/input/InputManagerService.java
public class InputManagerService extends IInputManager.Stub
implements Watchdog.Monitor {
private final NativeInputManagerService mNative;
public InputChannel createInputChannel(String name) {
return mNative.createInputChannel(name);
}
}
调用NativeInputManagerService的createInputChannel方法,NativeInputManagerService是一个接口,由NativeImpl实现,createInputChannel是一个Native方法,在com_android_server_input_inputManagerService.cpp中实现:
//frameworks/base/services/core/jni/com_android_server_input_inputManagerService.cpp
static jobject nativeCreateInputChannel(JNIEnv* env, jobject nativeImplObj, jstring nameObj) {
NativeInputManager* im = getNativeInputManager(env, nativeImplObj);
ScopedUtfChars nameChars(env, nameObj);
std::string name = nameChars.c_str();
base::Result<std::unique_ptr<InputChannel>> inputChannel = im->createInputChannel(name);
if (!inputChannel.ok()) {
std::string message = inputChannel.error().message();
message += StringPrintf(" Status=%d", static_cast<int>(inputChannel.error().code()));
jniThrowRuntimeException(env, message.c_str());
return nullptr;
}
jobject inputChannelObj =
android_view_InputChannel_createJavaObject(env, std::move(*inputChannel));
if (!inputChannelObj) {
return nullptr;
}
android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
handleInputChannelDisposed, im);
return inputChannelObj;
}
调用NativeInputManager的createInputChannel方法:
//frameworks/base/services/core/jni/com_android_server_input_inputManagerService.cpp
sp<InputManagerInterface> mInputManager;
base::Result<std::unique_ptr<InputChannel>> NativeInputManager::createInputChannel(
const std::string& name) {
ATRACE_CALL();
return mInputManager->getDispatcher().createInputChannel(name);
}
InputDispatcher createInputChannel
调用mInputManager(InputManagerInterface)的getDispatcher方法,取得InputDispatcherInterface,然后调用InputDispatcherInterface的createInputChannel方法,由InputDispatcher实现:
//frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp
Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) {
if (DEBUG_CHANNEL_CREATION) {
ALOGD("channel '%s' ~ createInputChannel", name.c_str());
}
std::unique_ptr<InputChannel> serverChannel;
std::unique_ptr<InputChannel> clientChannel;
// 将serverChannel(接收端) 与 clientChannel(发送端) 绑定
status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
if (result) {
return base::Error(result) << "Failed to open input channel pair with name " << name;
}
{ // acquire lock
std::scoped_lock _l(mLock);
const sp<IBinder>& token = serverChannel->getConnectionToken();
int fd = serverChannel->getFd();
sp<Connection> connection =
new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator);
if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) {
ALOGE("Created a new connection, but the token %p is already known", token.get());
}
mConnectionsByToken.emplace(token, connection);
// 注册回调 handleReceiveCallback 方法
std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
this, std::placeholders::_1, token);
// 将callback 与 FD(即serverChannel) 关联起来
mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr);
} // release lock
// Wake the looper because some connections have changed.
mLooper->wake();
return clientChannel;
}
InputDispatcher handleReceiveCallback
当有输入事件时就会调用InputDispatcher的handleReceiveCallback方法:
待更新