Fix sensor privacy check in audio policy service

This change addresses feedback for change
I6f9c762086cfa3900ec9585aaf68e458c9dadff5.

Test: Manual
Bug: 162549680
Change-Id: I0b4a318d0d14e545a845e9ac53ef170e4c5cb4c6
diff --git a/services/audiopolicy/service/AudioPolicyService.cpp b/services/audiopolicy/service/AudioPolicyService.cpp
index 80022c8..935fe94 100644
--- a/services/audiopolicy/service/AudioPolicyService.cpp
+++ b/services/audiopolicy/service/AudioPolicyService.cpp
@@ -472,34 +472,35 @@
 {
 //    Go over all active clients and allow capture (does not force silence) in the
 //    following cases:
-//    The client is the assistant
-//        AND an accessibility service is on TOP or a RTT call is active
+//    The client source is virtual (remote submix, call audio TX or RX...)
+//    OR The user the client is running in has microphone sensor privacy disabled
+//        AND The client is the assistant
+//                AND an accessibility service is on TOP or a RTT call is active
+//                        AND the source is VOICE_RECOGNITION or HOTWORD
+//                    OR uses VOICE_RECOGNITION AND is on TOP
+//                        OR uses HOTWORD
+//                    AND there is no active privacy sensitive capture or call
+//                        OR client has CAPTURE_AUDIO_OUTPUT privileged permission
+//            OR The client is an accessibility service
+//                AND Is on TOP
+//                        AND the source is VOICE_RECOGNITION or HOTWORD
+//                    OR The assistant is not on TOP
+//                        AND there is no active privacy sensitive capture or call
+//                            OR client has CAPTURE_AUDIO_OUTPUT privileged permission
+//                AND is on TOP
 //                AND the source is VOICE_RECOGNITION or HOTWORD
-//            OR uses VOICE_RECOGNITION AND is on TOP
-//                OR uses HOTWORD
-//            AND there is no active privacy sensitive capture or call
-//                OR client has CAPTURE_AUDIO_OUTPUT privileged permission
-//    OR The client is an accessibility service
-//        AND Is on TOP
-//                AND the source is VOICE_RECOGNITION or HOTWORD
-//            OR The assistant is not on TOP
+//            OR the client source is HOTWORD
+//                AND is on TOP
+//                    OR all active clients are using HOTWORD source
+//                AND no call is active
+//                    OR client has CAPTURE_AUDIO_OUTPUT privileged permission
+//            OR the client is the current InputMethodService
+//                AND a RTT call is active AND the source is VOICE_RECOGNITION
+//            OR Any client
+//                AND The assistant is not on TOP
+//                AND is on TOP or latest started
 //                AND there is no active privacy sensitive capture or call
 //                    OR client has CAPTURE_AUDIO_OUTPUT privileged permission
-//        AND is on TOP
-//        AND the source is VOICE_RECOGNITION or HOTWORD
-//    OR the client source is virtual (remote submix, call audio TX or RX...)
-//    OR the client source is HOTWORD
-//        AND is on TOP
-//            OR all active clients are using HOTWORD source
-//        AND no call is active
-//            OR client has CAPTURE_AUDIO_OUTPUT privileged permission
-//    OR the client is the current InputMethodService
-//        AND a RTT call is active AND the source is VOICE_RECOGNITION
-//    OR Any client
-//        AND The assistant is not on TOP
-//        AND is on TOP or latest started
-//        AND there is no active privacy sensitive capture or call
-//            OR client has CAPTURE_AUDIO_OUTPUT privileged permission
 
 
     sp<AudioRecordClient> topActive;
@@ -529,12 +530,8 @@
 
     for (size_t i =0; i < mAudioRecordClients.size(); i++) {
         sp<AudioRecordClient> current = mAudioRecordClients[i];
-        if (!isVirtualSource(current->attributes.source)
-                && isUserSensorPrivacyEnabledForUid(current->uid)) {
-            setAppState_l(current->portId, APP_STATE_IDLE);
-            continue;
-        }
-        if (!current->active) {
+        if (!current->active || (!isVirtualSource(current->attributes.source)
+                && isUserSensorPrivacyEnabledForUid(current->uid))) {
             continue;
         }
 
@@ -662,6 +659,9 @@
         if (isVirtualSource(source)) {
             // Allow capture for virtual (remote submix, call audio TX or RX...) sources
             allowCapture = true;
+        } else if (isUserSensorPrivacyEnabledForUid(current->uid)) {
+            // If sensor privacy is enabled, don't allow capture
+            allowCapture = false;
         } else if (mUidPolicy->isAssistantUid(current->uid)) {
             // For assistant allow capture if:
             //     An accessibility service is on TOP or a RTT call is active