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

Commit 70424ac2 authored by Darrell Shi's avatar Darrell Shi
Browse files

Fix input monitor issue.

This CL fixes an issue of the communal mode where unlocking the device
doesn't properly dispose the touch event listener, and it keeps
triggering the callback in personal mode. The issue is fixed by handling
the InputEventReceiver and disposing it when no longer needed.

Change-Id: I0008ceb3edc9dfdbcbfa09c1f68c11e87d2e1f7f
Test: atest IdleHostViewControllerTest#testInputEventReceiverLifecycle
Bug: 197365101
parent c4c62694
Loading
Loading
Loading
Loading
+9 −3
Original line number Original line Diff line number Diff line
@@ -39,6 +39,7 @@ import com.android.systemui.R;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.shared.system.InputChannelCompat;
import com.android.systemui.shared.system.InputMonitorCompat;
import com.android.systemui.shared.system.InputMonitorCompat;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.ViewController;
import com.android.systemui.util.ViewController;
@@ -139,6 +140,9 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
    // Monitor for tracking touches for activity.
    // Monitor for tracking touches for activity.
    private InputMonitorCompat mInputMonitor;
    private InputMonitorCompat mInputMonitor;


    // Input receiver of touch activities.
    private InputChannelCompat.InputEventReceiver mInputEventReceiver;

    // Intent filter for receiving dream broadcasts.
    // Intent filter for receiving dream broadcasts.
    private IntentFilter mDreamIntentFilter;
    private IntentFilter mDreamIntentFilter;


@@ -335,7 +339,7 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
    }
    }


    private void enableIdleMonitoring(boolean enable) {
    private void enableIdleMonitoring(boolean enable) {
        if (enable && mInputMonitor == null) {
        if (enable && mInputMonitor == null && mInputEventReceiver == null) {
            if (DEBUG) {
            if (DEBUG) {
                Log.d(TAG, "enable idle monitoring");
                Log.d(TAG, "enable idle monitoring");
            }
            }
@@ -345,7 +349,7 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme


            // Monitor - any input should reset timer
            // Monitor - any input should reset timer
            mInputMonitor = mInputMonitorFactory.getInputMonitor(INPUT_MONITOR_IDENTIFIER);
            mInputMonitor = mInputMonitorFactory.getInputMonitor(INPUT_MONITOR_IDENTIFIER);
            mInputMonitor.getInputReceiver(mLooper, mChoreographer,
            mInputEventReceiver = mInputMonitor.getInputReceiver(mLooper, mChoreographer,
                    v -> {
                    v -> {
                        if (DEBUG) {
                        if (DEBUG) {
                            Log.d(TAG, "touch detected, resetting timeout");
                            Log.d(TAG, "touch detected, resetting timeout");
@@ -358,7 +362,7 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
                        mCancelEnableIdling = mDelayableExecutor.executeDelayed(
                        mCancelEnableIdling = mDelayableExecutor.executeDelayed(
                                mEnableIdlingCallback, mIdleTimeout);
                                mEnableIdlingCallback, mIdleTimeout);
                    });
                    });
        } else if (!enable && mInputMonitor != null) {
        } else if (!enable && mInputMonitor != null && mInputEventReceiver != null) {
            if (DEBUG) {
            if (DEBUG) {
                Log.d(TAG, "disable idle monitoring");
                Log.d(TAG, "disable idle monitoring");
            }
            }
@@ -368,7 +372,9 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
                mCancelEnableIdling = null;
                mCancelEnableIdling = null;
            }
            }


            mInputEventReceiver.dispose();
            mInputMonitor.dispose();
            mInputMonitor.dispose();
            mInputEventReceiver = null;
            mInputMonitor = null;
            mInputMonitor = null;
        }
        }
    }
    }
+20 −0
Original line number Original line Diff line number Diff line
@@ -42,6 +42,7 @@ import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.shared.system.InputChannelCompat;
import com.android.systemui.shared.system.InputMonitorCompat;
import com.android.systemui.shared.system.InputMonitorCompat;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.concurrency.DelayableExecutor;
import com.android.systemui.util.concurrency.DelayableExecutor;
@@ -76,6 +77,7 @@ public class IdleHostViewControllerTest extends SysuiTestCase {
    @Mock private Sensor mSensor;
    @Mock private Sensor mSensor;
    @Mock private DreamHelper mDreamHelper;
    @Mock private DreamHelper mDreamHelper;
    @Mock private InputMonitorCompat mInputMonitor;
    @Mock private InputMonitorCompat mInputMonitor;
    @Mock private InputChannelCompat.InputEventReceiver mInputEventReceiver;


    private final long mTimestamp = Instant.now().toEpochMilli();
    private final long mTimestamp = Instant.now().toEpochMilli();
    private KeyguardStateController.Callback mKeyguardStateCallback;
    private KeyguardStateController.Callback mKeyguardStateCallback;
@@ -91,6 +93,7 @@ public class IdleHostViewControllerTest extends SysuiTestCase {
        when(mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)).thenReturn(mSensor);
        when(mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)).thenReturn(mSensor);
        when(mInputMonitorFactory.getInputMonitor("IdleHostViewController"))
        when(mInputMonitorFactory.getInputMonitor("IdleHostViewController"))
                .thenReturn(mInputMonitor);
                .thenReturn(mInputMonitor);
        when(mInputMonitor.getInputReceiver(any(), any(), any())).thenReturn(mInputEventReceiver);


        mController = new IdleHostViewController(mContext,
        mController = new IdleHostViewController(mContext,
                mBroadcastDispatcher, mPowerManager, mSensorManager, mIdleHostView,
                mBroadcastDispatcher, mPowerManager, mSensorManager, mIdleHostView,
@@ -231,4 +234,21 @@ public class IdleHostViewControllerTest extends SysuiTestCase {
        // Verifies it goes to sleep.
        // Verifies it goes to sleep.
        verify(mPowerManager).goToSleep(anyLong(), anyInt(), anyInt());
        verify(mPowerManager).goToSleep(anyLong(), anyInt(), anyInt());
    }
    }

    @Test
    public void testInputEventReceiverLifecycle() {
        // Keyguard showing.
        when(mKeyguardStateController.isShowing()).thenReturn(true);
        mKeyguardStateCallback.onKeyguardShowingChanged();

        // Should register input event receiver.
        verify(mInputMonitor).getInputReceiver(any(), any(), any());

        // Keyguard dismissed.
        when(mKeyguardStateController.isShowing()).thenReturn(false);
        mKeyguardStateCallback.onKeyguardShowingChanged();

        // Should dispose input event receiver.
        verify(mInputEventReceiver).dispose();
    }
}
}