From ce62280d4beb110f9695ddc0e28ca3cf48948635 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 20 Jul 2022 22:29:01 +0000 Subject: [PATCH] Refactor ProximityUpdateCallbackInternal to interface Refactor ProximityUpdateCallbackInternal to interface. Also making sure that the service unbinding happens when clear Attention Service command is received. Bug: 214395649 Test: atest CtsVoiceInteractionTestCases, atest CtsAttentionServiceDeviceTestCases Change-Id: If8c23a9c68952e323fcb8eeb681e86b0e6119b18 --- .../attention/AttentionManagerInternal.java | 4 ++-- .../server/attention/AttentionManagerService.java | 6 ++---- .../HotwordDetectionConnection.java | 15 +++++++-------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/core/java/android/attention/AttentionManagerInternal.java b/core/java/android/attention/AttentionManagerInternal.java index 47bec618cfd9c..24fe0dbe5760a 100644 --- a/core/java/android/attention/AttentionManagerInternal.java +++ b/core/java/android/attention/AttentionManagerInternal.java @@ -83,11 +83,11 @@ public abstract class AttentionManagerInternal { } /** Internal interface for proximity callback. */ - public abstract static class ProximityUpdateCallbackInternal { + public interface ProximityUpdateCallbackInternal { /** * @param distance the estimated distance of the user (in meter) * The distance will be PROXIMITY_UNKNOWN if the proximity sensing was inconclusive. */ - public abstract void onProximityUpdate(double distance); + void onProximityUpdate(double distance); } } diff --git a/services/core/java/com/android/server/attention/AttentionManagerService.java b/services/core/java/com/android/server/attention/AttentionManagerService.java index a948ce22497e5..d4ef638d08181 100644 --- a/services/core/java/com/android/server/attention/AttentionManagerService.java +++ b/services/core/java/com/android/server/attention/AttentionManagerService.java @@ -856,9 +856,6 @@ public class AttentionManagerService extends SystemService { @GuardedBy("mLock") private void cancelAndUnbindLocked() { synchronized (mLock) { - if (mCurrentAttentionCheck == null && mCurrentProximityUpdate == null) { - return; - } if (mCurrentAttentionCheck != null) { cancel(); } @@ -940,7 +937,7 @@ public class AttentionManagerService extends SystemService { } } - class TestableProximityUpdateCallbackInternal extends ProximityUpdateCallbackInternal { + class TestableProximityUpdateCallbackInternal implements ProximityUpdateCallbackInternal { private double mLastCallbackCode = PROXIMITY_UNKNOWN; @Override @@ -1072,6 +1069,7 @@ public class AttentionManagerService extends SystemService { private void resetStates() { synchronized (mLock) { mCurrentProximityUpdate = null; + cancelAndUnbindLocked(); } mComponentName = resolveAttentionService(mContext); } diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java index 96491ac4553f6..2eae68bd31827 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java @@ -192,14 +192,7 @@ final class HotwordDetectionConnection { final AttentionManagerInternal mAttentionManagerInternal; final AttentionManagerInternal.ProximityUpdateCallbackInternal mProximityCallbackInternal = - new AttentionManagerInternal.ProximityUpdateCallbackInternal() { - @Override - public void onProximityUpdate(double distance) { - synchronized (mLock) { - mProximityMeters = distance; - } - } - }; + this::setProximityMeters; volatile HotwordDetectionServiceIdentity mIdentity; @@ -1190,6 +1183,12 @@ final class HotwordDetectionConnection { } } + private void setProximityMeters(double proximityMeters) { + synchronized (mLock) { + mProximityMeters = proximityMeters; + } + } + private static void bestEffortClose(Closeable... closeables) { for (Closeable closeable : closeables) { bestEffortClose(closeable);