IMS分为Java层与Native层两个部分,其启动过程是从Java部分的初始化开始,进而完成Native部分的初始化。
□创建新的IMS对象。
□调用IMS对象的start()函数完成启动
同其他系统服务一样,IMS在SystemServer中的ServerThread线程中启动。
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startOtherServices");
inputManager = new InputManagerService(context);
inputManager.setWindowManagerCallbacks(wm.getInputManagerCallback());
inputManager.start();
创建新的IMS对象
线程 mLooper = DisplayThread.get().getLooper()
构造NativeInputManagerService
/** Point of injection for test dependencies. */
@VisibleForTesting
static class Injector {
private final Context mContext;
private final Looper mLooper;
Injector(Context context, Looper looper) {
mContext = context;
mLooper = looper;
}
Context getContext() {
return mContext;
}
Looper getLooper() {
return mLooper;
}
NativeInputManagerService getNativeService(InputManagerService service) {
return new NativeInputManagerService.NativeImpl(service, mLooper.getQueue());
}
void registerLocalService(InputManagerInternal localService) {
LocalServices.addService(InputManagerInternal.class, localService);
}
}
public InputManagerService(Context context) {
this(new Injector(context, DisplayThread.get().getLooper()));
}
class NativeImpl implements NativeInputManagerService {
/** Pointer to native input manager service object, used by native code. */
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final long mPtr;
NativeImpl(InputManagerService service, MessageQueue messageQueue) {
mPtr = init(service, messageQueue);
}
nativeInit()函数创建了一个类型为NativeInputManager的对象,它是Java层与Native层互相通信的桥梁。
com_android_server_input_InputManagerService.cpp
static jlong nativeInit(JNIEnv* env, jclass /* clazz */, jobject serviceObj,
jobject messageQueueObj) {
sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
if (messageQueue == nullptr) {
jniThrowRuntimeException(env, "MessageQueue is not initialized.");
return 0;
}
static std::once_flag nativeInitialize;
NativeInputManager* im = nullptr;
std::call_once(nativeInitialize, [&]() {
// Create the NativeInputManager, which should not be destroyed or deallocated for the
// lifetime of the process.
im = new NativeInputManager(serviceObj, messageQueue->getLooper());
});
LOG_ALWAYS_FATAL_IF(im == nullptr, "NativeInputManager was already initialized.");
return reinterpret_cast<jlong>(im);
}
看下这个类的声明可以发现,它实现了InputReaderPolicyInterface与InputDispatcher-PolicyInterface两个接口。这说明上一节曾经介绍过的两个重要的输入系统参与者InputReaderPolicy和InputDispatcherPolicy是由NativeInputManager实现的,然而它仅仅为两个策略提供接口实现而已,并不是策略的实际实现者。NativeInputManager通过JNI回调Java层的IMS,由它完成决策。本节暂不讨论其实现细节,读者只要先记住两个策略参与者的接口实现位于NativeInputManager即可。
4 class NativeInputManager : public virtual InputReaderPolicyInterface,
265 public virtual InputDispatcherPolicyInterface,
266 public virtual PointerControllerPolicyInterface {
NativeInputManager构造如下:
1、创建一个全局引用,并通过mServiceObj指向上层的InputManagerService对象
2、创建并注册服务InputManager。
原来,InputManager才是底层输入系统的服务,而NativeInputManagerService通过mServiceObj保存了上层InputManagerService引用,并且上层InputManagerService通过mPtr指向底层的NativeInputManager。因此,我们可以判定NativeInputManagerService就是一个连接上层与底层的桥梁。
NativeInputManager::NativeInputManager(jobject serviceObj, const sp<Looper>& looper)
: mLooper(looper), mInteractive(true) {
JNIEnv* env = jniEnv();
mServiceObj = env->NewGlobalRef(serviceObj);
InputManager* im = new InputManager(this, *this);
mInputManager = im;
defaultServiceManager()->addService(String16("inputflinger"), im);
}
InputManager.cpp
InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
InputDispatcherPolicyInterface& dispatcherPolicy) {
mDispatcher = createInputDispatcher(dispatcherPolicy); 4、创建InputDispatcher对象,使用InputReaderPolicyInterface
mProcessor = std::make_unique<InputProcessor>(*mDispatcher);
mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mProcessor);
mReader = createInputReader(readerPolicy, *mBlocker); 4、创建InputReader对象,使用InputReaderPolicyInterface和InputListenerInterface
}
可以看到InputManager
主要做以下几件事:
- 构造InputDispatcher对象;(用于后续事件分发处理)
- 构造InputReader对象;(用于事件输入监听)
- 调用InputDispatcher和InputReader的start()方法;
InputManager构造函数所使用的两个接口,分别由InputDispatcher和InputReader所使用。因此InputManager向上通信的能力是由子模块InputDispatcher和InputReader实现的。
InputManager创建了,InputReader、InputDispatcher。
InputReader负责从EventHub中获取事件,然后把事件加工后,发送给InputClassifier。
最后InputDispatcher会对事件进行分发。
frameworks/native/services/inputflinger/dispatcher/InputDispatcherFactory.cpp
std::unique_ptr<InputDispatcherInterface> createInputDispatcher(
InputDispatcherPolicyInterface& policy) {
return std::make_unique<android::inputdispatcher::InputDispatcher>(policy);
}
std::unique_ptr<InputReaderInterface> createInputReader(
const sp<InputReaderPolicyInterface>& policy, InputListenerInterface& listener) {
return std::make_unique<InputReader>(std::make_unique<EventHub>(), policy, listener);
}
调用IMS对象的start()函数完成启动
public void start() {
Slog.i(TAG, "Starting input manager");
mNative.start();
}
static void nativeStart(JNIEnv* env, jobject nativeImplObj) {
NativeInputManager* im = getNativeInputManager(env, nativeImplObj);
status_t result = im->getInputManager()->start();
if (result) {
jniThrowRuntimeException(env, "Input manager could not be started.");
}
}
status_t InputManager::start() {
status_t result = mDispatcher->start();
if (result) {
ALOGE("Could not start InputDispatcher thread due to error %d.", result);
return result;
}
result = mReader->start();
if (result) {
ALOGE("Could not start InputReader due to error %d.", result);
mDispatcher->stop();
return result;
}
return OK;
}
启动mDispatcher和mReader
status_t InputReader::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
return OK;
}
status_t InputDispatcher::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
return OK;
}