输入事件ANR原理分析

本文详细解析了Android系统中输入事件导致ANR(应用无响应)的原理,涉及InputDispatcher如何检测和处理ANR,以及ANR发生后的处理流程,包括窗口未准备好处理事件的判断、超时时间的设定以及ANR通知的发送机制。
摘要由CSDN通过智能技术生成

输入事件ANR原理分析

1.简介

当输入事件长时间未响应,则会发生ANR。输入事件ANR超时时间一般为5s。在InputDispatcher分发输入事件的过程中,会监控是否发生了ANR。具体是在执行findFocusedWindowTargetsLocked()方法时,如果当前窗口还未准备好处理输入事件,则会调用handleTargetsNotReadyLocked()方法,判断是否发生了ANR。判断的依据是等待的时间是否超过了5s,如果超过了5s,则执行onANRLocked()方法,发生ANR通知。

2.源码分析

接下来将从findFocusedWindowTargetsLocked()开始分析,ANR产生的过程。

2.1 InputDispatcher.findFocusedWindowTargetsLocked()

int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime,
    const EventEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) {
    int32_t injectionResult;
    String8 reason;
    if (mFocusedWindowHandle == NULL) {
        if (mFocusedApplicationHandle != NULL) {
            injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
                mFocusedApplicationHandle, NULL, nextWakeupTime,
                "Waiting because no window has focus but there is a "
                "focused application that may eventually add a window "
                "when it finishes starting up.");//当没有找到聚焦的窗口时,见2.3
                goto Unresponsive;
        }
        goto Failed;
    }

    reason = checkWindowReadyForMoreInputLocked(currentTime,
            mFocusedWindowHandle, entry, "focused");//检查窗口是否准备接受多个输入事件,见2.2
    if (!reason.isEmpty()) {
        injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
                mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime, reason.string());//不能接受处理多个输入事件,即当前正在处理其他事件,需要等待,见2.3
        goto Unresponsive;
    }
    .....
}

在findFocusedWindowTargetsLocked()方法中,有两种情况会执行handleTargetsNotReadyLocked()方法,一种是当前没有获取焦点的窗口,另外一种是当前正在处理其他事件,不支持窗口处理更多的输入事件,需要等待。

2.2 InputDispatcher.checkWindowReadyForMoreInputLocked()

String8 InputDispatcher::checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
    const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry,
    const char* targetType) {
    // 1. 窗口被暂停了,继续等待
    if (windowHandle->getInfo()->paused) {
        return String8::format("Waiting because the %s window is paused.", targetType);
    }
    // 2. 窗口连接还未被注册,继续等待
    ssize_t connectionIndex = getConnectionIndexLocked(windowHandle->getInputChannel());
    if (connectionIndex < 0) {
        return String8::format("Waiting because the %s window's input channel is not "
            "registered with the input dispatcher.  The window may be in the process "
            "of being removed.", targetType);
    }
    // 3. 连接已经死亡了,继续等待
    sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
    if (connection->status != Connection::STATUS_NORMAL) {
        return String8::format("Waiting because the %s window's input connection is %s."
            "The window may be in the process of being removed.", targetType,
            connection->getStatusLabel());
    }
    // 4. 连接上的队列已经满了,继续等待
    if (connection->inputPublisherBlocked) {
        return String8::format("Waiting because the %s window's input channel is full.  "
            "Outbound queue length: %d.  Wait queue length: %d.",
            targetType, connection->outboundQueue.count(), connection->waitQueue.count());
    }

    if (eventEntry->type == EventEntry::TYPE_KEY) {
        // 5.按键事件必须等待所有之前的输入事件处理完了,才能继续处理下一个,因为之前的按键事件可能会对聚焦的窗口有影响.
        if (!connection->outboundQueue.isEmpty() || !connection->waitQueue.isEmpty()) {
            return String8::format("Waiting to send key event because the %s window has not "
                "finished processing all of the input events that were previously "
                "delivered to it.  Outbound queue length: %d.  Wait queue length: %d.",
                targetType, connection->outboundQueue.count(), connection->waitQueue.count());
        }
    }else{
        // 6.因为应用没有响应,等待队列堆积了很多触摸事件,此时将触发ANR。
        if (!connection->waitQueue.isEmpty()
                && currentTime >= connection->waitQueue.head->deliveryTime
                    + STREAM_AHEAD_EVENT_TIMEOUT) {//STREAM_AHEAD_EVENT_TIMEOUT为500ms
            return String8::format("Waiting to send non-key event because the %s window has not "
                "finished processing certain input events that were delivered to it over "
                "%0.1fms ago.  Wait queue length: %d.  Wait queue head age: %0.1fms.",
                targetType, STREAM_AHEAD_EVENT_TIMEOUT * 0.000001f,
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值