Force unsilence record clients on startInput

We call startRecording unconditionally in startInput, so we must
update the client state to be unsilenced (since we are treating as
such). We subsequently re-update the silence state (with the client
marked as active to dispatch ops) in updateUidStates_l.

This fixes an issue where we call startRecording for a silenced client,
then call it again when it moves to unsilenced when the client is active.
Since startRecording is ref-counted, this leaves the client in the
recording state leading to incorrect appop attributions.

Bug: 279905816
Bug: 281485019
Test: Manual verification of repro cases + verbose log analysis
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:e7720b379bfaba648ab6d85c4c2df6f03ec854d3)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:12e41309c3e6bde85430c288e469fc1776835db0)
Merged-In: I31d50457ca8adae577407a28d4d4c0e8582bac5d
Change-Id: I31d50457ca8adae577407a28d4d4c0e8582bac5d
(cherry picked from commit dee3417a9c90c9b0631dd10db5274dcb6c677c3a)
diff --git a/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp b/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
index 92029b2..9d4c1ef 100644
--- a/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
+++ b/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
@@ -723,8 +723,29 @@
 
     Mutex::Autolock _l(mLock);
 
+    ALOGW_IF(client->silenced, "startInput on silenced input for port %d, uid %d. Unsilencing.",
+            portIdAidl,
+            client->attributionSource.uid);
+
+    if (client->active) {
+        ALOGE("Client should never be active before startInput. Uid %d port %d",
+                client->attributionSource.uid, portId);
+        finishRecording(client->attributionSource, client->attributes.source);
+        return binderStatusFromStatusT(INVALID_OPERATION);
+    }
+
+    // Force the possibly silenced client to be unsilenced since we just called
+    // startRecording (i.e. we have assumed it is unsilenced).
+    // At this point in time, the client is inactive, so no calls to appops are sent in
+    // setAppState_l.
+    // This ensures existing clients have the same behavior as new clients (starting unsilenced).
+    // TODO(b/282076713)
+    setAppState_l(client, APP_STATE_TOP);
+
     client->active = true;
     client->startTimeNs = systemTime();
+    // This call updates the silenced state, and since we are active, appropriately notifies appops
+    // if we silence the track.
     updateUidStates_l();
 
     status_t status;