Merge "Fix input monitor issue."

This commit is contained in:
Darrell Shi
2021-08-25 23:37:35 +00:00
committed by Android (Google) Code Review
2 changed files with 29 additions and 3 deletions

View File

@@ -39,6 +39,7 @@ import com.android.systemui.R;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dagger.qualifiers.Main;
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.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.ViewController;
@@ -139,6 +140,9 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
// Monitor for tracking touches for activity.
private InputMonitorCompat mInputMonitor;
// Input receiver of touch activities.
private InputChannelCompat.InputEventReceiver mInputEventReceiver;
// Intent filter for receiving dream broadcasts.
private IntentFilter mDreamIntentFilter;
@@ -335,7 +339,7 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
}
private void enableIdleMonitoring(boolean enable) {
if (enable && mInputMonitor == null) {
if (enable && mInputMonitor == null && mInputEventReceiver == null) {
if (DEBUG) {
Log.d(TAG, "enable idle monitoring");
}
@@ -345,7 +349,7 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
// Monitor - any input should reset timer
mInputMonitor = mInputMonitorFactory.getInputMonitor(INPUT_MONITOR_IDENTIFIER);
mInputMonitor.getInputReceiver(mLooper, mChoreographer,
mInputEventReceiver = mInputMonitor.getInputReceiver(mLooper, mChoreographer,
v -> {
if (DEBUG) {
Log.d(TAG, "touch detected, resetting timeout");
@@ -358,7 +362,7 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
mCancelEnableIdling = mDelayableExecutor.executeDelayed(
mEnableIdlingCallback, mIdleTimeout);
});
} else if (!enable && mInputMonitor != null) {
} else if (!enable && mInputMonitor != null && mInputEventReceiver != null) {
if (DEBUG) {
Log.d(TAG, "disable idle monitoring");
}
@@ -368,7 +372,9 @@ public class IdleHostViewController extends ViewController<IdleHostView> impleme
mCancelEnableIdling = null;
}
mInputEventReceiver.dispose();
mInputMonitor.dispose();
mInputEventReceiver = null;
mInputMonitor = null;
}
}

View File

@@ -42,6 +42,7 @@ import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.broadcast.BroadcastDispatcher;
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.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.concurrency.DelayableExecutor;
@@ -76,6 +77,7 @@ public class IdleHostViewControllerTest extends SysuiTestCase {
@Mock private Sensor mSensor;
@Mock private DreamHelper mDreamHelper;
@Mock private InputMonitorCompat mInputMonitor;
@Mock private InputChannelCompat.InputEventReceiver mInputEventReceiver;
private final long mTimestamp = Instant.now().toEpochMilli();
private KeyguardStateController.Callback mKeyguardStateCallback;
@@ -91,6 +93,7 @@ public class IdleHostViewControllerTest extends SysuiTestCase {
when(mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)).thenReturn(mSensor);
when(mInputMonitorFactory.getInputMonitor("IdleHostViewController"))
.thenReturn(mInputMonitor);
when(mInputMonitor.getInputReceiver(any(), any(), any())).thenReturn(mInputEventReceiver);
mController = new IdleHostViewController(mContext,
mBroadcastDispatcher, mPowerManager, mSensorManager, mIdleHostView,
@@ -231,4 +234,21 @@ public class IdleHostViewControllerTest extends SysuiTestCase {
// Verifies it goes to sleep.
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();
}
}