Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 19479dff authored by Vishnu Nair's avatar Vishnu Nair
Browse files

Handle ANR for cases where there are no focused windows

Fixes:b/143047723
Test: atest CtsWindowManagerDeviceTestCases:AnrTests

Change-Id: I8cd6433398cad84adff81d001756274794c9834e
parent 4a0b1750
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -1810,8 +1810,9 @@ public class InputManagerService extends IInputManager.Stub
    }

    // Native callback.
    private long notifyANR(IBinder token, String reason) {
        return mWindowManagerCallbacks.notifyANR(
    private long notifyANR(InputApplicationHandle inputApplicationHandle, IBinder token,
            String reason) {
        return mWindowManagerCallbacks.notifyANR(inputApplicationHandle,
                token, reason);
    }

@@ -2055,7 +2056,12 @@ public class InputManagerService extends IInputManager.Stub

        public void notifyInputChannelBroken(IBinder token);

        public long notifyANR(IBinder token, String reason);
        /**
         * Notifies the window manager about an application that is not responding.
         * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
         */
        long notifyANR(InputApplicationHandle inputApplicationHandle, IBinder token,
                String reason);

        public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags);

+3 −2
Original line number Diff line number Diff line
@@ -297,10 +297,10 @@ import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;

import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.ResolverActivity;
import com.android.internal.content.ReferrerIntent;
import com.android.internal.R;
import com.android.internal.util.ToBooleanFunction;
import com.android.internal.util.XmlUtils;
import com.android.server.AttributeCache;
@@ -374,6 +374,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
     * Value to increment the z-layer when boosting a layer during animations. BOOST in l33tsp34k.
     */
    @VisibleForTesting static final int Z_BOOST_BASE = 800570000;
    static final int INVALID_PID = -1;

    final ActivityTaskManagerService mAtmService;
    final ActivityInfo info; // activity info provided by developer in AndroidManifest
@@ -5350,7 +5351,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
            anrActivity = getWaitingHistoryRecordLocked();
            anrApp = app;
            windowFromSameProcessAsActivity =
                    !hasProcess() || app.getPid() == windowPid || windowPid == -1;
                    !hasProcess() || app.getPid() == windowPid || windowPid == INVALID_PID;
        }

        if (windowFromSameProcessAsActivity) {
+9 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

import static com.android.server.wm.ActivityRecord.INVALID_PID;
import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT;
import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
@@ -14,6 +15,7 @@ import android.os.IBinder;
import android.os.RemoteException;
import android.util.Slog;
import android.view.IWindow;
import android.view.InputApplicationHandle;
import android.view.KeyEvent;
import android.view.WindowManager;

@@ -80,7 +82,8 @@ final class InputManagerCallback implements InputManagerService.WindowManagerCal
     * Called by the InputManager.
     */
    @Override
    public long notifyANR(IBinder token, String reason) {
    public long notifyANR(InputApplicationHandle inputApplicationHandle,
            IBinder token, String reason) {
        ActivityRecord activity = null;
        WindowState windowState = null;
        boolean aboveSystem = false;
@@ -93,6 +96,10 @@ final class InputManagerCallback implements InputManagerService.WindowManagerCal
                }
            }

            if (activity == null && inputApplicationHandle != null) {
                activity = ActivityRecord.forTokenLocked(inputApplicationHandle.token);
            }

            if (windowState != null) {
                Slog.i(TAG_WM, "Input event dispatching timed out "
                        + "sending to " + windowState.mAttrs.getTitle()
@@ -122,7 +129,7 @@ final class InputManagerCallback implements InputManagerService.WindowManagerCal
            // Notify the activity manager about the timeout and let it decide whether
            // to abort dispatching or keep waiting.
            final boolean abort = activity.keyDispatchingTimedOut(reason,
                    windowState.mSession.mPid);
                    (windowState != null) ? windowState.mSession.mPid : INVALID_PID);
            if (!abort) {
                // The activity manager declined to abort dispatching.
                // Wait a bit longer and timeout again later.
+18 −3
Original line number Diff line number Diff line
@@ -709,6 +709,18 @@ void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
    checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
}

static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
        const sp<InputApplicationHandle>& inputApplicationHandle) {
    if (inputApplicationHandle == nullptr) {
        return nullptr;
    }
    NativeInputApplicationHandle* handle =
            static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get());

    return handle->getInputApplicationHandleObjLocalRef(env);
}


nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
        const sp<IBinder>& token, const std::string& reason) {
#if DEBUG_INPUT_DISPATCHER_POLICY
@@ -719,11 +731,14 @@ nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApp
    JNIEnv* env = jniEnv();
    ScopedLocalFrame localFrame(env);

    jobject inputApplicationHandleObj =
            getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);

    jobject tokenObj = javaObjectForIBinder(env, token);
    jstring reasonObj = env->NewStringUTF(reason.c_str());

    jlong newTimeout = env->CallLongMethod(mServiceObj,
                gServiceClassInfo.notifyANR, tokenObj,
            gServiceClassInfo.notifyANR, inputApplicationHandleObj, tokenObj,
                 reasonObj);
    if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
        newTimeout = 0; // abort dispatch
@@ -1865,7 +1880,7 @@ int register_android_server_InputManager(JNIEnv* env) {

    GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
            "notifyANR",
            "(Landroid/os/IBinder;Ljava/lang/String;)J");
            "(Landroid/view/InputApplicationHandle;Landroid/os/IBinder;Ljava/lang/String;)J");

    GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
            "filterInputEvent", "(Landroid/view/InputEvent;I)Z");